-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
84 lines (70 loc) · 2 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
package ai
import (
"github.com/dalloriam/rogue/rogue/cartography"
"github.com/dalloriam/rogue/rogue/components"
"github.com/dalloriam/rogue/rogue/object"
)
type inputProviderState int
const (
defaultState inputProviderState = iota
repeatWaitingForAction
repeatMode
)
// InputProvider provides input from the player.
type InputProvider interface {
GetDirection() cartography.Direction
RepeatModeTriggered() bool
AutoExploreTriggered() bool
}
// PlayerController exposes player behavior as an AI agent.
type PlayerController struct {
repeatedAction func(obj object.GameObject)
provider InputProvider
state inputProviderState
}
// NewPlayerController returns a new player controller.
func NewPlayerController(provider InputProvider) *PlayerController {
return &PlayerController{
provider: provider,
}
}
func (pc *PlayerController) getPlayerInputAction(tgtObj object.GameObject) func(obj object.GameObject) {
currentDirection := pc.provider.GetDirection()
if currentDirection != cartography.NoDirection {
return func(obj object.GameObject) {
obj.AddComponents(&components.Movement{Direction: currentDirection})
}
}
if pc.provider.RepeatModeTriggered() {
pc.state = repeatWaitingForAction
return nil
}
if pc.provider.AutoExploreTriggered() {
tgtObj.AddComponents(&components.Control{Agent: NewAutoExplorer(pc)})
return nil
}
return nil
}
// GetAction returns the action performed by this entity.
func (pc *PlayerController) GetAction(obj object.GameObject, worldMap cartography.Map) func() {
playerInputAction := pc.getPlayerInputAction(obj)
switch pc.state {
case defaultState:
if playerInputAction != nil {
return func() { playerInputAction(obj) }
}
case repeatWaitingForAction:
if playerInputAction != nil {
pc.repeatedAction = playerInputAction
pc.state = repeatMode
return func() { playerInputAction(obj) }
}
case repeatMode:
if playerInputAction == nil {
return func() { pc.repeatedAction(obj) }
} else {
pc.state = defaultState
}
}
return nil
}