我目前正在使用 HTML5 框架 Phaser 创建一款多人游戏。
在该游戏中,僵尸会在 map 上生成,玩家必须射击它们才能杀死它们。僵尸以距离它们最近的玩家为目标。
目前,我的设计策略存在问题。由于移动跟踪,我不确定 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], 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();
}
问题是我无法在服务器上创建 Phaser 游戏对象(因为它在 Linux 服务器上运行),因为没有可以使用的窗口。为了碰撞检测,子弹和僵尸需要是 Phaser 对象,但我不能在服务器上这样做。
我知道我可以在服务器端创建一个僵尸和子弹矢量,它会在任何给定时间包含每个子弹/僵尸位置的信息,然后更新客户端,但那样我就无法使用Phaser 中的 CollisionManager。
现在,我唯一的解决办法似乎是创建我自己的碰撞检测系统。有没有其他想法?
最佳答案
我也在寻找答案。 Phaser 论坛的管理员说,没有黑客攻击是不可能的。 请引用this post和 another post
关于html - 如何在没有窗口的情况下运行 Phaser 引擎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22754517/