-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.cpp
More file actions
130 lines (120 loc) · 2.48 KB
/
player.cpp
File metadata and controls
130 lines (120 loc) · 2.48 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "player.hpp"
Player::Player(SDL_Surface* display, Foreground* foreground)
{
this->display = display;
this->foreground = foreground;
px = py = 50*16;
vx = vy = jumping = death_animation = 0;
faceRight = true;
sprite = NULL;
}
Player::~Player()
{
if(sprite != NULL)
SDL_FreeSurface(sprite);
}
bool Player::loadSprite(std::string filename, int width, int height) // TODO autodetect witdh / height
{
this->width = width;
this->height = height;
if(sprite != NULL)
SDL_FreeSurface(sprite);
if((sprite = IMG_Load(filename.c_str())) != NULL)
return true;
else
return false;
}
void Player::draw(int x, int y)
{
//TODO animation
animation_time = (animation_time + vx) % 512;
int animation_phase = animation_time > 0 ? animation_time / 171 : -animation_time / 171;
SDL_Rect src = { death_animation ? 64 : jumping ? 48 : animation_phase * 16, 0, 16, 16 };
SDL_Rect dst = { px/16 - x, py/16 - y, 16, 16 };
SDL_BlitSurface(sprite, &src, display, &dst);
}
void Player::input(Uint8 direction)
{
if(death_animation)
{
vx = 0;
if(death_animation-- == 100)
vy = -32;
vy += 16;
py += vy;
} else {
// moving left and right
if(direction & 0x04)
{
vx -= 16;
faceRight = false;
}
if(direction & 0x08)
{
vx += 16;
faceRight = true;
}
if(!direction)
vx = 0;
if(vx > 64)
vx = 64;
if(vx < -64)
vx = -64;
// test for collision
if(foreground->collision((px+vx)/16, py/16, width, height) > 15)
death_animation = 100;
else
{
while(foreground->collision((px+vx)/16, py/16, width, height))
faceRight ? vx-- : vx++;
px += vx;
if(direction & 0x01 && jumping < 4)
{
jumping++;
vy -= 48;
}
}
}
}
void Player::physics()
{
if(!death_animation)
{
// gravity
vy = vy >= 64 ? 64 : vy + 16;
if(foreground->collision(px/16, (py+vy)/16, width, height) > 15)
death_animation = 100;
else
{
while(foreground->collision(px/16, (py+vy)/16, width, height))
{
if(vy > 0)
{
jumping = 0;
vy--;
} else {
vy = 8;
}
}
py += vy;
}
}
}
int Player::getX()
{
return px / 16;
}
Box Player::getBbox()
{
return Box(px / 16, px / 16 + width, py / 16, py / 16 + height);
}
int Player::hit()
{
if(vy > 0)
return -1; // player hit enemy
else
{
death_animation = 100;
return 0; // enemy hit player
}
}