-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcontrols.go
126 lines (108 loc) · 2.77 KB
/
controls.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
package gorched
import (
tl "github.com/JoelOtter/termloop"
)
// Controls holds data and logic for controlling game world.
type Controls struct {
// reference to game
game *Game
}
// Tick handles all key events
func (c *Controls) Tick(e tl.Event) {
// TODO: show some message box after resize about restart round is needed to be applied
// on resize update game options to be applied on round restart or on next round
if e.Type == tl.EventResize {
w, h := c.game.engine.Screen().Size()
c.game.options.Width = w
c.game.options.Height = h
}
// when message box is shown it is in control
if c.game.Hud().IsFormShown() {
return
}
// otherwise handle in-game controls
switch e.Key {
case tl.KeyArrowLeft:
c.MoveUp()
case tl.KeyArrowRight:
c.MoveDown()
case tl.KeySpace:
c.Shoot()
case tl.KeyCtrlR:
c.RestartRound()
case tl.KeyCtrlN:
c.NextRound()
}
switch e.Ch {
case 'h':
c.ShowInfo()
case 's':
c.ShowScore()
case 'a':
c.ShowAttributes()
}
// for the browser mode we cannot use ctrl+n and ctr+r as we would leave the window
if c.game.options.BrowserMode {
switch e.Ch {
case 'n':
c.NextRound()
case 'r':
c.RestartRound()
}
}
}
// Draw does nothing now
func (c *Controls) Draw(s *tl.Screen) {}
// Following methods can be also used from outside as a support for other external controller
// MoveUp increase cannon's angle of active tank
func (c *Controls) MoveUp() {
if c.game.round.IsPlayerOnTurn() {
c.game.round.ActiveTank().MoveUp()
}
}
// MoveDown decreases cannon's angle of active tank
func (c *Controls) MoveDown() {
if c.game.round.IsPlayerOnTurn() {
c.game.round.ActiveTank().MoveDown()
}
}
// Shoot will start loading or shoot with active tank if it's already loading
func (c *Controls) Shoot() {
if c.game.round.IsPlayerOnTurn() {
c.game.round.ActiveTank().Shoot()
}
}
// HideMessageBox will hide any active message box
func (c *Controls) HideMessageBox() {
c.game.Hud().HideForm()
}
// NextRound will switch game to the next round
func (c *Controls) NextRound() {
c.game.round.Next()
}
// RestartRound will restart current round
func (c *Controls) RestartRound() {
c.game.round.Restart()
}
// ShowInfo shows main game information
func (c *Controls) ShowInfo() {
c.game.Hud().ShowInfo()
}
// ShowScore shows actual score board
func (c *Controls) ShowScore() {
c.game.Hud().ShowScore()
}
// ShowAttributes shows attributes dialog
func (c *Controls) ShowAttributes() {
c.game.hud.ShowAttributes(true)
}
// MoveFocus moves focus to next component on currently opened form.
// If no form is opened ignore it.
func (c *Controls) MoveFocus() {
c.game.Hud().MoveFocus()
}
// PressButton sends action event to currently opened form.
// If no form is opened ignore it.
func (c *Controls) PressButton() {
c.game.Hud().PressButton()
}