-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.py
94 lines (80 loc) · 2.39 KB
/
Scene.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
# !/usr/bin/python
# coding:utf-8
import ScenePlayer
import threading
import ProtocolByte
import ServNet
class Scene(object):
playerList = []
# GetScenePlayer via id
def GetScenePlayer(self, id):
for i in xrange(len(self.playerList)):
if Scene.playerList[i].id == id:
return Scene.playerList[i]
return None
# Add player
def AddPlayer(self, id):
lock = threading.Lock()
with lock:
p = ScenePlayer.ScenePlayer()
p.id = id
self.playerList.append(p)
# Delete player
def DelPlayer(self, id):
lock = threading.Lock()
with lock:
p = self.GetScenePlayer(id)
if p is not None:
self.playerList.remove(p)
protocol = ProtocolByte.ProtocolByte()
protocol.AddString("PlayerLeave")
protocol.AddString(id)
ServNet.servNet.Boradcast(protocol)
# SendPlayerList
def SendPlayerList(self, player):
count = len(self.playerList)
protocol = ProtocolByte.ProtocolByte()
protocol.AddString("GetList")
protocol.AddInt(count)
for i in xrange(count):
p = self.playerList[i]
protocol.AddString(p.id)
protocol.AddFloat(p.x)
protocol.AddFloat(p.y)
protocol.AddFloat(p.z)
player.Send(protocol)
# Update pos
def UpdatePos(self, id, x, y, z):
p = self.GetScenePlayer(id)
if p is None:
return
p.x = x
p.y = y
p.z = z
# Update score
def UpdateScore(self, id, score):
p = self.GetScenePlayer(id)
if p is None:
return
p.score = score
# Update health
def UpdateHealth(self, id, health):
p = self.GetScenePlayer(id)
if p is None:
return
p.health = health
# Update ammo
def UpdateAmmo(self, id, weaponType, carryingAmmo, clipAmmo):
p = self.GetScenePlayer(id)
if p is None:
return
if weaponType == "Pistol":
p.pistolCarryingAmmo = carryingAmmo
p.pistolClipAmmo = clipAmmo
elif weaponType == "Rifle":
p.rifleCarryingAmmo = carryingAmmo
p.rifleClipAmmo = clipAmmo
else:
print "[Warning] Scene.UpdateAmmo: weapon type not found: ", weaponType
# Singleton pattern
scene = Scene()