本篇地址
http://blog.csdn.net/changjixiong/article/details/8103038,转载请注明出处
而g_animalDB的初始化,则在函数LoadGame()里面
在服务器启动后,监听前,调用LoadGame()加载数据,目前只有初始化g_animalDB,以后会有更多精彩内容~~
主要内容
客户端:重构了Sprite类,修改了资源加载。修改了消息格式
服务端:增加了GameLoop用来刷怪邮件地址:[email protected] 微博 http://weibo.com/changjixiong
本篇分为上下2篇
此为下篇
服务端增加了类 Animal用于处理怪的数据
玩家登陆的时候,服务端发送在线玩家的数据给客户端用于在客户端创建玩家并显示,这个之前已经完成了,现在需要将地图上面的怪物数据(目前是狼)发送给客户端,用于创建并显示,代码增加在处理器HandlerLogon里面
for sprite in g_animalDB.spriteDic.values():
print 'send g_animalDB'
connection.translator.sendString(connection, sprite.posMsg('INITSPRITE'))
而g_animalDB的初始化,则在函数LoadGame()里面
def LoadGame():
global g_animalDB
global OBJ_TYPE
for x in xrange(6):
animal = Animal(OBJ_TYPE['wolf'])
animal.inUse = True
animal.x = random.randint(0,640/32)*32
animal.y = random.randint(0,480/24)*24
g_animalDB.addSprite(animal)
在服务器启动后,监听前,调用LoadGame()加载数据,目前只有初始化g_animalDB,以后会有更多精彩内容~~
将精灵的动作消息的生成移动到精灵类(Role, Animal)里面。
posMsg 生成位置消息
dieMsg 生成死亡消息
reviveMsg 生成复活消息
然后,将生成的消息发送即可(现阶段是全局发送,以后地图大了,需要根据AOI来发送
GameLoop用来定时刷新并发送消息,比如,地图上面的怪刷新了,玩家死了以后复活时间到了发送复活消息等等
def GameLoop(*connList):
currentTime = time.time()
global LastanimalGenTimeInterval
'''
typeid, id, statu, action, targetTypeID, targetid, x, y
'''
global g_deadRoleList
for role in g_deadRoleList:
if currentTime - role.dieTime>=1:
role.revive()
g_deadRoleList.remove(role)
if currentTime - LastanimalGenTimeInterval>=5:
for animal in g_deadAnimalList:
if currentTime - animal.dieTime>=2:
animal.revive()
g_deadAnimalList.remove(animal)
LastanimalGenTimeInterval = currentTime
在客户端和服务端,精灵死亡以后,将状态标记,需要用的时候,在标记成正常并使用,这样省去了创建于销毁的开销,也就是所谓的对象池的思想。