-
Notifications
You must be signed in to change notification settings - Fork 0
/
ship.go
224 lines (200 loc) · 4.57 KB
/
ship.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"math/rand"
"time"
"github.com/nsf/termbox-go"
)
const (
left int = -1
down int = 0
right int = 1
)
// Ship that protects the planet
type Ship struct {
view [][]rune
positionX int
positionY int
stopCh chan bool
moveCh chan int
fleet *Fleet
size *sizes
matrix [][]*Ship
control bool
fg termbox.Attribute
bg termbox.Attribute
}
// Create the player controller ship
func initShip(size sizes, matrix [][]*Ship) Ship {
return Ship{
view: [][]rune{{'|'}, {'<', '=', '>'}},
positionX: size.width / 2,
positionY: size.height,
stopCh: make(chan bool),
moveCh: make(chan int),
size: &size,
matrix: matrix,
control: true,
fg: termbox.ColorWhite,
bg: termbox.ColorDefault,
}
}
// Create a group of not moving ships which act as a barrier
func initBarrier(size sizes, matrix [][]*Ship) {
positions := []int{0, 1, 1, 1, 0}
for i := 3; i < size.width-4; i = i + 7 {
for count, value := range positions {
for j := 0; j < 2; j++ {
ship := Ship{
view: [][]rune{{' '}},
positionX: i + count,
positionY: size.height - 4 - value + j,
stopCh: make(chan bool),
moveCh: make(chan int),
size: &size,
matrix: matrix,
fg: termbox.ColorDefault,
bg: termbox.ColorGreen,
}
go ship.run()
}
}
}
}
func (ship *Ship) run() {
ship.draw()
for {
select {
case <-ship.stopCh:
// Stop the ship and remove it from the fleet if it is affiliated
if ship.fleet != nil {
delete(ship.fleet.ships, ship)
}
ship.clear()
return
case dir := <-ship.moveCh:
switch dir {
case left:
if ship.positionX > 1 {
ship.move(left)
}
if ship.fleet != nil && ship.positionX == 1 {
ship.fleet.dirCh <- down
}
case down:
if ship.positionY < ship.size.height {
ship.move(down)
}
if ship.fleet != nil {
if ship.positionX == 1 {
ship.fleet.dirCh <- right
}
if ship.positionX == ship.size.width-2 {
ship.fleet.dirCh <- left
}
// Stop if fleet reaches defense ship
if ship.positionY == ship.size.height-1 {
ship.fleet.stopCh <- true
cleanExit()
}
}
case right:
if ship.positionX < ship.size.width-2 {
ship.move(right)
}
if ship.fleet != nil && ship.positionX == ship.size.width-2 {
ship.fleet.dirCh <- down
}
}
case <-time.After(1000000 / 30 * time.Microsecond):
ship.draw()
}
}
}
// Fire a shot from a ship - directed upwards or downwards depending on
// fleet affiliation
func (ship *Ship) fire() {
var dir, offset int
var color termbox.Attribute
// Avoid creating the shot _in_ the ship
if ship.fleet != nil {
dir = 1
offset = 0
color = termbox.ColorRed
} else {
dir = -1
offset = -3
color = termbox.ColorCyan
}
shot := Shot{
positionX: ship.positionX,
positionY: ship.positionY + offset,
direction: dir,
stopCh: make(chan bool),
ship: ship,
fg: color,
bg: termbox.ColorDefault,
}
go shot.run()
}
func (ship *Ship) fireFromFleet() {
r := rand.Intn(100)
if r < 5 && ship.positionY+6 < ship.size.height {
matrix := ship.matrix
ship_3 := matrix[ship.positionX][ship.positionY+2]
ship_6 := matrix[ship.positionX][ship.positionY+5]
if !((ship_3 != nil && ship_3.fleet != nil) ||
(ship_6 != nil && ship_6.fleet != nil)) {
ship.fire()
}
}
}
// Draw the ship and set the reference in the collision matrix
func (ship *Ship) draw() {
rows := len(ship.view)
matrix := ship.matrix
for r, row := range ship.view {
width := len(row) / 2
for c, ch := range row {
x := ship.positionX - width + c
y := ship.positionY - rows + r
if matrix[x][y] != nil && matrix[x][y] != ship {
matrix[x][y].stopCh <- true
}
matrix[x][y] = ship
termbox.SetCell(x, y, ch, ship.fg, ship.bg)
}
}
}
// Make sure all view fields of the ship are cleared as well as
// the collision matrix
func (ship *Ship) clear() {
rows := len(ship.view)
matrix := ship.matrix
for r, row := range ship.view {
width := len(row) / 2
for c := range row {
x := ship.positionX - width + c
y := ship.positionY - rows + r
matrix[x][y] = nil
termbox.SetCell(x, y, ' ', termbox.ColorDefault, termbox.ColorDefault)
}
}
}
func (ship *Ship) moveLeft() {
ship.moveCh <- -1
}
func (ship *Ship) moveRight() {
ship.moveCh <- 1
}
func (ship *Ship) moveDown() {
ship.moveCh <- 0
}
func (ship *Ship) move(direction int) {
ship.clear()
if direction == down {
ship.positionY++
} else {
ship.positionX += direction
}
ship.draw()
}