-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.go
154 lines (135 loc) · 2.61 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"math"
"math/rand"
"github.com/xyproto/vt100"
)
const enemyEraseChar = ' ' // for erasing when moving
type Enemy struct {
color vt100.AttributeColor // foreground color
x, y int // current position
oldx, oldy int // previous position
state rune // looks
}
func NewEnemies(n int) []*Enemy {
enemies := make([]*Enemy, n)
for i := range enemies {
enemies[i] = NewEnemy()
}
return enemies
}
func NewEnemy() *Enemy {
return &Enemy{
x: 10,
y: 10,
oldx: 10,
oldy: 10,
state: '°',
color: vt100.LightMagenta,
}
}
func (b *Enemy) Draw(c *vt100.Canvas) {
c.PlotColor(uint(b.x), uint(b.y), b.color, b.state)
}
func (b *Enemy) Right(c *vt100.Canvas) bool {
oldx := b.x
b.x += 1
if b.x >= int(c.W()) {
b.x -= 1
return false
}
b.oldx = oldx
b.oldy = b.y
return true
}
func (b *Enemy) Left(c *vt100.Canvas) bool {
oldx := b.x
if b.x-1 < 0 {
return false
}
b.x -= 1
b.oldx = oldx
b.oldy = b.y
return true
}
func (b *Enemy) Up(c *vt100.Canvas) bool {
oldy := b.y
if b.y-1 < 0 {
return false
}
b.y -= 1
b.oldx = b.x
b.oldy = oldy
return true
}
func (b *Enemy) Down(c *vt100.Canvas) bool {
oldy := b.y
b.y += 1
if b.y >= int(c.H()) {
b.y -= 1
return false
}
b.oldx = b.x
b.oldy = oldy
return true
}
// Terminal was resized
func (b *Enemy) Resize() {
b.color = vt100.LightCyan
}
// Next moves the object to the next position, and returns true if it moved
func (e *Enemy) Next(c *vt100.Canvas, bob *Bob) bool {
e.oldx = e.x
e.oldy = e.y
// Now try to move the enemy intelligently, given the position of bob
d := distance(bob.x, e.x, bob.y, e.y)
if d > 10 {
if e.x < bob.x {
e.x++
} else if e.x > bob.x {
e.x--
}
if e.y < bob.y {
e.y++
} else if e.y > bob.y {
e.y--
}
} else {
for {
dx := e.x - e.oldx
dy := e.y - e.oldy
e.x += int(math.Round(float64(dx*3+rand.Intn(5)-2) / float64(4))) // -2, -1, 0, 1, 2
e.y += int(math.Round(float64(dy*3+rand.Intn(5)-2) / float64(4)))
if e.x != e.oldx {
break
}
if e.y != e.oldy {
break
}
}
}
if e.HitSomething(c) {
e.x = e.oldx
e.y = e.oldy
return false
}
if e.x >= int(c.W()) {
e.x = e.oldx
} else if e.x <= 0 {
e.x = e.oldx
}
if e.y >= int(c.H()) {
e.y = e.oldy
} else if e.y <= 0 {
e.y = e.oldy
}
return e.x != e.oldx || e.y != e.oldy
}
func (e *Enemy) HitSomething(c *vt100.Canvas) bool {
r, err := c.At(uint(e.x), uint(e.y))
if err != nil {
return false
}
// Hit something?
return r != rune(0) && r != bulletEraseChar && r != bobEraseChar && r != enemyEraseChar
}