/
bullet.go
113 lines (87 loc) · 1.96 KB
/
bullet.go
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
package main
import (
"github.com/go-gl/gl/v2.1/gl"
)
var (
bulletMaxHP = 1
bulletMass = 0.5
bulletColSize = 20.0
bulletScale = vertex{x: 10, y: 10, z: 10}
bulletModel polyModel
)
// bullet is a small projectile shot by players or enemies.
type bullet struct {
uidGenerator
loc vertex
moveDir vertex
speed float64
rot float64
alliance alliance
harmful collision
hp int
}
// bulletPosData is positional data for a bullet, used to initialize.
type bulletPosData struct {
loc vertex
rot float64
moveDir vertex
speed float64
}
func newBullet(data bulletPosData, alliance alliance) *bullet {
bul := new(bullet)
bul.hp = bulletMaxHP
bul.loc = data.loc
bul.rot = data.rot
bul.moveDir = data.moveDir
bul.speed = data.speed
bul.alliance = alliance
return bul
}
func (b *bullet) tick() []entity {
b.loc.x += (b.speed * mainWindow.delta) * b.moveDir.x
b.loc.y += (b.speed * mainWindow.delta) * b.moveDir.y
b.updateCols()
return []entity{}
}
func (b *bullet) draw() {
gl.PushMatrix()
rotateOnPoint(b.rot, b.loc)
gl.Translated(b.loc.x, b.loc.y, b.loc.z)
gl.Scaled(bulletScale.x, bulletScale.y, bulletScale.z)
bulletModel.draw()
gl.PopMatrix()
}
func (b *bullet) collision(yours, other collision) {
switch other.typ {
case bouncer:
if other.alliance == unaligned {
// We bumped into a wall or something
b.hp--
}
case vulnerable:
if other.alliance != b.alliance {
// We probably hit an enemy
b.hp--
}
}
}
func (b *bullet) updateCols() {
b.harmful.alliance = b.alliance
b.harmful.typ = harmful
b.harmful.bounding = bounding{
vertex{b.loc.x - bulletColSize/2.0, b.loc.y - bulletColSize/2.0, 0.0},
vertex{b.loc.x + bulletColSize/2.0, b.loc.y + bulletColSize/2.0, 0.0}}
}
func (b *bullet) collisions() []collision {
return []collision{
b.harmful}
}
func (b *bullet) location() vertex {
return b.loc
}
func (b *bullet) deletable() bool {
return b.hp <= 0
}
func (b *bullet) mass() float64 {
return bulletMass
}