-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player2D.cpp
94 lines (75 loc) · 2.01 KB
/
Player2D.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
#include "stdafx.h"
#include "Keyboard.h"
#include "Player2D.h"
#include "Scroller.h"
Player2D::Player2D ()
{
}
Player2D::Player2D (int player_width, int player_height)
{
x = 150;
y = 150;
width = player_width;
height = player_height;
move_speed = 2;
}
Player2D::~Player2D ()
{
}
// per frame
void Player2D::Get_Input ()
{
keyboard.get_state ();
}
// per step
void Player2D::Update (Scroller &scroll)
{
handle_keyboard (scroll);
position_rect ();
}
void Player2D::position_rect ()
{
render_rect.left = x - width / 2;
render_rect.right = x + width / 2;
render_rect.top = y - height;
render_rect.bottom = y;
}
void Player2D::handle_keyboard (Scroller &scroll)
{
if (keyboard.left) move_left (scroll);
if (keyboard.right) move_right (scroll);
if (keyboard.up) move_up (scroll);
if (keyboard.down) move_down (scroll);
}
void Player2D::move_left (Scroller &scroll)
{
x -= move_speed;
// update scroll
if (scroll.x + x < scroll.margin && scroll.x < scroll.max_x) scroll.x += move_speed;
// collision detection
if (scroll.x + x - (width / 2) < 0) x = width / 2;
}
void Player2D::move_right (Scroller &scroll)
{
x += move_speed;
// update scroll
if (scroll.x + x > scroll.screen_width - scroll.margin && scroll.x > scroll.min_x) scroll.x -= move_speed;
// collision detection
if (scroll.x + x + (width / 2) > scroll.screen_width) x = scroll.screen_width - (width / 2) - scroll.x;
}
void Player2D::move_up (Scroller &scroll)
{
y -= move_speed;
// update scroll
if (scroll.y + y - (height / 2) < scroll.margin && scroll.y < scroll.max_y) scroll.y += move_speed;
// collision detection
if (scroll.y + y - height < 0) y = height;
}
void Player2D::move_down (Scroller &scroll)
{
y += move_speed;
// update scroll
if (scroll.y + y - (height / 2) > scroll.screen_height - scroll.margin && scroll.y > scroll.min_y) scroll.y -= move_speed;
// collision detection
if (scroll.y + y > scroll.screen_height) y = scroll.screen_height - scroll.y;
}