forked from codelympics/go_game_jam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.go
102 lines (93 loc) · 1.98 KB
/
player.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
package main
import (
tl "github.com/JoelOtter/termloop"
)
type PlayerState int
const (
stateAlive PlayerState = iota
stateDead
)
type Player struct {
lives int
score int
boardX int
boardY int
entity *tl.Entity
game *Game
state PlayerState
}
func NewPlayer(game *Game) *Player {
player := Player{
game: game,
entity: tl.NewEntity(1, 1, 1, 1),
}
player.Init()
player.entity.SetCell(0, 0, &tl.Cell{Bg: tl.ColorRed, Ch: playerChar})
return &player
}
func (player *Player) Draw(screen *tl.Screen) {
player.entity.Draw(screen)
}
func (player *Player) Tick(event tl.Event) {
if event.Type == tl.EventKey {
if player.state == stateDead {
player.game.restartGame()
return
}
switch event.Key {
case tl.KeyArrowRight:
if player.boardX < boardWidth-1 {
player.boardX += 1
}
break
case tl.KeyArrowLeft:
if player.boardX > 0 {
player.boardX -= 1
}
break
case tl.KeyArrowUp:
if player.boardY > 0 {
player.boardY -= 1
}
break
case tl.KeyArrowDown:
if player.boardY < boardHeight-1 {
player.boardY += 1
}
break
case tl.KeySpace:
if (*player.game.board)[player.boardY][player.boardX].Hit() {
player.score++
} else {
player.game.Kill()
}
player.game.updateStatus()
if player.game.board.isLevelComplete() {
player.score += player.lives
player.lives++
player.boardX = 0
player.boardY = 0
player.entity.SetPosition(player.getPosition())
player.game.nextLevel()
}
break
}
if player.game.isCaptured() {
player.game.Kill()
}
player.entity.SetPosition(player.getPosition())
}
}
func (player *Player) Init() {
player.lives = playerLives
player.score = 0
player.boardX = 0
player.boardY = 0
player.state = stateAlive
player.entity.SetPosition(player.getPosition())
}
func (player *Player) getPosition() (int, int) {
x := player.boardX*(squareWidth+borderWidth) + offsetX + borderWidth
y := player.boardY*(squareHeight+borderHeight) + offsetY + borderHeight
return x, y
}