Skip to content

Commit

Permalink
Dash ability added
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterMjir committed Feb 10, 2020
1 parent e2a9f2c commit edc5e72
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 48 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ target_include_directories(tsa PUBLIC
src/game
src/game/manager
src/game/object
src/level/enemies
src/level/player
src/level/trampoline
src/menu
Expand Down
Binary file modified bin/tsa
Binary file not shown.
18 changes: 9 additions & 9 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void Game::init(const char* title, int x, int y, int width, int height, bool ful
{
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 Expand Up @@ -55,33 +55,33 @@ void Game::input()
case SDL_KEYDOWN:
switch (e.key.keysym.sym)
{
case SDLK_LEFT:
case SDLK_a:
inputs.left = true;
break;
case SDLK_RIGHT:
case SDLK_d:
inputs.right = true;
break;
case SDLK_UP:
case SDLK_w:
inputs.up = true;
break;
case SDLK_DOWN:
case SDLK_s:
inputs.down = true;
break;
}
break;
case SDL_KEYUP:
switch (e.key.keysym.sym)
{
case SDLK_LEFT:
case SDLK_a:
inputs.left = false;
break;
case SDLK_RIGHT:
case SDLK_d:
inputs.right = false;
break;
case SDLK_UP:
case SDLK_w:
inputs.up = false;
break;
case SDLK_DOWN:
case SDLK_s:
inputs.down = false;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/level/enemies/enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef ENEMY
#define ENEMY

#endif
145 changes: 106 additions & 39 deletions src/level/player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@
#include "game_states.h"
#include "game.h"
#include <SDL2/SDL.h>
#include <deque>

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;
for (int i = 0; i < 10; i++)
deltas.push_back(delta);

canJump = false;
jumping = false;
shouldJump = false;

boosting = false;
canBoost = true;
boostTicks = 0;

ticks = 0;
trampTicks = 0;
airTicks = 0;

yVel = 0;
xVel = 0;
defMinMaxYVel = 5;
defMaxMaxYVel = 40;
maxYVel = defMinMaxYVel;
Expand All @@ -30,12 +40,15 @@ Player::~Player()

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

if (GameStates::getState() == GameState::LEVEL)
{
ticks++;

delta = destRect;
deltas.pop_back();
deltas.push_front(delta);

// Check if can jump
if (Game::inputs.up)
{
if (maxYVel < defMaxMaxYVel && canJump && ticks % 2 == 0)
Expand All @@ -61,72 +74,124 @@ void Player::update()
shouldJump = false;
}

if (Game::inputs.left || Game::inputs.right)
// Check if can boost
if (Game::inputs.attack && canBoost)
{
boosting = true;
if (Game::inputs.left)
boostDir = 2;
else if (Game::inputs.right)
boostDir = 3;
else
boosting = false;
canBoost = false;
}

if (boosting)
{
boostTicks++;
if (boostTicks > 8)
{
if (-xVel < maxXVel)
xVel -= 0.5;
boosting = false;
yVel = -10;
xVel = 0;
}
else
switch (boostDir)
{
if (xVel < maxXVel)
xVel += 0.5 ;
case 2:
destRect.x -= boostTicks * 2;
break;
case 3:
destRect.x += boostTicks * 2;
}
}
else
xVel /= 2;

if (!jumping)
else // Not Boosting
{
yVel += 0.5;
if (Game::inputs.up)
if (Game::inputs.left || Game::inputs.right)
{
if (yVel < 0)
yVel += 1;
if (Game::inputs.left)
{
if (-xVel < maxXVel)
xVel -= 0.5;
}
else
yVel *= 1.1;
{
if (xVel < maxXVel)
xVel += 0.5 ;
}
}
}
else
{
if (-yVel < maxYVel)
else
xVel /= 2;

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

destRect.y += yVel;
destRect.x += xVel;
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)
if (yVel > 0)
{
Game::camera.y = (destRect.y + destRect.h / 2) - WINDOW_HEIGHT / 4;
if (Game::camera.y < Game::levelInfo.maxHeight)
Game::levelInfo.maxHeight = Game::camera.y;
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;

if (destRect.x < 0)
destRect.x = WINDOW_WIDTH - destRect.w;

if (destRect.x > WINDOW_WIDTH)
destRect.x = 0;
}

void Player::draw()
{
if (boosting)
{
int i = 0;
for (auto pos : deltas)
{

SDL_Rect dRect = {pos.x, (pos.y + maxYVel / 5) - Game::camera.y, pos.w, pos.h};
renderer->setAlpha(Game::getTexture()->getTexture(), 128);
renderer->copy(Game::getTexture()->getTexture(), &srcRect, &dRect);
renderer->setAlpha(Game::getTexture()->getTexture(), 255);
}
}

if (trampTicks % (-maxYVel / 5 + 20) == 0)
srcRect = {64, 32, 64, 64};
Expand Down Expand Up @@ -159,6 +224,8 @@ void Player::setPos(int x, int y)
yVel = 0;
canJump = true;
airTicks = 0;
canBoost = true;
boostTicks = 0;
}

int Player::getMaxYVel()
Expand Down
4 changes: 4 additions & 0 deletions src/level/player/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "game_item.h"
#include "renderer.h"
#include <SDL2/SDL.h>
#include <deque> // Probably better options but no time

class Player : public GameItem
{
Expand All @@ -18,11 +19,14 @@ class Player : public GameItem
int getMaxYVel();
private:
SDL_Rect delta;
std::deque<SDL_Rect> deltas;
float yVel, xVel;
bool jumping, canJump, shouldJump;
int defMinMaxYVel, defMaxMaxYVel;
int maxYVel, maxXVel;
int ticks, trampTicks, airTicks;
bool boosting, canBoost;
int boostTicks, boostDir;
};

#endif
5 changes: 5 additions & 0 deletions src/sdl/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ SDL_Renderer* Renderer::getRenderer()
{
return renderer;
}

void Renderer::setAlpha(SDL_Texture* texture, Uint8 a)
{
SDL_SetTextureAlphaMod(texture, a);
}
1 change: 1 addition & 0 deletions src/sdl/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Renderer
void clear();
void fillRect(SDL_Rect* destRect);
void copy(SDL_Texture* texture, SDL_Rect* srcRect, SDL_Rect* destRect);
void setAlpha(SDL_Texture*, Uint8);
SDL_Renderer* getRenderer();
private:
SDL_Renderer* renderer;
Expand Down

0 comments on commit edc5e72

Please sign in to comment.