-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
181 lines (152 loc) · 7.13 KB
/
player.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
####################################
# Driftwood 2D Folkdance Engine #
# player.py #
# Copyright 2017 Michael D. Reiley #
# & Paul Merrill #
####################################
# **********
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# **********
# Folkdance Player Manager
import ast
import json
class PlayerManager:
"""Player Manager"""
def __init__(self, entity=None):
if entity is not None:
Driftwood.entity.player = entity
self.__diagonal_movement = False
def set(self, entity):
"""Set a new entity as the player.
Returns the previously set player entity.
"""
oldentity = Driftwood.entity.player
Driftwood.entity.player = entity
return oldentity
def setup_default_keybinds(self, diagonal=False):
if Driftwood.entity.player is None:
return False
if not diagonal:
Driftwood.input.register("up", self._four_way_keybind_move_up)
Driftwood.input.register("down", self._four_way_keybind_move_down)
Driftwood.input.register("left", self._four_way_keybind_move_left)
Driftwood.input.register("right", self._four_way_keybind_move_right)
Driftwood.input.register("interact", self._default_keybind_interact)
Driftwood.input.register("face", self._default_keybind_face)
else:
Driftwood.entity.player._move_keys_active = [0, 0, 0, 0]
Driftwood.input.register("up", self._eight_way_keybind_move_up)
Driftwood.input.register("down", self._eight_way_keybind_move_down)
Driftwood.input.register("left", self._eight_way_keybind_move_left)
Driftwood.input.register("right", self._eight_way_keybind_move_right)
Driftwood.input.register("interact", self._default_keybind_interact)
Driftwood.input.register("face", self._default_keybind_face)
return True
def _four_way_keybind_move_up(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._walk_stop()
player.walk(0, -1, dont_stop=True, stance="walk_up", end_stance="face_up")
elif keyevent == Driftwood.input.ONREPEAT:
player.walk(0, -1, dont_stop=True, stance="walk_up", end_stance="face_up")
elif keyevent == Driftwood.input.ONUP:
player._walk_stop()
def _four_way_keybind_move_down(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._walk_stop()
player.walk(0, 1, dont_stop=True, stance="walk_down", end_stance="face_down")
elif keyevent == Driftwood.input.ONREPEAT:
player.walk(0, 1, dont_stop=True, stance="walk_down", end_stance="face_down")
elif keyevent == Driftwood.input.ONUP:
player._walk_stop()
def _four_way_keybind_move_left(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._walk_stop()
player.walk(-1, 0, dont_stop=True, stance="walk_left", end_stance="face_left")
if keyevent == Driftwood.input.ONREPEAT:
player.walk(-1, 0, dont_stop=True, stance="walk_left", end_stance="face_left")
elif keyevent == Driftwood.input.ONUP:
player._walk_stop()
def _four_way_keybind_move_right(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._walk_stop()
player.walk(1, 0, dont_stop=True, stance="walk_right", end_stance="face_right")
if keyevent == Driftwood.input.ONREPEAT:
player.walk(1, 0, dont_stop=True, stance="walk_right", end_stance="face_right")
elif keyevent == Driftwood.input.ONUP:
player._walk_stop()
def _default_keybind_interact(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
if not player.interact():
player.interact("under")
def _default_keybind_face(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._face_key_active = True
elif keyevent == Driftwood.input.ONUP:
player._face_key_active = False
def _eight_way_keybind_move_up(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._move_keys_active[0] = 1
elif keyevent == Driftwood.input.ONUP:
player._move_keys_active[0] = 0
self._eight_way_update()
def _eight_way_keybind_move_down(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._move_keys_active[1] = 1
elif keyevent == Driftwood.input.ONUP:
player._move_keys_active[1] = 0
self._eight_way_update()
def _eight_way_keybind_move_left(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._move_keys_active[2] = 1
elif keyevent == Driftwood.input.ONUP:
player._move_keys_active[2] = 0
self._eight_way_update()
def _eight_way_keybind_move_right(self, keyevent):
player = Driftwood.entity.player
if keyevent == Driftwood.input.ONDOWN:
player._move_keys_active[3] = 1
elif keyevent == Driftwood.input.ONUP:
player._move_keys_active[3] = 0
self._eight_way_update()
def _eight_way_update(self):
eight_way_stance = [
["up_left", "up", "up_right"],
["left", "none", "right"],
["down_left", "down", "down_right"],
]
player = Driftwood.entity.player
up, down, left, right = player._move_keys_active
y = down - up
x = right - left
base = eight_way_stance[y + 1][x + 1]
stance = "walk_" + base
end_stance = "face_" + base
if x == 0 and y == 0:
player._walk_stop()
else:
player.walk(x, y, dont_stop=True, facing=None, stance=stance, end_stance=end_stance)