From 43394c8a8908442982e3a7e25975c31b3c952923 Mon Sep 17 00:00:00 2001 From: Nikolas Date: Sun, 27 Oct 2024 12:52:55 +0200 Subject: root --- src/player.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/player.cpp (limited to 'src/player.cpp') 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 + +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(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 -- cgit v1.2.3