-
Notifications
You must be signed in to change notification settings - Fork 0
/
shot.go
76 lines (68 loc) · 1.28 KB
/
shot.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
package main
import (
"time"
"github.com/nsf/termbox-go"
)
//Shot fired by a ship
type Shot struct {
positionX int
positionY int
direction int
stopCh chan bool
ship *Ship
fg termbox.Attribute
bg termbox.Attribute
}
func (shot *Shot) run() {
shot.draw()
for {
select {
case <-time.After(250000 * time.Microsecond):
shot.clear()
shot.positionY += shot.direction
shot.draw()
shot.detectCollision()
case <-shot.stopCh:
shot.clear()
return
}
}
}
func (shot *Shot) draw() {
if shot.positionY < 0 || shot.positionY > shot.ship.size.height {
shot.stopCh <- true
}
termbox.SetCell(
shot.positionX,
shot.positionY,
'|',
shot.fg,
shot.bg,
)
}
func (shot *Shot) detectCollision() {
// Avoid checking if the shot has reached the top corner
if shot.positionY >= 0 {
ship := shot.ship.matrix[shot.positionX][shot.positionY]
// Check if there is a ship (which did not fire the shot)
if ship != nil && ship != shot.ship {
// Check for player controlled ship
if ship.control {
cleanExit()
} else {
ship.stopCh <- true
shot.clear()
shot.stopCh <- true
}
}
}
}
func (shot *Shot) clear() {
termbox.SetCell(
shot.positionX,
shot.positionY,
' ',
termbox.ColorDefault,
termbox.ColorDefault,
)
}