Skip to content

Commit

Permalink
id -> name
Browse files Browse the repository at this point in the history
  • Loading branch information
arikel committed Apr 11, 2012
1 parent e541809 commit 1930011
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 60 deletions.
64 changes: 32 additions & 32 deletions gameClient.py
Expand Up @@ -114,17 +114,17 @@ def Network_player_enter_map(self, data):
print "Player %s entered the map" % (name)

def Network_player_leave_map(self, data):
id = data["id"]
self.displayMap.delPlayer(id)
print "Player %s left the map" % (id)
name = data["id"]
self.displayMap.delPlayer(name)
print "Player %s left the map" % (name)


def Network_player_update_move(self, data):
id = data['id']
if id == self.id:
name = data['id']
if name == self.name:
# if we're not connected yet
if self.id not in self.displayMap.players:
self.addPlayer(self.id, data['x'], data['y'])
if self.name not in self.displayMap.players:
self.addPlayer(self.name, data['x'], data['y'])
print("Looks like we're on the map now...")
#else:
#d= getDist(self.displayMap.players[self.id].mapRect, pygame.Rect((data['x'], data['y'],0,0)))
Expand All @@ -139,54 +139,54 @@ def Network_player_update_move(self, data):
y = data['y']
dx = data['dx']
dy = data['dy']
if id in self.displayMap.players:
self.displayMap.players[id].setPos(x, y)
self.displayMap.players[id].setMovement(dx, dy)
if name in self.displayMap.players:
self.displayMap.players[name].setPos(x, y)
self.displayMap.players[name].setMovement(dx, dy)
#print "updated other player pos : %s, %s/%s" % (name, x, y)
else:
self.addPlayer(id, x, y)
self.displayMap.players[id].setMovement(dx, dy)
self.addPlayer(name, x, y)
self.displayMap.players[name].setMovement(dx, dy)

#print "received move_update from server : %s is now at %s / %s, and going in %s / %s" % (id, x, y, dx, dy)

def Network_mob_leave_map(self, data):
id = data["id"]
self.displayMap.delMob(id)
print "Mob %s left the map" % (id)
if self.displayMap.selected == id:
name = data["id"]
self.displayMap.delMob(name)
print "Mob %s left the map" % (name)
if self.displayMap.selected == name:
self.displayMap.unselectTarget()

def Network_mob_took_damage(self, data):
mobId = data["id"]
if mobId not in self.displayMap.mobs:
mobName = data["id"]
if mobName not in self.displayMap.mobs:
return
dmg = data["dmg"]

print "Mob %s took %s damage points" % (mobId, dmg)
x, y = self.displayMap.mobs[mobId].mapRect.topleft
y -= self.displayMap.mobs[mobId]._sprite.rect.h
x -= self.displayMap.mobs[mobId]._sprite.rect.w
print "Mob %s took %s damage points" % (mobName, dmg)
x, y = self.displayMap.mobs[mobName].mapRect.topleft
y -= self.displayMap.mobs[mobName]._sprite.rect.h
x -= self.displayMap.mobs[mobName]._sprite.rect.w

self.displayMap.particleManager.addParticle("damage", x, y, str(dmg))

def Network_mob_update_move(self, data):
id = data['id']
if id == self.id:# why don't i feel confident this would never happen?
name = data['id']
if name == self.name:# why don't i feel confident this would never happen?
print("Something totally weird just happened, i think you're a mob.")
return

x = data['x']
y = data['y']
dx = data['dx']
dy = data['dy']
if id in self.displayMap.mobs:
self.displayMap.mobs[id].setPos(x, y)
self.displayMap.mobs[id].setMovement(dx, dy)
if name in self.displayMap.mobs:
self.displayMap.mobs[name].setPos(x, y)
self.displayMap.mobs[name].setMovement(dx, dy)
#print "known mob pos : %s, %s/%s, %s/%s" % (id, x, y, dx, dy)
else:
self.addMob(id, x, y)
self.displayMap.mobs[id].setPos(x, y)
self.displayMap.mobs[id].setMovement(dx, dy)
self.addMob(name, x, y)
self.displayMap.mobs[name].setPos(x, y)
self.displayMap.mobs[name].setMovement(dx, dy)
#print "mob spotted at %s %s , moving : %s %s" % (x, y, dx, dy)
#print "received MOB_move_update from server : %s is now at %s / %s, and going in %s / %s" % (id, x, y, dx, dy)

Expand All @@ -195,7 +195,7 @@ def Network_warp(self, data):
mapFileName = data['mapFileName']
x = data['x']
y = data['y']
player = self.displayMap.players[self.id]
player = self.displayMap.players[self.name]
w = self.displayMap.tileWidth
h = self.displayMap.tileHeight

Expand Down
24 changes: 11 additions & 13 deletions gameEngine.py
Expand Up @@ -206,9 +206,9 @@ def getSkill(self, skillName):


class Mob(Being, MapCreature):
def __init__(self, id, mobId, _map, x, y):
Being.__init__(self, id)
MapCreature.__init__(self, id, _map)
def __init__(self, name, mobId, _map, x, y):
Being.__init__(self, name)
MapCreature.__init__(self, name, _map)
self.mobile = True
self.category = "mob"
self.mobId = mobId
Expand All @@ -218,9 +218,9 @@ def __init__(self, id, mobId, _map, x, y):
self.speed = 0.05

class Player(Being, MapCreature):
def __init__(self, id, _map, x, y):
Being.__init__(self, id)
MapCreature.__init__(self, id, _map)
def __init__(self, name, _map, x, y):
Being.__init__(self, name)
MapCreature.__init__(self, name, _map)
self.mobile = True
self.category = "player"
self.setPos(x, y)
Expand Down Expand Up @@ -492,13 +492,11 @@ def addPlayer(self, playerId, x, y):
def delPlayer(self, playerName):
del self.players[playerName]

def addMob(self, id, mobId, x, y):
newId = id
self.mobs[newId]=Mob(newId, mobId, self, x, y)
#self.mobs[mob.id].setPos(x, y)

def delMob(self, id):
del self.mobs[id]
def addMob(self, name, mobId, x, y):
self.mobs[name]=Mob(name, mobId, self, x, y)

def delMob(self, name):
del self.mobs[name]

def update(self, dt):
for player in self.players.values():
Expand Down
22 changes: 11 additions & 11 deletions main.py
Expand Up @@ -77,20 +77,20 @@ def setMap(self, mapFileName, x, y):
print "Entering map %s" % (mapFileName)
self.mode = "game"
self.update = self.updateGame
self.id = self.loginGui.name
self.name = self.loginGui.name
self.displayMap = Map(mapFileName)
self.addPlayer(self.id, x, y)
self.addPlayer(self.name, x, y)
else:
if self.displayMap.filename == mapFileName:
print "Warp in current map (%s)" % (mapFileName)
self.displayMap.players[self.id].setPos(x, y)
print "Warping in current map (%s)" % (mapFileName)
self.displayMap.players[self.name].setPos(x, y)
else:
print "Changing map for %s" % (mapFileName)
self.displayMap = Map(mapFileName)
self.addPlayer(self.id, x, y)
self.addPlayer(self.name, x, y)

def getClosestMobName(self):
myRect = self.displayMap.players[self.id].mapRect
myRect = self.displayMap.players[self.name].mapRect
minDist = 2000.0
closestMob = None
for mobName, mob in self.displayMap.mobs.items():
Expand Down Expand Up @@ -126,7 +126,7 @@ def updateGame(self):
self.prevTime = t
self.prevMove = (self.dx, self.dy)

if self.id not in self.displayMap.players:
if self.name not in self.displayMap.players:
#print "not connected to map"
return

Expand All @@ -143,17 +143,17 @@ def updateGame(self):
if not self.gameGui.chatWindow.entry.has_focus:

if (self.prevMove != (self.dx, self.dy)):# or (t>self.sendPosCooldown):
self.displayMap.players[self.id].setMovement(self.dx, self.dy)
self.displayMap.players[self.name].setMovement(self.dx, self.dy)
#self.sendPosCooldown = t+25
#print "Player direction changed from %s to %s/%s" % (self.prevMove, self.dx, self.dy)
self.SendUpdateMove(self.displayMap.players[self.id].x, self.displayMap.players[self.id].y, self.dx, self.dy)
self.SendUpdateMove(self.displayMap.players[self.name].x, self.displayMap.players[self.name].y, self.dx, self.dy)

else:
self.dx = 0
self.dy = 0

offx = self.displayMap.players[self.id].mapRect.x-SCREEN_WIDTH/2
offy = self.displayMap.players[self.id].mapRect.y-SCREEN_HEIGHT/2
offx = self.displayMap.players[self.name].mapRect.x-SCREEN_WIDTH/2
offy = self.displayMap.players[self.name].mapRect.y-SCREEN_HEIGHT/2
self.displayMap.setOffset(offx, offy)
self.displayMap.update(dt)

Expand Down
8 changes: 4 additions & 4 deletions sprite.py
Expand Up @@ -195,10 +195,10 @@ def blit(self, screen):
screen.blit(self.talkImg, (self.rect.x+3, self.rect.y-16))


#pygame.draw.rect(screen,
# (255,120,120,120),
# (self.mapRect.x-self.mapOffsetX, self.mapRect.y-self.mapOffsetY, self.mapRect.w, self.mapRect.h),
# 1)
pygame.draw.rect(screen,
(255,120,120,120),
(self.mapRect.x-self.mapOffsetX, self.mapRect.y-self.mapOffsetY, self.mapRect.w, self.mapRect.h),
1)

def destroy(self):
pass
Expand Down

0 comments on commit 1930011

Please sign in to comment.