-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_enemy.go
83 lines (73 loc) · 1.52 KB
/
entity_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
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"math/rand"
"time"
"github.com/gdamore/tcell/v2"
)
var enemyFrames = []rune{
'\u25F4',
'\u25F5',
'\u25F6',
'\u25F7',
}
type Enemy struct {
Class
Position
Velocity
Sprite
Delay
animationDelay Delay
}
// Draw returns the current animation frame but also increments
// the frame for the next draw call
func (e *Enemy) Draw() rune {
if e.animationDelay.Tick() {
e.frame++
if e.frame > len(e.frames)-1 {
e.frame = 0
}
}
return e.frames[e.frame]
}
// Move checks for an entity in what would be our Enemy's occupying
// cell post-movement. If the cell-to-be-occupied has no occupying
// entity, update the Enemy's position based on movement vectors.
func (e *Enemy) Move() {
e.SetPosition(e.x+e.vx, e.y+e.vy)
}
// HasVelocity overrides Velocity.HasVelocity to randomize
// movement direction and speed (time between movement intervals)
func (e *Enemy) HasVelocity() bool {
if !e.Tick() {
return false
}
e.SetRandomVelocity()
return true
}
// NewEnemy returns a new Enemy
func NewEnemy(x, y int) *Enemy {
delay := time.Duration(rand.Intn(501)+250) * time.Millisecond
animationDelay := time.Duration(rand.Intn(250)+250) * time.Millisecond
return &Enemy{
Class: NewEnemyClass(),
Delay: Delay{
ticker: *time.NewTicker(delay),
},
Position: Position{
x: x,
y: y,
},
Velocity: Velocity{
vx: 0,
vy: 0,
},
Sprite: Sprite{
frames: enemyFrames,
frame: 0,
color: tcell.ColorRed,
},
animationDelay: Delay{
ticker: *time.NewTicker(animationDelay),
},
}
}