-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.go
More file actions
137 lines (109 loc) · 2.69 KB
/
actions.go
File metadata and controls
137 lines (109 loc) · 2.69 KB
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
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"os"
)
type IAction interface {
Perform()
}
// Action is the base struct, intended to be extended by structs that implement the Perform function of IAction
type Action struct {
entity *Entity
}
// engine is a shortcut to Engine
func (action *Action) engine() *Engine {
return action.entity.dungeon.engine
}
func (action *Action) Perform() {
// "Abstract" function to be implemented by sub-actions
}
type EscapeAction struct {
}
func (esc *EscapeAction) Perform() {
os.Exit(0)
}
// ActionWithDirection is a base struct for Actions with destination
type ActionWithDirection struct {
Action
dx int
dy int
}
func (action *ActionWithDirection) DestinationXY() (int, int) {
return action.entity.X + action.dx, action.entity.Y + action.dy
}
// BlockingEntity returns the blocking Entity at this Actions destination
func (action *ActionWithDirection) BlockingEntity() *Entity {
dX, dY := action.DestinationXY()
entityAtDestination := action.engine().gameMap.entities.AtLocation(dX, dY)
if entityAtDestination != nil && entityAtDestination.blocksMovement == true {
return entityAtDestination
}
return nil
}
type MovementAction struct {
ActionWithDirection
}
func (move *MovementAction) Perform() {
dX, dY := move.DestinationXY()
if !move.engine().gameMap.InBounds(dX, dY) {
return // Destination out of bounds
}
if !move.engine().gameMap.GetTile(dX, dY).Walkable {
return // Destination is blocked by a tile
}
if move.BlockingEntity() != nil {
return // Destination blocked by Entity
}
move.entity.Move(move.dx, move.dy)
}
type MeleeAction struct {
ActionWithDirection
}
// Perform this MeleeAction on Entity
func (action *MeleeAction) Perform() {
target := action.BlockingEntity()
if target != nil {
fmt.Println(fmt.Sprintf("You kick the %s, much to its annoyance!", target.name))
}
}
type BumpAction struct {
ActionWithDirection
}
func (action *BumpAction) Perform() {
target := action.BlockingEntity()
if target == nil {
NewMovementAction(action.entity, action.dx, action.dy).Perform()
} else {
NewMeleeAction(action.entity, action.dx, action.dy).Perform()
}
}
func NewMeleeAction(entity *Entity, dx, dy int) *MeleeAction {
return &MeleeAction{
ActionWithDirection{
Action: Action{entity},
dx: dx,
dy: dy,
},
}
}
func NewMovementAction(entity *Entity, dx, dy int) *MovementAction {
return &MovementAction{
ActionWithDirection{
Action: Action{entity},
dx: dx,
dy: dy,
},
}
}
func NewBumpAction(entity *Entity, dx, dy int) *BumpAction {
return &BumpAction{
ActionWithDirection{
Action: Action{entity},
dx: dx,
dy: dy,
},
}
}
func NewEscapeAction() *EscapeAction {
return &EscapeAction{}
}