This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
/
door.go
126 lines (109 loc) · 2.39 KB
/
door.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 material
import (
"github.com/BenLubar/Rnoadm/world"
"sync"
)
type Door struct {
world.VisibleObject
open bool
mtx sync.Mutex
}
func init() {
world.Register("door", world.Visible((*Door)(nil)))
world.RegisterSpawnFunc(func(s string) world.Visible {
if s == "door" {
return &Door{}
}
return nil
})
}
func (d *Door) Name() string {
return "door"
}
func (d *Door) Sprite() string {
return "door"
}
func (d *Door) Colors() []string {
return []string{"#888", "#888"}
}
func (d *Door) SpritePos() (uint, uint) {
d.mtx.Lock()
defer d.mtx.Unlock()
if d.open {
return 1, 0
}
return 0, 0
}
func (d *Door) SpriteSize() (uint, uint) {
return 48, 64
}
func (d *Door) Blocking() bool {
d.mtx.Lock()
defer d.mtx.Unlock()
return !d.open
}
func (d *Door) Actions(player world.PlayerLike) []string {
actions := d.VisibleObject.Actions(player)
d.mtx.Lock()
defer d.mtx.Unlock()
if d.Position() != nil {
if d.open {
actions = append([]string{"close"}, actions...)
} else {
actions = append([]string{"open"}, actions...)
}
}
return actions
}
func (d *Door) Interact(player world.PlayerLike, action string) {
switch action {
case "open":
pos := d.Position()
if pos == nil {
return
}
x, y := pos.Position()
px, py := player.Position().Position()
if (px == x && py != y-1 && py != y+1) || (py == y && px != x-1 && px != x+1) || (px != x && py != y) {
player.SetSchedule(&world.ScheduleSchedule{
Schedules: []world.Schedule{
world.NewWalkSchedule(x, y, true, uint(player.Weight()/player.WeightMax())),
&world.ActionSchedule{
Action: "open",
Target_: d.Outer().(world.Visible),
},
},
})
return
}
d.mtx.Lock()
d.open = true
d.mtx.Unlock()
pos.Zone().Update(pos, d)
case "close":
pos := d.Position()
if pos == nil {
return
}
x, y := pos.Position()
px, py := player.Position().Position()
if (px == x && (py < y-1 || py > y+1)) || (py == y && (px < x-1 || px > x+1)) || (px != x && py != y) {
player.SetSchedule(&world.ScheduleSchedule{
Schedules: []world.Schedule{
world.NewWalkSchedule(x, y, true, uint(player.Weight()/player.WeightMax())),
&world.ActionSchedule{
Action: "close",
Target_: d.Outer().(world.Visible),
},
},
})
return
}
d.mtx.Lock()
d.open = false
d.mtx.Unlock()
pos.Zone().Update(pos, d)
default:
d.VisibleObject.Interact(player, action)
}
}