-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrub.wl
109 lines (93 loc) · 2.64 KB
/
grub.wl
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
import "entity.wl"
import "libwl/gl.wl"
import "libwl/mesh.wl"
import "libwl/image.wl"
import "libwl/fmt/tga.wl"
import "libwl/fmt/mdl.wl"
import "libwl/vec.wl"
import "libwl/file.wl"
import "libwl/random.wl"
import "libwl/collision.wl"
import "drawDevice.wl"
import "man.wl"
use "importc"
import(C) "math.h"
class Grub : Entity {
static const int STATE_ROTATE = 0
static const int STATE_MOVE = 1
static GLMesh mesh
static GLTexture texture
float scale
float timer
int state
int spin
bool dead
bool isDead() return .dead
this() {
.timer = 5
.scale = 0.5 + randomFloat()
.spin = 1
if(!mesh) {
Mesh m = loadMdl(new StringFile(pack "res/grub.mdl"))
.mesh = new GLMesh(m)
}
if(!texture) {
Image i = loadTGA(new StringFile(pack "res/grub.tga"))
.texture = new GLTexture(i)
}
.rotation = randomFloat() * 6 // 6 = 2PI (close enough)
.position.v[0] = randomFloat() * 20.0f - 10.0f
.position.v[2] = randomFloat() * 20.0f - 10.0f
}
float nummies() return 0.06f
void update(float dt) {
.timer -= dt
if(.timer <= 0) {
.state = !.state //swap tween rotate/move
if(randomFloat() > 0.4) .spin = -.spin
.timer = randomFloat() * 3
}
DuckMan d = DuckMan.getInstance()
Box3 dhit = d.getHitbox()
if(dhit.collides(.getHitbox())) {
if(d.scale * 2.2 > .scale * 0.40) {
d.eat(this)
.dead = true
} else {
d.dead = true
}
}
if(.state == 0) {
.rotation += 0.333 * .spin
} else if(.state == 1) {
vec4 dv = vec4(0, 0, -0.2 * sqrt(.scale), 0)
mat4 matrix = mat4()
matrix = matrix.rotate(.rotation, vec4(0, 1, 0, 0))
dv = matrix.vmul(dv)
if(.position.v[0] < -9.0f || .position.v[0] > 9.0f ||
.position.v[2] < -9.0f || .position.v[2] > 9.0f) {
if(dv.dot(.position) > 0) {
.state = 0
.timer /= 2.0f
return
}
}
.position = .position.add(dv)
} else {
.state = 0
}
}
Box3 getHitbox() {
vec4 dim = vec4(0.45, 0.2, 0.45, 0)
dim.mul(.scale)
return Box3(.position, dim)
}
float getScale() return .scale
GLMesh getMesh() return .mesh
GLTexture getTexture() return .texture
}
void initGrubs() {
for(int i = 0; i < 5; i++) {
(Entity.add(new Grub()))
}
}