forked from codelympics/go_game_jam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
foe.go
77 lines (67 loc) · 1.57 KB
/
foe.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
package main
import (
tl "github.com/JoelOtter/termloop"
"math/rand"
)
type Foe struct {
boardX int
boardY int
game *Game
entity *tl.Entity
speed int
frame int
}
func NewFoe(game *Game) *Foe {
foe := Foe{
game: game,
entity: tl.NewEntity(1, 1, 1, 1),
}
foe.Init()
foe.entity.SetCell(0, 0, &tl.Cell{Bg: tl.ColorMagenta, Ch: foeChar})
return &foe
}
func (foe *Foe) Draw(screen *tl.Screen) {
foe.speed = int(foe.game.game.Screen().TimeDelta() * float64(30000000/foe.game.level))
if foe.frame > foe.speed {
foe.boardX, foe.boardY = foe.newPosition(foe.game.player.boardX, foe.game.player.boardY, foe.boardX, foe.boardY)
foe.entity.SetPosition(foe.getPosition())
if foe.game.isCaptured() {
foe.game.Kill()
}
foe.frame = 0
}
foe.frame++
foe.entity.Draw(screen)
}
func (foe *Foe) Tick(event tl.Event) {
}
func (foe *Foe) newPosition(playerX, playerY, x, y int) (int, int) {
move := rand.Intn(3) - 1
if move != 0 {
if rand.Intn(2) > 0 {
newX := x + move
if newX >= 0 && newX < boardWidth {
return newX, y
}
return x - move, y
} else {
newY := y + move
if newY >= 0 && newY < boardHeight {
return x, newY
}
return x, y - move
}
}
return x, y
}
func (foe *Foe) getPosition() (int, int) {
x := foe.boardX*(squareWidth+borderWidth) + offsetX + borderWidth + squareWidth - 1
y := foe.boardY*(squareHeight+borderHeight) + offsetY + borderHeight + squareHeight - 1
return x, y
}
func (foe *Foe) Init() {
foe.boardX = boardWidth - 1
foe.boardY = boardHeight - 1
foe.frame = 0
foe.entity.SetPosition(foe.getPosition())
}