-
Notifications
You must be signed in to change notification settings - Fork 2
/
actor.c
119 lines (87 loc) · 2.35 KB
/
actor.c
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
#include "config.h"
#include "actor.h"
#include "world.h"
#include "utils.h"
#include <math.h>
public void actorDrawBBox(Actor* actor)
{
Vec4f edges[12][2] = {
{{-1,-1,-1},{-1,1,-1}},
{{-1,1,-1},{1,1,-1}},
{{1,1,-1},{1,-1,-1}},
{{1,-1,-1},{-1,-1,-1}},
{{-1,-1,1},{-1,1,1}},
{{-1,1,1},{1,1,1}},
{{1,1,1},{1,-1,1}},
{{1,-1,1},{-1,-1,1}},
{{-1,-1,-1},{-1,-1,1}},
{{-1,1,-1},{-1,1,1}},
{{1,-1,-1},{1,-1,1}},
{{1,1,-1},{1,1,1}},
};
glDisable(GL_DEPTH_TEST);
glBegin(GL_LINES);
for(int i=0;i<12;i++)
{
glColor3f(1,1,1);
glVertexf(actor->pos+actor->size*edges[i][0]);
glColor3f(1,1,1);
glVertexf(actor->pos+actor->size*edges[i][1]);
}
glEnd();
}
private bool actorCollision(World* world, const Actor* actor)
{
for(int z=floor(actor->pos[2]-actor->size[2]+0.01);z<=floor(actor->pos[2]+actor->size[2]-0.01);z++)
for(int y=floor(actor->pos[1]-actor->size[1]+0.01);y<=floor(actor->pos[1]+actor->size[1]-0.01);y++)
for(int x=floor(actor->pos[0]-actor->size[0]+0.01);x<=floor(actor->pos[0]+actor->size[0]-0.01);x++)
{
Block block=worldGet(world,(Vec4i){x,y,z});
if(block_definition[block.id].solid)
return true;
}
return false;
}
public void actorTick(World* world, Actor* actor, float timeDelta)
{
for(int d=0;d<3;d++)
{
float grid=actor->size[d];
//float pos0=actor->pos[d];
float pos1=actor->pos[d]+actor->v[d]*timeDelta;
float s=sign(actor->v[d]);
while(actor->pos[d]*s<pos1*s)
{
float old_pos=actor->pos[d];
if(actor->v[d]>0)
{
actor->pos[d]=min(floor(actor->pos[d]+grid+1)-grid,pos1);
}
else
{
actor->pos[d]=max(ceil(actor->pos[d]-grid-1)+grid,pos1);
}
if(actorCollision(world,actor))
{
actor->v[d]=0.0;
actor->pos[d]=old_pos;
break;
}
}
}
actor->v[2]-=0.6;
}
public Pickup* pickupNew()
{
return calloc(sizeof(Pickup),1);
}
public Vehicle* vehicleNew()
{
return calloc(sizeof(Vehicle),1);
}
public Mob* mobNew()
{
Mob* mob=calloc(sizeof(Mob),1);
mob->draw=true;
return mob;
}