Skip to content

Commit

Permalink
Added music | Added player sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterMjir committed Feb 8, 2020
1 parent a5dbf71 commit e2a9f2c
Show file tree
Hide file tree
Showing 21 changed files with 250 additions and 38 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_executable(tsa
src/menu/level_select/level_select_manager.cpp
src/menu/level_select/level_tile.cpp

src/sdl/mixer.cpp
src/sdl/renderer.cpp
src/sdl/texture.cpp
src/sdl/window.cpp
Expand Down Expand Up @@ -51,5 +52,5 @@ target_include_directories(tsa PUBLIC
src/ui/slide
)

target_link_libraries(tsa SDL2 SDL2_image)
target_link_libraries(tsa SDL2 SDL2_image SDL2_mixer)
set_target_properties(tsa PROPERTIES RUNTIME_OUTPUT_DIRECTORY bin)
Binary file modified bin/tsa
Binary file not shown.
Binary file added res/music/title.wav
Binary file not shown.
Binary file added res/music/tsa_boss_1_3.wav
Binary file not shown.
Binary file modified res/spritesheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#include "game.h"
#include "game_states.h"
#include <SDL2/SDL_mixer.h>

GameInput Game::inputs = {false, false, false, false, false, false, 0, 0};
bool Game::running = true;
Window* Game::window;
Renderer* Game::renderer;
Texture* Game::texture;
ManagerManager* Game::manager;
int Game::selectedLevel = 0;
Camera Game::camera = {0, 0};
LevelInfo Game::levelInfo = {0, 0};

void Game::init(const char* title, int x, int y, int width, int height, bool fullScreen)
{
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 2048);


window = new Window(title, x, y, width, height, fullScreen);
renderer = new Renderer(window->getWindow());
texture = new Texture("res/spritesheet.png", renderer);
Expand Down
7 changes: 7 additions & 0 deletions src/game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ struct Camera
int y;
};

struct LevelInfo
{
int level;
int maxHeight;
};

class Game
{
public:
Expand All @@ -28,6 +34,7 @@ class Game
static GameInput inputs;
static int selectedLevel;
static Camera camera;
static LevelInfo levelInfo;
private:
Game();
~Game();
Expand Down
36 changes: 27 additions & 9 deletions src/game/manager/manager_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,43 @@
#include "trampoline_manager.h"
#include "player_manager.h"

/*
* It updates in the order here,
* but draws in the reverse order
*/

ManagerManager::ManagerManager(Renderer* renderer)
{
objects.reserve(4);
objects.reserve(6);

BackgroundManager* backgroundM = new BackgroundManager(renderer);
objects.push_back(backgroundM);
SlideManager* slideM = new SlideManager(renderer);
objects.push_back(slideM);
MenuManager* menuM = new MenuManager(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);

ComponentManager* componentM = new ComponentManager(renderer);
objects.push_back(componentM);

MenuManager* menuM = new MenuManager(renderer);
objects.push_back(menuM);

SlideManager* slideM = new SlideManager(renderer);
objects.push_back(slideM);

BackgroundManager* backgroundM = new BackgroundManager(renderer);
objects.push_back(backgroundM);
}

ManagerManager::~ManagerManager()
{

}

void ManagerManager::draw()
{
for (int i = objects.size() - 1; i >= 0; i--)
{
objects[i]->draw();
}
}
1 change: 1 addition & 0 deletions src/game/manager/manager_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ManagerManager : public Manager
public:
ManagerManager(Renderer*);
~ManagerManager();
void draw();
};

#endif
98 changes: 85 additions & 13 deletions src/level/player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
#include "constants.h"
#include "game_states.h"
#include "game.h"

static int maxYVel = 20;
static int maxXVel = 20;
#include <SDL2/SDL.h>

Player::Player(Renderer* ren) : GameItem(ren)
{
destRect = {WINDOW_WIDTH / 2 - 32, WINDOW_HEIGHT - 64 * 6, 64, 64};
srcRect = {0, 32, 64, 64};
delta = destRect;
yVel = 0;
canJump = true;
canJump = false;
jumping = false;
shouldJump = false;
ticks = 0;
trampTicks = 0;
airTicks = 0;

defMinMaxYVel = 5;
defMaxMaxYVel = 40;
maxYVel = defMinMaxYVel;
maxXVel = 10;
}

Player::~Player()
Expand All @@ -22,20 +30,36 @@ Player::~Player()

void Player::update()
{
ticks++;

if (GameStates::getState() == GameState::LEVEL)
{
delta = destRect;

if (Game::inputs.up)
{
if (maxYVel < defMaxMaxYVel && canJump && ticks % 2 == 0)
{
maxYVel += 1;
}

if (canJump)
{
trampTicks++;
}

shouldJump = true;
}
else
{
trampTicks = 1;
if (shouldJump && canJump)
{
jumping = true;
canJump = false;
}
shouldJump = false;
}
else
jumping = false;

if (Game::inputs.left || Game::inputs.right)
{
Expand All @@ -47,33 +71,69 @@ void Player::update()
else
{
if (xVel < maxXVel)
xVel += 0.5;
xVel += 0.5 ;
}
}
else
xVel /= 2;

if (!jumping)
{
if (yVel < maxYVel)
yVel += 0.5;
yVel += 0.5;
if (Game::inputs.up)
{
if (yVel < 0)
yVel += 1;
else
yVel *= 1.1;
}
}
else
{
if (-yVel < maxYVel / 2)
yVel -= 0.5;
if (-yVel < maxYVel)
{
yVel = -maxYVel - yVel / 2;
}
else
{
maxYVel = defMinMaxYVel;
jumping = false;
}
}

destRect.y += yVel;
destRect.x += xVel;

if (yVel > 0)
{
if (destRect.y + destRect.h / 2 >= Game::camera.y + WINDOW_HEIGHT - WINDOW_HEIGHT / 4)
Game::camera.y = (destRect.y + destRect.h / 2) - WINDOW_HEIGHT + WINDOW_HEIGHT / 4;
}
else
{
if (destRect.y + destRect.h / 2 <= Game::camera.y + WINDOW_HEIGHT / 4)
{
Game::camera.y = (destRect.y + destRect.h / 2) - WINDOW_HEIGHT / 4;
if (Game::camera.y < Game::levelInfo.maxHeight)
Game::levelInfo.maxHeight = Game::camera.y;
}
}
}

airTicks++;
if (airTicks > 2)
canJump = false;
}

void Player::draw()
{
renderer->fillRect(&destRect);

if (trampTicks % (-maxYVel / 5 + 20) == 0)
srcRect = {64, 32, 64, 64};
else
srcRect = {0, 32, 64, 64};
SDL_Rect dRect = {destRect.x, (destRect.y + maxYVel / 5) - Game::camera.y, destRect.w, destRect.h};
renderer->copy(Game::getTexture()->getTexture(), &srcRect, &dRect);
}

SDL_Rect Player::getPos()
Expand All @@ -90,6 +150,18 @@ void Player::setPos(int x, int y)
{
destRect.x = x;
destRect.y = y;
yVel = 0;
if (Game::inputs.up)
yVel = 0;
else
if (yVel > 5)
yVel = -yVel / 2;
else
yVel = 0;
canJump = true;
airTicks = 0;
}

int Player::getMaxYVel()
{
return maxYVel;
}
6 changes: 5 additions & 1 deletion src/level/player/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ class Player : public GameItem
SDL_Rect getPos();
SDL_Rect getDelta();
void setPos(int x, int y);
int getMaxYVel();
private:
SDL_Rect delta;
float yVel, xVel;
bool jumping, canJump;
bool jumping, canJump, shouldJump;
int defMinMaxYVel, defMaxMaxYVel;
int maxYVel, maxXVel;
int ticks, trampTicks, airTicks;
};

#endif
24 changes: 23 additions & 1 deletion src/level/trampoline/trampoline.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "trampoline.h"
#include "game.h"
#include <SDL2/SDL.h>

int Trampoline::height = 32;

Expand All @@ -7,6 +9,7 @@ Trampoline::Trampoline(Renderer* ren, Player* p, int x, int y, int w) : GameItem
myPos = {x, y, w, height};
destRect = myPos;
player = p;
touchingPlayer = false;
}

Trampoline::~Trampoline()
Expand All @@ -16,17 +19,36 @@ Trampoline::~Trampoline()

void Trampoline::update()
{
if (touchingPlayer)
{
ticks++;
if (ticks > 1)
{
touchingPlayer = false;
ticks = 0;
}
}

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);
touchingPlayer = true;
}
}
}

void Trampoline::draw()
{
renderer->fillRect(&destRect);
if (touchingPlayer)
destRect.y = myPos.y + player->getMaxYVel() / 5;
else
{
destRect.y += (myPos.y - destRect.y) / 4;
}
SDL_Rect dRect = destRect;
dRect.y = dRect.y - Game::camera.y;
renderer->fillRect(&dRect);
}
2 changes: 2 additions & 0 deletions src/level/trampoline/trampoline.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Trampoline : public GameItem
static int height;
SDL_Rect myPos;
Player* player;
bool touchingPlayer;
int ticks;
};

#endif
Loading

0 comments on commit e2a9f2c

Please sign in to comment.