-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
223 lines (187 loc) · 7.03 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from config import *
import AI
from utils import *
import weapons
import math_utils
import particle_factories
global Done
class Screen(object):
#class to deal with screen-scrolling n shit
def __init__(self):
self.screenLocationX = 0.0
self.screenLocationY = 0.0
self.offsetX = -1.0 * screenTileWidth / 2.0
self.offsetY = -1.0 * screenTileHeight / 2.0
self.map_limit = np.array([9999.0, 9999.0]) # default
def update(self, x, y):
screenLocation = np.array([x + self.offsetX, y + self.offsetY])
screenLocation = np.clip(screenLocation, [0.0, 0.0], self.map_limit)
self.screenLocationX, self.screenLocationY = screenLocation.tolist()
# update global DATA
DATA["screen_location"] = screenLocation
def getLocation(self):
return (self.screenLocationX, self.screenLocationY)
def get_footprint_rect(self):
""" get pygame Rect of the object's footprint. i.e. the portion that can be collided with.
"""
lt = self.getLocation()
wh = [screenTileWidth, screenTileHeight]
x, y = [lt[0], lt[0] + wh[0]], [lt[1], lt[1] + wh[1]]
rect = PatchExt([x, y]) # xxyy_limits' a sequence of two pairs: [[x_low, x_high], [y_low, y_high]]
#rect = pygame.Rect(lt, wh) # Rect(left, top, width, height) -> Rect
return rect
rect = property(get_footprint_rect)
class Keyboard(object):
#specify conversion from keyboard event to player action
def __init__(self):
self.keyboard_convertDict = {pygame.K_a: 'left',
pygame.K_d: 'right',
pygame.K_w: 'up',
pygame.K_s: 'down',
pygame.K_SPACE: 'interact',
pygame.K_ESCAPE: 'exit',
pygame.QUIT: 'exit',
pygame.K_e: 'edit',
pygame.K_RCTRL: 'attack',
pygame.K_1: 'use_item1',
pygame.K_2: 'use_item2',
pygame.K_3: 'use_ability1',
1: 'attack',
2: 'use_item1',
3: 'switch_weapon',
4: 'switch_weapon',
5: 'switch_weapon',
}
# https://stackoverflow.com/questions/34287938/how-to-distinguish-left-click-right-click-mouse-clicks-in-pygame
def get_action(self, key):
if key in self.keyboard_convertDict:
#print(self.keyboard_convertDict[key])
return self.keyboard_convertDict[key]
else:
return ''
def getRealTimeAction(self, status):
actions = []
for key in self.keyboard_convertDict:
if status[key]: actions.append(self.keyboard_convertDict[key])
return actions
class Logitech():
A = 0
LB = 4
RB = 5
class Joystick(Keyboard):
def __init__(self):
pygame.joystick.init()
self.joystick = pygame.joystick.Joystick(0)
self.joystick.init()
self.keyboard_convertDict = {Logitech.A: '',
Logitech.LB: 'attack',
Logitech.RB: 'interact',
}
self.action_to_button = dict([[v,k] for k,v in self.keyboard_convertDict.items()])
def continuous_attack(self):
if self.joystick.get_button(self.action_to_button["attack"]):
return True
def get_direction(self):
""" axis 0 and 1, already normalized from [-1.0, 1.0]
"""
xdir = self.joystick.get_axis(0)
ydir = self.joystick.get_axis(1)
#
dir = np.array([xdir, ydir])
dir[np.abs(dir) < 0.1] = 0.0
return dir
class Player(AI.Basic):
def __init__(self, *args, **kwargs):
#default values
self.gameObject = None
self.screenClass = Screen()
self.keyboard = Keyboard()
try:
self.joystick = Joystick()
self.use_joystick = True
except:
self.joystick = None
self.use_joystick = False
self.weapon_idx = 3
self.cbs_to_call = []
def next_weapon(self):
self.weapon_idx += 1
self.weapon_idx %= len(weapons.weapons_list)
self.gameObject.attacker.change_weapon( weapons.weapons_list[self.weapon_idx] )
def setGameObject(self, object):
self.parent = object
self.gameObject = object
self.gameObject.objectType = 'Player'
# over-ride AI
self.gameObject.AI = self
def update(self, events):
#convert keyboard+mouse events into actions
dx = 0; dy = 0
actions = []
for event in events:
if event.type == pygame.KEYDOWN:
action = self.keyboard.get_action(event.key)
elif event.type == pygame.JOYBUTTONDOWN:
# with members: joy (which joystick), button
action = self.joystick.get_action(event.button)
elif event.type == pygame.MOUSEBUTTONDOWN:
action = self.keyboard.get_action(event.button)
else:
action = ""
#
if action not in actions:
actions.append(action)
if action is 'interact':
pass
if action is 'exit': Done = True
if action is 'edit':
#turn on edit mode
print('edit mode')
if action == "use_item1": self.gameObject.inventory.use_item("Potion")
if action == "use_item2":
self.gameObject.inventory.use_item("GoldKey")
if action == "use_ability1":
target = self.screenClass.getLocation() + get_mouse_pos("tiles")
particle_factories.explosionFactory.create(target[0], target[1])
if action is 'attack': self.cbs_to_call.append(action)
# real-time events -- meaning, keys that are continuously held down
status = pygame.key.get_pressed()
status_mouse = pygame.mouse.get_pressed()
if self.use_joystick:
movement = self.joystick.get_direction()
# scale by squaring, for more sensitivity at smaller joystick values
movement = np.multiply(movement, np.abs(movement))
dx = movement[0]
dy = movement[1]
# continuous weapon
if self.gameObject.attacker.weapon.is_continuous and "attack" not in self.cbs_to_call:
if self.joystick.continuous_attack(): self.cbs_to_call.append("attack")
else:
realTimeActions = self.keyboard.getRealTimeAction(status)
for aa in realTimeActions:
if aa == 'left': dx -= 1
if aa == 'right': dx += 1
if aa == 'up': dy -= 1
if aa == 'down': dy += 1
if abs(dx) > 0.0 and abs(dy) > 0.0: #moving diagonally
dx *= 0.707106
dy *= 0.707106
# continuous weapon
if self.gameObject.attacker.weapon.is_continuous and "attack" not in self.cbs_to_call:
# keys = [i for i in range(3) if status_mouse[i]]
# action = self.keyboard.get_action(event.button)
if status_mouse[0]:
self.cbs_to_call.append("attack")
# scale to max velocity
dx *= self.gameObject.max_velocity
dy *= self.gameObject.max_velocity
self.dx = dx
self.dy = dy
self.dt = 0
return actions
def get_action(self, elapsed_time):
out = {'dv': np.array([self.dx, self.dy])}
cbs = self.cbs_to_call
# clear cbs
self.cbs_to_call = []
return out, cbs