summaryrefslogtreecommitdiff
path: root/src/player.cpp
blob: 1d79ccb8762ade64b741d34738b7d730214a56cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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();

}