-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.cpp
136 lines (113 loc) · 2.47 KB
/
character.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "character.h"
#include "test.h"
Character::Character(std::string filename)
:Living_things(0,0, filename)
{
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Character::handle_input()
{
//If a key was pressed
if( global::event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( global::event.key.keysym.sym )
{
case SDLK_UP:
yVel -= m_height / 8;
break;
case SDLK_DOWN:
yVel += m_height / 8;
break;
case SDLK_LEFT:
xVel -= m_width / 6;
break;
case SDLK_RIGHT:
xVel += m_width / 6;
break;
} }
//If a key was released
else if( global::event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( global::event.key.keysym.sym )
{
case SDLK_UP: yVel += m_height / 8; break;
case SDLK_DOWN: yVel -= m_height / 8; break;
case SDLK_LEFT: xVel += m_width / 6; break;
case SDLK_RIGHT: xVel -= m_width / 6; break;
}
}
}
void Character::move()
{
//Move the dot left or right
m_x += xVel;
if(xVel > 0)
{
change_image(RIGHT);
}
if(xVel < 0)
{
change_image(LEFT);
}
// check if not out of bounds
if( m_x < 0)
{
m_x = 0;
}
if( (m_x + m_width) > SCREEN_WIDTH )
{
m_x = SCREEN_WIDTH - m_width;
}
//Move the dot left or right
m_y += yVel;
if(yVel > 0)
{
change_image(DOWN);
}
if(yVel < 0)
{
change_image(UP);
}
// check if not out of bounds
if( m_y < 0)
{
m_y = 0;
}
if( (m_y + m_height) > SCREEN_HEIGHT )
{
m_y = SCREEN_HEIGHT - m_height;
}
}
//FIXME: Dont change after last keypress, but direction of movement
void Character::change_image(Character::Direction direction)
{
switch(direction)
{
case UP:
m_image = load_image("character_up.bmp");
break;
case DOWN:
m_image = load_image("character_down.bmp");
break;
case LEFT:
m_image = load_image("character_left.bmp");
break;
case RIGHT:
m_image = load_image("character_right.bmp");
break;
}
}
void Character::respawn()
{
m_x = 0;
m_y = 0;
m_invul = true;
}
bool Character::get_invul()
{
return m_invul;
}