diff options
author | Nikolas <nikolas@boutalas.com> | 2024-10-27 12:52:55 +0200 |
---|---|---|
committer | Nikolas <nikolas@boutalas.com> | 2024-10-27 12:52:55 +0200 |
commit | 43394c8a8908442982e3a7e25975c31b3c952923 (patch) | |
tree | 2facd563e29f48fe3b0653ac5c113998940b4d5e /src/player.cpp |
Diffstat (limited to 'src/player.cpp')
-rw-r--r-- | src/player.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..1d79ccb --- /dev/null +++ b/src/player.cpp @@ -0,0 +1,80 @@ +#include "config.h" +#include "graphics.h" +#include "player.h" +#include "movinggameobject.h" +#include "game.h" +#include "shot.h" + +#include <string> + +using namespace std; + +Player::Player(float pos_x, float pos_y, float size_x, float size_y, float orientation, string texture, float speed) : MovingGameObject(pos_x, pos_y, size_x, size_y, orientation, texture, speed){ + + last_time_shot = graphics::getGlobalTime(); + +} + +Player::~Player(){ + +} + +void Player::update(){ + + MovingGameObject::update(); + + float pos_y = this -> getPosY(); + float pos_x = this -> getPosX(); + float speed = this -> getSpeed(); + + float temp_pos; + + if (graphics::getKeyState(graphics::SCANCODE_W) || graphics::getKeyState(graphics::SCANCODE_UP)){ + temp_pos = pos_y - speed * graphics::getDeltaTime(); + if(temp_pos > WINDOW_HEIGHT/2 + 50) pos_y = temp_pos; + } + if (graphics::getKeyState(graphics::SCANCODE_S) || graphics::getKeyState(graphics::SCANCODE_DOWN)){ + temp_pos = pos_y + speed * graphics::getDeltaTime(); + if(temp_pos < WINDOW_HEIGHT - 50) pos_y = temp_pos; + } + if (graphics::getKeyState(graphics::SCANCODE_A) || graphics::getKeyState(graphics::SCANCODE_LEFT)){ + temp_pos = pos_x - speed * graphics::getDeltaTime(); + if(temp_pos > 50) pos_x = temp_pos; + } + if (graphics::getKeyState(graphics::SCANCODE_D) || graphics::getKeyState(graphics::SCANCODE_RIGHT)){ + temp_pos = pos_x + speed * graphics::getDeltaTime(); + if(temp_pos < WINDOW_WIDTH - 50) pos_x = temp_pos; + } + + this -> setPosX(pos_x); + this -> setPosY(pos_y); + + if(graphics::getGlobalTime() > last_time_shot + 500){ + + if (graphics::getKeyState(graphics::SCANCODE_SPACE)){ + + Game * game = reinterpret_cast<Game *>(graphics::getUserData()); + game -> addObject(new Shot(pos_x + 3 , pos_y - 60, 100, 100, this -> getOrientation(), "shot", 1)); + last_time_shot = graphics::getGlobalTime(); + } + } +} + +void Player::draw(){ + + MovingGameObject::draw(); + + float pos_y = this -> getPosY(); + float pos_x = this -> getPosX(); + + graphics::Brush br; + br.outline_opacity = 0.0f; + int exhaust_number = (int)(graphics::getGlobalTime()/100) % 4 + 1; + br.texture = string(ASSET_PATH) + "exhaust" + to_string(exhaust_number) + ".png"; + + graphics::setOrientation(this -> getOrientation()); + graphics::drawRect(pos_x-5, pos_y+50, 30, 30, br); + graphics::drawRect(pos_x+8, pos_y+50, 30, 30, br); + graphics::resetPose(); + +}
\ No newline at end of file |