-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbullet.h
57 lines (44 loc) · 1.22 KB
/
bullet.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
#pragma once
#include "vector2.h"
#include <graphics.h>
#include <functional>
#include "player_id.h"
#include "camera.h"
class Bullet
{
public:
Bullet()=default;
~Bullet()=default;
void set_callback(std::function<void()> callback) {
//设置回调函数
this->callback = callback;
}
virtual void on_collide() {
//碰撞执行回调
if (callback)callback();
}
virtual bool check_collision(const Vector2<float>& pos, const Vector2<float>& size) {
//子弹中心点与人物矩形碰撞检测
return this->pos.x + this->size.x / 2 >= pos.x
&& this->pos.x + this->size.x / 2 <= pos.x + size.x
&& this->pos.y + this->size.y / 2 >= pos.y
&& this->pos.y + this->size.y <= pos.y + size.y;
}
virtual void on_update(int delta) {
}
virtual void on_draw(const Camera& camera) const{
}
virtual bool check_exceed_screen() {
//检测子弹超出窗口
return (pos.x + size.x <= 0 || pos.x >= getwidth()) || (pos.y >= getheight() || pos.y + size.y <= 0);
}
public:
PlayerID target_id;//子弹要碰撞的目标id
Vector2<float> size;//尺寸
Vector2<float> pos;//子弹位置
Vector2<float> velcity;//子弹速度
int damage;//子弹威力
bool valid = true;//子弹有效
bool can_remove = false;//可以移除
std::function<void()> callback;//子弹碰撞后回调函数
};