我目前正在使用
HTML5框架Phaser创建一个多人游戏.
这是一个僵尸在地图上产生的游戏,玩家必须射杀它们才能杀死它们.僵尸瞄准最接近他们的玩家.
目前,我的设计策略存在问题.由于运动跟踪,我不确定Phaser是否可以使用这种类型的游戏.
目前,客户端正在处理所有玩家移动,因此每当玩家移动时,它会将其广播到服务器,然后服务器将其发送给所有其他客户端.
但是,我想让僵尸和子弹完全由服务器控制.然后,服务器使用每个僵尸的速度及其当前位置更新每个客户端.我的理由是,任何非玩家输入的内容都应由服务器计算.这将防止诸如两个客户说不同时间僵尸死亡然后试图彼此通信,同时在不同位置有子弹,或者在客户之间的不同时间产生僵尸的问题.
这是一个僵尸类的例子:
function Zombie(game,data){
this.game = game;
this.id = data.id;
Phaser.Sprite.call(this,this.game,data.x,data.y,'zombie');
this.anchor.setTo(0.5,0.5);
this.animations.add('right',[0,1,2,3],7,true);
this.animations.add('left',[4,5,6,7],true);
this.game.physics.arcade.enable(this);
this.body.collideWorldBounds = true;
this.health = data.health;
this.maxHealth = data.maxHealth;
this.speed = data.speed;
this.target = this.game.player;
this.waiting = 100;
this.name = "zombie";
this.healthBary = 20;
this.healthBar = this.game.add.sprite(this.x,this.y + this.healthBary,'player_health');
this.healthBar.anchor.setTo(0.5,0.5);
CollisionManager.addObjectToGroup(this,'baddies');
this.game.add.existing(this);
}
Zombie.prototype = Object.create( Phaser.Sprite.prototype );
Zombie.prototype.constructor = Zombie;
Zombie.prototype.update = function(){
this.updateHealthBar();
this.moveTowards(this.target);
Zombie.prototype.uTarget = function(target) {
this.target = target;
};
Zombie.prototype.moveTowards = function(target){
var x = target.x - this.x;
var y = target.y - this.y;
var mag = Math.sqrt((x * x) + (y * y));
var nx = x / mag;
var ny = y / mag;
this.body.velocity.x = nx * this.speed;
this.body.velocity.y = ny * this.speed;
if(this.body.velocity.x >= 0){
this.animations.play('right');
}
else if(this.body.velocity.x < 0){
this.animations.play('left')
}
}
Zombie.prototype.updateHealthBar = function(){
this.healthBar.x = this.x;
this.healthBar.y = this.y + this.healthBary;
var p = (this.health / this.maxHealth);
p = parseFloat(p.toFixed(1));
this.healthBar.frame = 10 - (p * 10);
}
Zombie.prototype._damage = function(amount){
this.health -= amount;
if(this.health <= 0){
this.kill;
this.die(true);
}
}
Zombie.prototype.die = function(points){
if(this.game){
//this.game.baddie_die_sfx.play();
}
WaveManager.onMap--;
CollisionManager.removeObjectFromGroup(this,"baddies");
if(this.healthBar){
this.healthBar.destroy();
}
socket.emit("kill zombie",{id: this.id});
this.kill();
this.destroy();
}@H_301_13@
问题是我无法在服务器上创建Phaser游戏对象(因为它在Linux服务器上运行),因为没有可以使用的窗口.为了碰撞检测,子弹和僵尸需要是Phaser对象,但我不能在服务器上这样做.
我知道我可以创建一个僵尸和子弹服务器端的向量,它可以在任何给定时间获得每个项目符号/僵尸位置的信息,然后更新客户端,但之后我将无法在Phaser中使用CollisionManager .
现在,似乎我唯一的解决方案是创建我自己的碰撞检测系统.还有其他想法吗?