-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Level prototype | Move player and jump
- Loading branch information
1 parent
4de374c
commit a5dbf71
Showing
21 changed files
with
346 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "player.h" | ||
#include "constants.h" | ||
#include "game_states.h" | ||
#include "game.h" | ||
|
||
static int maxYVel = 20; | ||
static int maxXVel = 20; | ||
|
||
Player::Player(Renderer* ren) : GameItem(ren) | ||
{ | ||
destRect = {WINDOW_WIDTH / 2 - 32, WINDOW_HEIGHT - 64 * 6, 64, 64}; | ||
delta = destRect; | ||
yVel = 0; | ||
canJump = true; | ||
jumping = false; | ||
} | ||
|
||
Player::~Player() | ||
{ | ||
|
||
} | ||
|
||
void Player::update() | ||
{ | ||
if (GameStates::getState() == GameState::LEVEL) | ||
{ | ||
delta = destRect; | ||
|
||
if (Game::inputs.up) | ||
{ | ||
if (canJump) | ||
{ | ||
jumping = true; | ||
canJump = false; | ||
} | ||
} | ||
else | ||
jumping = false; | ||
|
||
if (Game::inputs.left || Game::inputs.right) | ||
{ | ||
if (Game::inputs.left) | ||
{ | ||
if (-xVel < maxXVel) | ||
xVel -= 0.5; | ||
} | ||
else | ||
{ | ||
if (xVel < maxXVel) | ||
xVel += 0.5; | ||
} | ||
} | ||
else | ||
xVel /= 2; | ||
|
||
if (!jumping) | ||
{ | ||
if (yVel < maxYVel) | ||
yVel += 0.5; | ||
} | ||
else | ||
{ | ||
if (-yVel < maxYVel / 2) | ||
yVel -= 0.5; | ||
else | ||
jumping = false; | ||
} | ||
|
||
destRect.y += yVel; | ||
destRect.x += xVel; | ||
} | ||
} | ||
|
||
void Player::draw() | ||
{ | ||
renderer->fillRect(&destRect); | ||
} | ||
|
||
SDL_Rect Player::getPos() | ||
{ | ||
return destRect; | ||
} | ||
|
||
SDL_Rect Player::getDelta() | ||
{ | ||
return delta; | ||
} | ||
|
||
void Player::setPos(int x, int y) | ||
{ | ||
destRect.x = x; | ||
destRect.y = y; | ||
yVel = 0; | ||
canJump = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef PLAYER | ||
#define PLAYER | ||
|
||
#include "game_item.h" | ||
#include "renderer.h" | ||
#include <SDL2/SDL.h> | ||
|
||
class Player : public GameItem | ||
{ | ||
public: | ||
Player(Renderer*); | ||
~Player(); | ||
void update(); | ||
void draw(); | ||
SDL_Rect getPos(); | ||
SDL_Rect getDelta(); | ||
void setPos(int x, int y); | ||
private: | ||
SDL_Rect delta; | ||
float yVel, xVel; | ||
bool jumping, canJump; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "player_manager.h" | ||
#include "player.h" | ||
#include "game_states.h" | ||
|
||
PlayerManager::PlayerManager(Renderer* ren) : ItemManager(ren) | ||
{ | ||
Player* p = new Player(renderer); | ||
objects.push_back(p); | ||
playerActive = false; | ||
} | ||
|
||
PlayerManager::~PlayerManager() | ||
{ | ||
|
||
} | ||
|
||
void PlayerManager::update() | ||
{ | ||
if (GameStates::getFirstTick()) | ||
{ | ||
switch (GameStates::getState()) | ||
{ | ||
case GameState::LEVEL: | ||
{ | ||
playerActive = true; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
ItemManager::update(); | ||
} | ||
|
||
void PlayerManager::draw() | ||
{ | ||
if (playerActive == true) | ||
ItemManager::draw(); | ||
} | ||
|
||
Player* PlayerManager::getPlayer() | ||
{ | ||
return (Player*) objects[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef PLAYER_MANAGER | ||
#define PLAYER_MANAGER | ||
|
||
#include "item_manager.h" | ||
#include "renderer.h" | ||
#include "player.h" | ||
|
||
class PlayerManager : public ItemManager | ||
{ | ||
public: | ||
PlayerManager(Renderer*); | ||
~PlayerManager(); | ||
Player* getPlayer(); | ||
void update(); | ||
void draw(); | ||
private: | ||
bool playerActive; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "trampoline.h" | ||
|
||
int Trampoline::height = 32; | ||
|
||
Trampoline::Trampoline(Renderer* ren, Player* p, int x, int y, int w) : GameItem(ren) | ||
{ | ||
myPos = {x, y, w, height}; | ||
destRect = myPos; | ||
player = p; | ||
} | ||
|
||
Trampoline::~Trampoline() | ||
{ | ||
|
||
} | ||
|
||
void Trampoline::update() | ||
{ | ||
if (player->getDelta().y + player->getDelta().h <= myPos.y && player->getPos().y + player->getPos().h > myPos.y) | ||
{ | ||
if ((player->getPos().x > myPos.x && player->getPos().x < myPos.x + myPos.w) || | ||
(player->getPos().x + player->getPos().w > myPos.x && player->getPos().x + player->getPos().w < myPos.x + myPos.w)) | ||
{ | ||
player->setPos(player->getPos().x, myPos.y - player->getPos().h); | ||
} | ||
} | ||
} | ||
|
||
void Trampoline::draw() | ||
{ | ||
renderer->fillRect(&destRect); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef TRAMPOLINE | ||
#define TRAMPOLINE | ||
|
||
#include "game_item.h" | ||
#include "renderer.h" | ||
#include "player.h" | ||
#include <SDL2/SDL.h> | ||
|
||
class Trampoline : public GameItem | ||
{ | ||
public: | ||
Trampoline(Renderer*, Player*, int x, int y, int w); | ||
~Trampoline(); | ||
void update(); | ||
void draw(); | ||
private: | ||
static int height; | ||
SDL_Rect myPos; | ||
Player* player; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "trampoline_manager.h" | ||
#include "game_states.h" | ||
#include "constants.h" | ||
#include "trampoline.h" | ||
|
||
TrampolineManager::TrampolineManager(Renderer* ren, Player* p) : ItemManager(ren) | ||
{ | ||
player = p; | ||
} | ||
|
||
TrampolineManager::~TrampolineManager() | ||
{ | ||
|
||
} | ||
|
||
void TrampolineManager::update() | ||
{ | ||
if (GameStates::getFirstTick()) | ||
{ | ||
switch (GameStates::getState()) | ||
{ | ||
case GameState::LEVEL: | ||
{ | ||
Trampoline* t = new Trampoline(renderer, player, 0, WINDOW_HEIGHT - 48, WINDOW_WIDTH); | ||
objects.push_back(t); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
ItemManager::update(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef TRAMPOLINE_MANAGER | ||
#define TRAMPOLINE_MANAGER | ||
|
||
#include "item_manager.h" | ||
#include "renderer.h" | ||
#include "player.h" | ||
|
||
class TrampolineManager : public ItemManager | ||
{ | ||
public: | ||
TrampolineManager(Renderer*, Player*); | ||
~TrampolineManager(); | ||
void update(); | ||
private: | ||
Player* player; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.