-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
113 lines (93 loc) · 2.1 KB
/
player.cpp
File metadata and controls
113 lines (93 loc) · 2.1 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2014 Ross Simpson
* All Right Reserved
*
* This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
* To view a copy of this license,visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
*/
#include "player.h"
#include "gamestate.h"
#include <SDL2/SDL_image.h>
#include <string>
using namespace std;
Player::Player()
{
width = 90;
height = 120;
// Start at the bottom middle of the screen
uint32_t xpos = GameState::get()->getWindowWidth()/2 - width/2;
uint32_t ypos = GameState::get()->getWindowHeight() - height*1.5;
position.setX(xpos);
position.setY(ypos);
}
Player::~Player()
{
if (playerTexture != nullptr)
SDL_DestroyTexture(playerTexture);
}
bool Player::init()
{
// TODO: Implement texture manager
const string playerTextureFile = "/home/rsimpson/Development/EndlessQuest/images/destroyer.png";
playerTexture = IMG_LoadTexture(GameState::get()->getRenderer(), playerTextureFile.c_str());
if (playerTexture == nullptr)
{
logSDLError(cout, "Player::init(), IMG_LoadTexture");
return false;
}
weapon = new StandardLaserWeapon();
weapon->init();
return true;
}
void Player::render(SDL_Renderer *renderer)
{
SDL_Rect dest;
dest.x = position.getX();
dest.y = position.getY();
dest.w = width;
dest.h = height;
SDL_RenderCopy(renderer, playerTexture, NULL, &dest);
weapon->render(renderer);
}
void Player::handleInput()
{
const uint8_t* keyState = SDL_GetKeyboardState(NULL);
float accelX = acceleration.getX();
float accelY = acceleration.getY();
if (keyState[SDL_SCANCODE_RIGHT])
{
accelX = accelX == 0 ? 0.5 : 0;
}
else if (keyState[SDL_SCANCODE_LEFT])
{
accelX = accelX == 0 ? -0.5 : 0;
}
else
{
accelX = 0;
}
if(keyState[SDL_SCANCODE_UP])
{
accelY = accelY == 0 ? -0.5 : 0;
}
else if (keyState[SDL_SCANCODE_DOWN])
{
accelY = accelY == 0 ? 0.5 : 0;
}
else
{
accelY = 0;
}
acceleration.setX(accelX);
acceleration.setY(accelY);
if (keyState[SDL_SCANCODE_SPACE])
{
weapon->fire(position);
}
}
void Player::update()
{
handleInput();
Object::update();
weapon->update();
}