-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys_projectile.go
64 lines (60 loc) · 1.63 KB
/
sys_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
package kar
import (
"kar/items"
"kar/res"
"math"
)
type Projectile struct {
snowBallBox AABB
bounceVelocity float64
}
func (p *Projectile) Init() {
p.snowBallBox = AABB{Half: Vec{4, 4}}
p.bounceVelocity = -math.Sqrt(2 * SnowballGravity * SnowballBounceHeight)
}
func (p *Projectile) Update() {
// projectile physics
q := filterProjectile.Query()
for q.Next() {
itemID, projectilePos, projectileVel := q.Get()
// snowball physics
if itemID.ID == items.Snowball {
projectileVel.Y += SnowballGravity
projectileVel.Y = min(projectileVel.Y, SnowballMaxFallVelocity)
p.snowBallBox.Pos.X = projectilePos.X
p.snowBallBox.Pos.Y = projectilePos.Y
tileCollider.Collide(p.snowBallBox, *(*Vec)(projectileVel), func(ci []HitTileInfo, delta Vec) {
projectilePos.X += delta.X
projectilePos.Y += delta.Y
isHorizontalCollision := false
for _, cinfo := range ci {
if cinfo.Normal.Y == -1 {
projectileVel.Y = p.bounceVelocity
}
if cinfo.Normal.X == -1 && projectileVel.X > 0 && projectileVel.Y > 0 {
isHorizontalCollision = true
}
if cinfo.Normal.X == 1 && projectileVel.X < 0 && projectileVel.Y > 0 {
isHorizontalCollision = true
}
}
if isHorizontalCollision {
if world.Alive(q.Entity()) {
toRemove = append(toRemove, q.Entity())
}
}
},
)
}
}
}
func (p *Projectile) Draw() {
// Draw snowball
q := filterProjectile.Query()
for q.Next() {
id, pos, _ := q.Get()
colorMDIO.GeoM.Reset()
colorMDIO.GeoM.Translate(pos.X-dropItemAABB.Half.X, pos.Y-dropItemAABB.Half.Y)
cameraRes.DrawWithColorM(res.Icon8[id.ID], colorM, colorMDIO, Screen)
}
}