-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobject.h
207 lines (174 loc) · 4.55 KB
/
object.h
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#pragma once
#include "constant.h"
#include "window.h"
#include "platform.h"
//对象基类
class Object
{
public:
Vector2 size = Vector2(50, 50); //大小
Vector2 velocity = Vector2(0, 0); //速度
Vector2 position = Vector2(0, 0); //位置
SDL_Rect rect = { 0 }; //图形显示矩形
bool is_player = 0;
public:
enum class State {
IDLE,
WALK,
};
enum class Axis {
X, Y, NONE
};
Object() = default;
~Object()=default;
//初始化
virtual void on_create() {
}
//更新
virtual void on_update() {
}
//退出
virtual void on_exit(){
}
//释放
virtual void on_destroy(){
delete this;
}
void Move(float speed, float delta)
{
//加速度
if (speed == 0) {
velocity = Vector2(0, 0);
}
else {
if (velocity.x < speed) {
velocity.x += ACCELERATION * delta;
}
if (velocity.y < speed) {
velocity.y += ACCELERATION * delta;
}
}
//面部朝向
if (speed) {
face_direction.x = direction.x;
face_direction.y = direction.y;
if (direction.x && direction.y) {
if (direction.x > 0) {
angle = 90 + direction.y * 45;
}
else {
angle = -90 - direction.y * 45;
}
}
else if (direction.x) {
angle = direction.x * 90;
}
else if (direction.y) {
angle = 90 + direction.y * 90;
}
}
//原先位置
int temp_x = position.x, temp_y = position.y;
//移动计算
if (direction.x && direction.y) {
position.x += direction.x * velocity.x * 0.707;
position.y += direction.y * velocity.y * 0.707;
}
else if (direction.x) {
position.x += velocity.x * direction.x;
}
else if (direction.y) {
position.y += velocity.y * direction.y;
}
//墙体碰撞
if (position.x + size.x > platform->get_position().x + platform->get_size().x ||
position.x < platform->get_position().x) {
position.x = temp_x;
}
if (position.y + size.y > platform->get_position().y + platform->get_size().y
|| position.y < platform->get_position().y) {
position.y = temp_y;
}
}
//设置y移动方向
void SetDirectionY(float dir)
{
direction.y = dir;
}
//设置x移动方向
void SetDirectionX(float dir)
{
direction.x = dir;
}
//获取面朝方向
Vector2 get_direction() {
return face_direction;
}
//获取位置
void set_position(int x,int y) {
position.x = x, position.y = y;
}
//获取位置
Vector2 get_position() {
return position;
}
//获取大小
Vector2 get_size() {
return size;
}
//设置状态
void SetCurrentState(State state) {
transition_state(current_state, state);
current_state = state;
state_time = 0.0f;
}
//状态机循环
void PhysicsProcess(float delta) {
while (true) {
State next = get_next_state(current_state);
if (current_state == next) {
break;
}
current_state = next;
SetCurrentState(current_state);
}
tick_physics(current_state, delta);
state_time += delta;
}
//设置所在平台
void set_platform(Platform* plat) {
platform = plat;
}
//获取所在平台
Platform* get_platform(){
return platform;
}
//获取边界
int border_up() const{
return position.y;
}
int border_down() const{
return position.y + size.y;
}
int border_left() const{
return position.x;
}
int border_right() const {
return position.x + size.x;
}
protected:
Window* window = nullptr;
Vector2 face_direction = Vector2(0, 0); //玩家面朝方向
Vector2 direction = Vector2(0, 0); //玩家移动方向
float angle = 0; //玩家图像面朝角度
Platform* platform=nullptr;
//状态机
State current_state=State::IDLE;
float state_time=0;
//状态的物理逻辑
virtual void tick_physics(State state, float delta) { return; }
// 获取下一个状态的逻辑
virtual State get_next_state(State current){return current;}
// 状态转换逻辑
virtual void transition_state(State current, State next) { return; }
};