-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.go
70 lines (54 loc) · 1.68 KB
/
enemy.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
package main
import (
"fmt"
"go-game/phy"
)
type Enemy struct {
Character
anim *SeqAnimation
rb *phy.RigidBody
collider *phy.Collider
LastSafePosition *phy.Vector
}
func NewEnemy(props *Properties) (*Enemy, error) {
collider := &phy.Collider{}
collider.SetBuffer(-5, -10, 0, 0)
// texId is seqId for enemy
anim, err := NewSeqAnimation(true, "assets/seqAnims.xml", props.texId)
if err != nil {
return nil, fmt.Errorf("failed to load seq animation, %v", err)
}
return &Enemy{
Character: *NewCharacter(props),
anim: anim,
rb: phy.NewRigidBody(props.transform),
collider: collider,
LastSafePosition: &phy.Vector{},
}, nil
}
func (e *Enemy) Draw() {
pos := e.transform
e.anim.Draw(int(pos.X), int(pos.Y), 1, 1, e.flip)
}
func (e *Enemy) Update(dt uint64) {
e.rb.UnsetForces()
e.rb.Update(dt)
disp := e.rb.GetDisplacement()
e.LastSafePosition.Set(phy.Vector{X: e.GetTransform().X, Y: e.LastSafePosition.Y})
e.transform.TranslateX(disp.X)
e.collider.Set(int32(e.transform.X), int32(e.transform.Y), 2*TILE_SIZE, 2*TILE_SIZE)
if CollisionHandlerInstance.GetInstance().MapCollision(e.collider.Get()) {
e.transform.Set(phy.Vector{X: e.LastSafePosition.X, Y: e.transform.Y})
}
e.LastSafePosition.Set(phy.Vector{X: e.LastSafePosition.Y, Y: e.GetTransform().Y})
e.transform.TranslateY(disp.Y)
e.collider.Set(int32(e.transform.X), int32(e.transform.Y), TILE_SIZE, TILE_SIZE)
if CollisionHandlerInstance.GetInstance().MapCollision(e.collider.Get()) {
e.transform.Set(phy.Vector{X: e.transform.X, Y: e.LastSafePosition.Y})
}
e.anim.Update(dt)
if e.anim.IsEnded() {
e.anim.SetCurrentSeq("player_idle")
e.anim.SetRepeat(true)
}
}