-
Notifications
You must be signed in to change notification settings - Fork 0
/
client1.py
212 lines (163 loc) · 7.09 KB
/
client1.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
""" Demonstrates the DC system from the client side, using a
DistributedNode-based avatar. """
from direct.directbase.DirectStart import *
from direct.distributed.ClientRepository import ClientRepository
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from DistributedPellet import DistributedPellet
import sys
import random
helpText = """[Left Arrow]: Rotate Left
[Right Arrow]: Rotate Right
[Up Arrow]: Move Forward
[Down Arrow]: Move Backwards
[Tab]: Change color
[Space]: Drop pellet
[c]: Clear pellets
[1 - 9]: Set zone
[Escape]: exit"""
class MyClientRepository(ClientRepository):
def __init__(self):
dcFileNames = ['direct.dc', 'net.dc']
ClientRepository.__init__(self, dcFileNames = dcFileNames)
class World(DirectObject):
# Degrees per second of rotation
rotateSpeed = 90
# Units per second of motion
moveSpeed = 8
def __init__(self):
DirectObject.__init__(self)
# No avatar yet.
self.av = None
# No pellets either.
self.pellets = []
# The list of keys that we will be monitoring.
self.moveKeyList = [
'arrow_left', 'arrow_right', 'arrow_up', 'arrow_down'
]
# Initially, all keys are up. Construct a dictionary that
# maps each of the above keys to False, and hang the event to
# manage that state.
self.moveKeys = {}
for key in self.moveKeyList:
self.moveKeys[key] = False
self.accept(key, self.moveKeyStateChanged, extraArgs = [key, True])
self.accept(key + '-up', self.moveKeyStateChanged, extraArgs = [key, False])
tcpPort = base.config.GetInt('server-port', 4400)
hostname = base.config.GetString('server-host', '127.0.0.1')
self.url = URLSpec('http://%s:%s' % (hostname, tcpPort))
self.cr = MyClientRepository()
self.cr.connect([self.url],
successCallback = self.connectSuccess,
failureCallback = self.connectFailure)
self.waitingText = OnscreenText(
'Connecting to %s.\nPress ESC to cancel.' % (self.url),
scale = 0.1, fg = (1, 1, 1, 1), shadow = (0, 0, 0, 1))
self.accept('escape', self.escape)
# Oobe mode is handy to have on all the time. Why not?
base.oobe()
def moveKeyStateChanged(self, key, newState):
""" A key event has been received. Change the key state in
the dictionary. """
self.moveKeys[key] = newState
def escape(self):
""" The user pressed escape. Exit the client. """
sys.exit()
def connectFailure(self, statusCode, statusString):
self.waitingText.destroy()
self.failureText = OnscreenText(
'Failed to connect to %s: %s.\nPress ESC to quit.' % (self.url, statusString),
scale = 0.15, fg = (1, 0, 0, 1), shadow = (0, 0, 0, 1))
def connectSuccess(self):
""" Successfully connected. But we still can't really do
anything until we've got the doID range. """
self.waitingText.destroy()
self.waitingText = OnscreenText(
'Waiting for server.',
scale = 0.1, fg = (1, 1, 1, 1), shadow = (0, 0, 0, 1))
self.acceptOnce('createReady', self.createReady)
def createReady(self):
""" Now we're ready to go! """
self.waitingText.destroy()
# Manifest an avatar for ourselves.
self.av = self.cr.createDistributedObject(
className = 'DistributedAvatar', zoneId = 1)
# The tab key changes your color.
self.accept('tab', self.changeAvColor)
# The space bar drops a new pellet.
self.accept('space', self.dropPellet)
# The 'c' key clears the pellets you've dropped.
self.accept('c', self.clearPellets)
# A number key (other than zero) changes your zone.
for zoneId in range(1, 10):
self.accept(str(zoneId), self.changeAvZone, extraArgs = [zoneId])
# Pop up some help text.
self.title = OnscreenText(
parent = base.a2dBottomRight,
text = 'DistributedNode client demo',
fg = (1, 1, 1, 1),
pos = (-0.03, 0.03), align = TextNode.ARight, scale = 0.1)
self.help = OnscreenText(
parent = base.a2dTopLeft,
text = helpText,
fg = (1, 1, 1, 1),
pos=(0.03, -0.1), align = TextNode.ALeft, scale = 0.07)
# Update the local avatar's position every frame.
self.moveTask = taskMgr.add(self.moveAvatar, 'moveAvatar')
# Send position updates only several times a second, instead
# of every frame, or we will flood the network.
self.lastBroadcastTransform = self.av.getTransform()
self.updateTask = taskMgr.doMethodLater(0.2, self.updateAvatar, 'updateAvatar')
def dropPellet(self):
# Create a new DistributedPellet, and put it right where the
# avatar is.
pellet = DistributedPellet(self.cr)
x, y, z = self.av.getPos()
pellet.setInitialPos(x, y, z)
self.cr.createDistributedObject(
distObj = pellet, zoneId = self.av.zoneId)
self.pellets.append(pellet)
def clearPellets(self):
# Remove all of the DistributedPellets we've created.
for p in self.pellets:
self.cr.sendDeleteMsg(p.doId)
def changeAvColor(self):
""" The user pressed the tab key. Change the color of the
local avatar to a new random color. """
r = random.uniform(0, 1)
g = random.uniform(0, 1)
b = random.uniform(0, 1)
self.av.b_setAvColor(r, g, b)
def changeAvZone(self, zoneId):
""" The user pressed one of the number keys to change zones.
Move the avatar into the indicated zone. """
# Move our avatar into the indicated zone
self.cr.setObjectZone(self.av, zoneId)
def moveAvatar(self, task):
""" This task runs each frame to move the avatar according to
the set of arrow keys that are being held. """
dt = globalClock.getDt()
if self.moveKeys['arrow_left']:
self.av.setH(self.av, dt * self.rotateSpeed)
elif self.moveKeys['arrow_right']:
self.av.setH(self.av, -dt * self.rotateSpeed)
if self.moveKeys['arrow_up']:
self.av.setY(self.av, dt * self.moveSpeed)
elif self.moveKeys['arrow_down']:
self.av.setY(self.av, -dt * self.moveSpeed)
return task.cont
def updateAvatar(self, task):
""" This task runs five times a second to broadcast the
avatar's position to all of the other clients. """
currentTransform = self.av.getTransform()
if self.lastBroadcastTransform == currentTransform:
# No change since last broadcast.
return task.again
self.lastBroadcastTransform = currentTransform
x, y, z = self.av.getPos()
h, p, r = self.av.getHpr()
self.av.d_setPosHpr(x, y, z, h, p, r)
return task.again
w = World()
run()