This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.go
106 lines (86 loc) · 1.77 KB
/
projectile.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
package main
import (
"image/color"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
)
var (
Projectiles []*projectile
projIMD = imdraw.New(nil)
)
func updateProjectiles(dt float64) {
for i, p := range Projectiles {
if p != nil {
del := p.update(dt)
if del {
copy(Projectiles[i:], Projectiles[i+1:])
Projectiles[len(Projectiles)-1] = nil
Projectiles = Projectiles[:len(Projectiles)-1]
}
}
}
}
func drawProjectiles(target pixel.Target) {
projIMD.Clear()
for _, p := range Projectiles {
if p != nil {
p.draw(projIMD)
}
}
projIMD.Draw(target)
}
func NewProjectile(pos, dir pixel.Vec, speed, dam, diameter float64, colour color.RGBA, colide bool) {
p := projectile{
pos: pos,
dir: dir.Unit(),
speed: speed,
dam: dam,
diameter: diameter,
colour: colour,
canCollide: colide,
}
Projectiles = append(Projectiles, &p)
}
type projectile struct {
pos pixel.Vec
dir pixel.Vec
speed float64
dam float64
diameter float64
colour color.RGBA
canCollide bool
}
// return if to remove
func (p *projectile) update(dt float64) bool {
if !winBounds.Contains(cam.Project(p.pos)) {
// not on screen
return true
}
if Player.collisionBox().Contains(p.pos) {
Player.hurt(p.dam)
return true
}
if p.canCollide {
if pointCollides(p.pos) {
// hit an obsticle
return true
}
}
// Hit enemies
for _, e := range Enemies {
if e == nil {
continue
}
if pixel.C(p.pos, p.diameter/2).IntersectRect(e.collisionBox()) != pixel.ZV {
e.hurt(p.dam)
return true
}
}
p.pos = p.pos.Add(p.dir.Scaled(p.speed * dt))
return false
}
func (p *projectile) draw(imd *imdraw.IMDraw) {
imd.Color = p.colour
imd.Push(p.pos)
imd.Circle(p.diameter/2, 0)
}