diff options
-rw-r--r-- | Level.cpp | 18 | ||||
-rw-r--r-- | Player.cpp | 35 | ||||
-rw-r--r-- | Player.h | 6 | ||||
-rw-r--r-- | intensemarcus.h | 2 | ||||
-rw-r--r-- | main.cpp | 10 | ||||
-rw-r--r-- | marcussheet.png | bin | 46110 -> 53881 bytes |
6 files changed, 71 insertions, 0 deletions
@@ -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 @@ -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; +} @@ -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); @@ -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 Binary files differindex 9aba562..68a71cf 100644 --- a/marcussheet.png +++ b/marcussheet.png |