summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Robinson <joe@lc8n.com>2010-12-21 18:00:21 +0000
committerLuke Bratch <l_bratch@yahoo.co.uk>2010-12-21 18:00:21 +0000
commitc04ccc9cc3aa91c634880cbfd2d77b58d68189ad (patch)
treeadf3565be9fa62b6c1092e53a621c69e756ad77b
parent610c22ab2bf4ac2c6abbce7991436a910dafe5f6 (diff)
Implement crouching
-rw-r--r--Level.cpp18
-rw-r--r--Player.cpp35
-rw-r--r--Player.h6
-rw-r--r--intensemarcus.h2
-rw-r--r--main.cpp10
-rw-r--r--marcussheet.pngbin46110 -> 53881 bytes
6 files changed, 71 insertions, 0 deletions
diff --git a/Level.cpp b/Level.cpp
index d956a78..9a230b0 100644
--- a/Level.cpp
+++ b/Level.cpp
@@ -23,6 +23,7 @@ void Level::changeLevel(int levelNo) {
player.setClip(ORIENT_BACK, MARCUS_WIDTH * 1, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
player.setClip(ORIENT_RIGHT, MARCUS_WIDTH * 2, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
player.setClip(ORIENT_LEFT, MARCUS_WIDTH * 3, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
+ player.setClip(ORIENT_CROUCH, MARCUS_WIDTH * 4, 0, MARCUS_WIDTH, CROUCH_HEIGHT);
// Read the map file (all level/player init should be done here, not above)
loadMap(levelNo);
@@ -53,6 +54,23 @@ void Level::move() {
// Player x-axis movement
rect.x = player.getX() + player.getXVel();
+ // Check for crouching, 2 is setting crouch on
+ if (player.getCrouch() == 2)
+ rect.h = CROUCH_HEIGHT;
+ // 1 is trying to uncrouch, don't let them if something is in the way
+ if (player.getCrouch() == 1) {
+ rect.h = MARCUS_HEIGHT;
+ rect.y = player.getY() - MARCUS_HEIGHT + CROUCH_HEIGHT;
+ // If there's no collision, set crouch to 0 (normal, uncrouched)
+ if (!checkCollision(rect)) {
+ player.setCrouch(0);
+ // If the check failed then just keep checking collisions in crouched position
+ } else {
+ rect.h = CROUCH_HEIGHT;
+ rect.y = player.getY();
+ }
+ }
+
// If there's a collision in the x-axis ...
while (checkCollision(rect)) {
// ... fit snugly to edge
diff --git a/Player.cpp b/Player.cpp
index e38492d..09d1743 100644
--- a/Player.cpp
+++ b/Player.cpp
@@ -4,6 +4,7 @@
Player::Player() {
health = 100;
sprint = false;
+ crouch = 0;
}
void Player::jump() {
@@ -31,3 +32,37 @@ int Player::getXVel() {
else
return xVel;
}
+
+/* Set crouching state
+ 0 = default, not crouching
+ 1 = attempting to stop crouching
+ 2 = crouching */
+void Player::setCrouch(int c) {
+ if (c == 2) {
+ crouch = 2;
+ setY(getY() + MARCUS_HEIGHT - CROUCH_HEIGHT);
+ } else if (c == 1) {
+ crouch = 1;
+ } else if (c == 0) {
+ crouch = 0;
+ setY(getY() - MARCUS_HEIGHT + CROUCH_HEIGHT);
+ }
+}
+
+// Override orient to give player crouching
+void Player::orient() {
+ if (crouch > 0)
+ clipNo = ORIENT_CROUCH;
+ else if (xVel < 0)
+ clipNo = ORIENT_LEFT;
+ else if (xVel > 0)
+ clipNo = ORIENT_RIGHT;
+ else if (yVel < 0)
+ clipNo = ORIENT_BACK;
+ else
+ clipNo = ORIENT_FRONT;
+}
+
+int Player::getCrouch() {
+ return crouch;
+}
diff --git a/Player.h b/Player.h
index ad08b56..7c8cdf7 100644
--- a/Player.h
+++ b/Player.h
@@ -18,8 +18,14 @@ class Player : public Creature {
int getXVel();
+ void setCrouch(int c);
+ int getCrouch();
+
+ void orient();
+
private:
bool sprint;
+ int crouch;
};
#endif
diff --git a/intensemarcus.h b/intensemarcus.h
index 3e98220..4724e85 100644
--- a/intensemarcus.h
+++ b/intensemarcus.h
@@ -15,6 +15,7 @@ const int ORIENT_FRONT = 0;
const int ORIENT_BACK = 1;
const int ORIENT_RIGHT = 2;
const int ORIENT_LEFT = 3;
+const int ORIENT_CROUCH = 4;
const int GRAVITY = 3;
const int JUMP_STRENGTH = 40;
const int MOVE_AMOUNT = 10;
@@ -22,6 +23,7 @@ const float SPRINT_MULTIPLYER = 1.5;
const int MAP_X = 48;
const int MAP_Y = 12;
const int TILE_SIZE = 50;
+const int CROUCH_HEIGHT = 30;
SDL_Surface *loadImage(std::string filename);
diff --git a/main.cpp b/main.cpp
index 9cd8f37..b6e0a73 100644
--- a/main.cpp
+++ b/main.cpp
@@ -52,6 +52,11 @@ int main(int argc, char* args[]) {
case SDLK_LCTRL:
level.player.setSprint(true);
break;
+ case SDLK_DOWN:
+ // Only crouch if they are not currently crouched
+ if (level.player.getCrouch() == 0)
+ level.player.setCrouch(2);
+ break;
}
} else if (event.type == SDL_KEYUP) {
// Adjust player velocity
@@ -67,6 +72,11 @@ int main(int argc, char* args[]) {
case SDLK_LCTRL:
level.player.setSprint(false);
break;
+ case SDLK_DOWN:
+ // Attempt to uncrouch, if crouching
+ if (level.player.getCrouch() == 2)
+ level.player.setCrouch(1);
+ break;
}
}
diff --git a/marcussheet.png b/marcussheet.png
index 9aba562..68a71cf 100644
--- a/marcussheet.png
+++ b/marcussheet.png
Binary files differ