Skip to content

Commit

Permalink
Level prototype | Move player and jump
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterMjir committed Feb 7, 2020
1 parent 4de374c commit a5dbf71
Show file tree
Hide file tree
Showing 21 changed files with 346 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ add_executable(tsa
src/game/manager/manager.cpp
src/game/object/game_item.cpp

src/level/player/player_manager.cpp
src/level/player/player.cpp
src/level/trampoline/trampoline_manager.cpp
src/level/trampoline/trampoline.cpp

src/menu/menu_manager.cpp
src/menu/level_select/level_select_manager.cpp
src/menu/level_select/level_tile.cpp
Expand All @@ -35,6 +40,8 @@ target_include_directories(tsa PUBLIC
src/game
src/game/manager
src/game/object
src/level/player
src/level/trampoline
src/menu
src/menu/level_select
src/sdl
Expand Down
Binary file modified bin/tsa
Binary file not shown.
Binary file added res/images/levels/grassy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Window* Game::window;
Renderer* Game::renderer;
Texture* Game::texture;
ManagerManager* Game::manager;
int Game::selectedLevel = 0;
Camera Game::camera = {0, 0};

void Game::init(const char* title, int x, int y, int width, int height, bool fullScreen)
{
Expand Down
8 changes: 8 additions & 0 deletions src/game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include "manager_manager.h"
#include "game_input.h"

struct Camera
{
int x;
int y;
};

class Game
{
public:
Expand All @@ -20,6 +26,8 @@ class Game
static bool isRunning();
static Texture* getTexture();
static GameInput inputs;
static int selectedLevel;
static Camera camera;
private:
Game();
~Game();
Expand Down
6 changes: 6 additions & 0 deletions src/game/manager/manager_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "slide_manager.h"
#include "menu_manager.h"
#include "component_manager.h"
#include "trampoline_manager.h"
#include "player_manager.h"

ManagerManager::ManagerManager(Renderer* renderer)
{
Expand All @@ -16,6 +18,10 @@ ManagerManager::ManagerManager(Renderer* renderer)
objects.push_back(menuM);
ComponentManager* componentM = new ComponentManager(renderer);
objects.push_back(componentM);
PlayerManager* playerM = new PlayerManager(renderer);
objects.push_back(playerM);
TrampolineManager* trampolineM = new TrampolineManager(renderer, playerM->getPlayer());
objects.push_back(trampolineM);
}

ManagerManager::~ManagerManager()
Expand Down
95 changes: 95 additions & 0 deletions src/level/player/player.cpp
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;
}
24 changes: 24 additions & 0 deletions src/level/player/player.h
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
43 changes: 43 additions & 0 deletions src/level/player/player_manager.cpp
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];
}
20 changes: 20 additions & 0 deletions src/level/player/player_manager.h
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
32 changes: 32 additions & 0 deletions src/level/trampoline/trampoline.cpp
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);
}
22 changes: 22 additions & 0 deletions src/level/trampoline/trampoline.h
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
32 changes: 32 additions & 0 deletions src/level/trampoline/trampoline_manager.cpp
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();
}
18 changes: 18 additions & 0 deletions src/level/trampoline/trampoline_manager.h
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
7 changes: 4 additions & 3 deletions src/menu/level_select/level_select_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

LevelSelectManager::LevelSelectManager(Renderer* ren) : ItemManager(ren)
{
selectedIndex = 0;

}

LevelSelectManager::~LevelSelectManager()
Expand Down Expand Up @@ -48,12 +48,13 @@ void LevelSelectManager::update()
{
if (Game::inputs.attack)
{
selectedIndex = i;
Game::selectedLevel = i;
switchSelected((LevelTile*) objects[i]);
}
}

dynamic_cast<LevelTile*>(objects[i])->setXY((WINDOW_WIDTH / 2 - 128) + (i * 492) - (selectedIndex * 492), 64);
int arb = 512;
dynamic_cast<LevelTile*>(objects[i])->setXY((WINDOW_WIDTH / 2 - 192) + (i * arb) - (Game::selectedLevel * arb), WINDOW_HEIGHT / 2 - 192);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/menu/level_select/level_select_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class LevelSelectManager : public ItemManager
~LevelSelectManager();
void update();
void switchSelected(LevelTile*);
private:
int selectedIndex;
};

#endif
Loading

0 comments on commit a5dbf71

Please sign in to comment.