-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
556 lines (445 loc) · 19.5 KB
/
game.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import math
from panda3d.core import AmbientLight, DirectionalLight
from panda3d.core import TextNode
from panda3d.core import Point3, Vec3, Vec4
from panda3d.core import CompassEffect, KeyboardButton, WindowProperties
from direct.interval.LerpInterval import LerpPosInterval, LerpPosHprInterval
from direct.gui.DirectGui import DirectFrame, DirectEntry, DirectButton
from direct.gui.OnscreenText import OnscreenText
from direct.actor.Actor import Actor
from common.Constants import Constants
# helpers
from direct.showbase.PythonUtil import clampScalar
def addInstructions(pos, msg):
return OnscreenText(text = msg, style = 1, fg = (1, 1, 1, 1),
pos = (-1.3, pos), align = TextNode.ALeft, scale = .05)
def addTitle(text):
return OnscreenText(text = text, style = 1, fg = (1, 1, 1, 1),
pos = (1.3, -0.95), align = TextNode.ARight, scale = .07)
class Camera(object):
"""Uses mouse controls to orbit the camera around a parent."""
def __init__(self, game, parent):
self.game = game
# class properties for camera rotation
self.heading = 0
self.pitch = 0
# class properties for camera zoom
self.targetY = -6
self.interval = None
# find screen center
props = base.win.getProperties()
self.centerX = props.getXSize() / 2
self.centerY = props.getYSize() / 2
# constrain mouse
base.disableMouse() # disable default mouse camera control
self.hideMouse()
# set up floater
self.floater = render.attachNewNode('floater')
self.floater.reparentTo(parent) # inherit position from parent, but rotation and scale from render
self.floater.setEffect(CompassEffect.make(render, CompassEffect.P_rot | CompassEffect.P_scale))
self.floater.setZ(1) # distance above model
# set up camera
base.camera.reparentTo(self.floater)
base.camera.setY(self.targetY) # original distance from model
base.camera.lookAt(self.floater)
# camera zooming
# TODO move into method, clamp Y value?
base.accept('wheel_up', lambda: self.zoom(2))
base.accept('shift-wheel_up', lambda: self.zoom(2))
base.accept('wheel_down', lambda: self.zoom(-2))
base.accept('shift-wheel_down', lambda: self.zoom(-2))
# start task
taskMgr.add(self.mouseControl, 'Camera.mouseControl')
def hideMouse(self):
props = WindowProperties()
props.setCursorHidden(True)
props.setMouseMode(WindowProperties.M_relative)
base.win.requestProperties(props)
def showMouse(self):
props = WindowProperties()
props.setCursorHidden(False)
props.setMouseMode(WindowProperties.M_absolute)
base.win.requestProperties(props)
def mouseControl(self, task):
if self.game.isChatting: return task.cont
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
# update heading & pitch based on mouse movement from screen center
if base.win.movePointer(0, self.centerX, self.centerY):
self.heading -= (x - self.centerX) * 0.5
self.pitch -= (y - self.centerY) * 0.5
# constrain pitch
if self.pitch < 280: self.pitch = 280
if self.pitch > 360: self.pitch = 360
# update floater
self.floater.setHpr(self.heading, self.pitch, 0)
return task.cont
def zoom(self, amount):
if self.game.isChatting: return
self.targetY = clampScalar(self.targetY + amount, -2, -15)
if self.interval is not None: self.interval.pause()
self.interval = LerpPosInterval(base.camera,
duration = 0.6,
pos = Vec3(base.camera.getX(), self.targetY, base.camera.getZ()),
blendType = 'easeOut',
name = 'Camera.zoom')
self.interval.start()
class Character(object):
"""Handles character models and animations."""
def __init__(self, id, name, model):
# class properties
self.id = id
self.name = name
self.modelType = model
self.isMoving = False
self.entity = render.attachNewNode('entity')
self.target = render.attachNewNode('target')
# set up model
if model == Constants.CHAR_RALPH:
self.model = Actor('models/ralph',
{'run': 'models/ralph-run',
'walk': 'models/ralph-walk'})
self.model.setScale(0.2)
elif model == Constants.CHAR_PANDA:
self.model = Actor('models/panda-model',
{'run': 'models/panda-walk4',
'walk': 'models/panda-walk4'})
self.model.setScale(0.001)
elif model == Constants.CHAR_VEHICLE:
self.model = Actor('models/car', {})
self.model.setScale(0.3)
self.model.setPos(0, 0.2, 0.1)
self.model.reparentTo(self.entity)
# start movement task
taskMgr.doMethodLater(1.0 / Constants.TICKRATE, self.move, 'Character[' + str(id) + '].move')
def move(self, task):
diffPos = self.target.getPos() - self.entity.getPos()
diffH = (self.target.getH() - self.entity.getH()) % 360
if diffPos.lengthSquared() < 0.001 and -0.1 < diffH < 0.1:
if self.isMoving:
self.model.stop()
self.model.pose('walk', 5)
self.isMoving = False
else:
if not self.isMoving:
self.model.loop('run')
self.isMoving = True
interval = LerpPosHprInterval(self.entity,
duration = task.getDelay(),
pos = self.target.getPos(),
hpr = self.target.getHpr(),
name = 'Character[' + str(self.id) + '].move.interval')
interval.start()
return task.again
def destroy(self):
self.entity.removeNode()
self.entity = None
self.model = None
self.target.removeNode()
self.target = None
taskMgr.remove('Character[' + str(self.id) + '].move')
class Player(object):
"""Handles player character motion from user input."""
def __init__(self, game, character):
self.game = game
self.character = character
# class properties
self.buttons = {
'forward': KeyboardButton.ascii_key('w'),
'backward': KeyboardButton.ascii_key('s'),
'left': KeyboardButton.ascii_key('a'),
'right': KeyboardButton.ascii_key('d'),
'sprint': KeyboardButton.shift()
}
self.moved = False
# start movement task
taskMgr.add(self.move, 'Player.move')
taskMgr.doMethodLater(1.0 / Constants.TICKRATE, self.sendLoc, 'Player.sendLoc')
def move(self, task):
if self.game.isChatting: return task.cont
# use polling to detect keypresses
dt = globalClock.getDt()
is_down = base.mouseWatcherNode.is_button_down
move = is_down(self.buttons['forward']) - is_down(self.buttons['backward'])
turn = is_down(self.buttons['right']) - is_down(self.buttons['left'])
sprint = is_down(self.buttons['sprint']) + 1
# update heading
if turn:
self.character.target.setH(self.character.target.getH() + turn * -300 * dt)
# update position
if move:
oldPos = self.character.target.getPos()
self.character.target.setY(self.character.target, move * sprint * -8 * dt)
targetPos = self.character.target.getPos()
for id in self.game.characters:
char = self.game.characters[id]
if 0.1 < self.character.target.getPos(char.target).lengthSquared() < 1:
if (targetPos - oldPos).angleDeg(char.target.getPos() - oldPos) < 90:
move = False
self.character.target.setPos(oldPos)
break
# mark movement for sendLoc
if turn or move: self.moved = True
# continue
return task.cont
def sendLoc(self, task):
if self.moved:
self.moved = False
self.game.main.cManager.sendRequest(Constants.C_MOVE,
{ 'x': self.character.target.getX()
, 'y': self.character.target.getY()
, 'z': self.character.target.getZ()
, 'h': self.character.target.getH()
, 'p': self.character.target.getP()
, 'r': self.character.target.getR()
})
return task.again
class Sphere(object):
"""A mostly static sphere that spins when a Character is nearby."""
def __init__(self, game, type):
self.game = game
self.model = loader.loadModel('models/planet_sphere')
self.model.reparentTo(render)
if type == 'sun':
self.model.setTexture(loader.loadTexture('models/sun_1k_tex.jpg'), 1)
self.scale = 8
elif type == 'earth':
self.model.setTexture(loader.loadTexture('models/earth_1k_tex.jpg'), 1)
self.scale = 4
elif type == 'venus':
self.model.setTexture(loader.loadTexture('models/venus_1k_tex.jpg'), 1)
self.scale = 3
self.model.setScale(self.scale)
self.spin = self.model.hprInterval(self.scale * 3, Vec3(360, 0, 0))
self.spin.loop() # set up the interval to loop
self.spin.pause() # then pause so we don't start in motion
self.maxDist = self.scale * 2.5
taskMgr.add(self.move, 'Sphere.move')
def move(self, task):
spinning = self.checkDistance(self.game.character)
if not spinning:
for id in self.game.characters:
spinning = self.checkDistance(self.game.characters[id])
if spinning: break
if spinning != self.spin.isPlaying():
if spinning:
self.spin.resume()
else:
self.spin.pause()
return task.cont
def checkDistance(self, character):
if character is None: return False
return self.model.getPos(character.entity).lengthSquared() <= (self.maxDist * self.maxDist)
class Chat(object):
"""Handles all chat."""
def __init__(self, game):
self.game = game
self.lines = []
self.whisperTarget = None
props = base.win.getProperties()
ratio = float(props.getXSize()) / props.getYSize()
self.frame = DirectFrame(frameColor = (0, 0, 0, 0),
frameSize = (0, 1, 0, 1),
pos = (-ratio, 0, -1))
self.text = OnscreenText(text = '',
pos = (0.01, 0.45),
scale = 0.05,
fg = (1, 1, 1, 1),
bg = (0, 0, 0, 0.2),
parent = self.frame,
align = TextNode.ALeft,
mayChange = True)
self.entry = DirectEntry(parent = self.frame,
text = '',
scale = 0.05,
pos = (0.01, 0, 0.02),
initialText = '',
width = 26,
numLines = 1)
self.targetText = OnscreenText(text = '',
pos = (1.34, 0.025),
scale = 0.05,
fg = (1, 1, 1, 1),
bg = (0, 0, 0, 0.4),
parent = self.frame,
align = TextNode.ALeft,
mayChange = True)
base.accept('t', self.startChatting)
base.accept('shift-t', self.startChatting)
base.accept('y', self.startWhispering)
base.accept('shift-y', self.startWhispering)
base.accept('arrow_up', lambda: self.changeTarget(1))
base.accept('shift-arrow_up', lambda: self.changeTarget(1))
base.accept('arrow_down', lambda: self.changeTarget(-1))
base.accept('shift-arrow_down', lambda: self.changeTarget(-1))
base.accept('enter', self.sendChat)
base.accept('shift-enter', self.sendChat)
def startChatting(self):
if not self.game.isChatting:
self.game.isChatting = True
# focus on the DirectEntry
self.entry['focus'] = 1
# reset whisper target (just in case)
self.whisperTarget = None
# enable UI mode
if self.game.camera is not None:
self.game.camera.showMouse()
def startWhispering(self):
if not self.game.isChatting:
if not self.game.characters:
self.addLine('<<System>> Nobody to whisper!')
else:
self.startChatting()
self.changeTarget(0)
def changeTarget(self, amt):
if self.game.isChatting:
targets = self.game.characters.keys()
targets.sort()
# find target (or the closest id before target, or 0)
index = 0
match = False
for i in range(len(targets)):
if targets[i] < self.whisperTarget:
index = i
elif targets[i] == self.whisperTarget:
index = i
match = True
break
else:
break
# if not found but going back 1 anyway, set amt to 0
if not match and amt == -1:
amt = 0
# set new whisper target
index = (index + amt) % len(targets)
self.whisperTarget = targets[index]
self.targetText.setText('to: ' + str(self.game.characters[self.whisperTarget].name))
def sendChat(self):
if self.game.isChatting:
message = self.entry.get().strip()
if len(message) > 0:
# figure out target
target = ''
if self.whisperTarget in self.game.characters:
target = self.game.characters[self.whisperTarget].name
# submit message
self.game.main.cManager.sendRequest(Constants.C_CHAT, {'message': message, 'target': target})
# stop chatting
self.stopChatting()
def stopChatting(self):
if self.game.isChatting:
self.game.isChatting = False
# disable chat entry
self.entry['focus'] = 0
# clear text box
self.entry.enterText('')
# remove whisper target
self.whisperTarget = None
self.targetText.setText('')
# disable UI mode
if self.game.camera is not None:
self.game.camera.hideMouse()
def addLine(self, line):
self.lines.append(line)
self.text.setText('\n'.join(self.lines[-8:]))
class PlayerList(object):
"""Handles the list of players and the associated UI."""
def __init__(self, game):
self.game = game
self.display = False
props = base.win.getProperties()
ratio = float(props.getXSize()) / props.getYSize()
self.frame = DirectFrame(frameColor = (0, 0, 0, 0),
frameSize = (-1, 0, -1, 0),
pos = (ratio, 0, 1))
self.text = OnscreenText(text = '',
pos = (-0.02, -0.05),
scale = 0.05,
fg = (1, 1, 1, 1),
parent = self.frame,
align = TextNode.ARight,
mayChange = True)
base.accept('tab', self.toggleDisplay)
def toggleDisplay(self):
if not self.game.isChatting:
self.display = not self.display
self.updateDisplay()
def updateDisplay(self):
if self.display:
list = 'Current Players:'
if self.game.character is not None:
list += '\n' + self.describe(self.game.character)
for id in self.game.characters:
list += '\n' + self.describe(self.game.characters[id])
self.text.setText(list)
self.display = True
else:
self.text.setText('')
def describe(self, character):
desc = character.name + ' ('
if character.modelType == Constants.CHAR_RALPH: desc += 'Ralph'
if character.modelType == Constants.CHAR_PANDA: desc += 'Panda'
if character.modelType == Constants.CHAR_VEHICLE: desc += 'Car'
return desc + ')'
class Game(object):
"""Handles the entire game environment."""
def __init__(self, main):
self.main = main
self.isChatting = False
def init(self):
# window setup
base.win.setClearColor(Vec4(0, 0, 0, 1))
# display instructions
addTitle('CS454 HW2')
addInstructions(0.95, '[ESC]: Quit')
addInstructions(0.90, '[Mouse Move]: Move camera')
addInstructions(0.85, '[Mouse Wheel]: Zoom camera')
addInstructions(0.80, '[W,A,S,D]: Move character')
addInstructions(0.75, '[T]: Open chat box')
addInstructions(0.70, '[Y]: Open whisper box')
addInstructions(0.65, '[Up,Down]: Select whisper target')
addInstructions(0.60, '[Tab]: Show players')
# create environment
environ = loader.loadModel('models/square')
environ.reparentTo(render)
environ.setPos(0, 0, 0)
environ.setScale(100, 100, 1)
environ.setTexture(loader.loadTexture('models/moon_1k_tex.jpg'), 1)
# create lighting
ambientLight = AmbientLight('ambientLight')
ambientLight.setColor(Vec4(.3, .3, .3, 1))
render.setLight(render.attachNewNode(ambientLight))
directionalLight = DirectionalLight('directionalLight')
directionalLight.setDirection(Vec3(-5, -5, -5))
directionalLight.setColor(Vec4(1, 1, 1, 1))
directionalLight.setSpecularColor(Vec4(1, 1, 1, 1))
render.setLight(render.attachNewNode(directionalLight))
# accept special keys
base.accept('escape', self.exit)
base.accept('shift-escape', self.exit)
# create spheres
sun = Sphere(self, 'sun')
sun.model.setPos(-15, -15, 8)
earth = Sphere(self, 'earth')
earth.model.setPos(-12, 12, 5)
venus = Sphere(self, 'venus')
venus.model.setPos(10, 10, 3.5)
# track game entities
self.character = None
self.player = None
self.camera = None
self.characters = {}
self.chat = Chat(self)
self.playerList = PlayerList(self)
# DEBUG offline: create character and camera
#self.character = Character(1, 'Ralph', Constants.CHAR_RALPH)
#self.player = Player(self, self.character)
#self.camera = Camera(self, self.character.entity)
#self.characters[2] = Character(2, 'Panda', Constants.CHAR_PANDA)
#self.characters[3] = Character(3, 'Car', Constants.CHAR_VEHICLE)
def exit(self):
if self.isChatting:
self.chat.stopChatting()
else:
self.main.cManager.sendRequest(Constants.C_DISCONNECT)