Fixed game over handling

This commit is contained in:
walterhiggins 2013-01-28 22:36:26 +00:00
parent 359222fba9
commit 2ffb1ff78d

View file

@ -1,4 +1,4 @@
load(__folder + "events/events.js");
load(__folder + "../events/events.js");
/*
OK - this is a rough and ready prototype of a simple multi-player shoot-em-up.
Get a bunch of players in close proximity and issue the following commands...
@ -33,16 +33,23 @@ SnowBallFight.prototype.start = function()
{
// put all players in survival mode and give them each 200 snowballs
var snowBalls = new org.bukkit.inventory.ItemStack(org.bukkit.Material.SNOW_BALL, 64);
var ammo = [snowBalls];
// 64 snowballs for every 30 seconds should be more than enough
for (var i = 30;i < this.duration;i+=30){
ammo.push(snowBalls);
}
var teamScores = {};
var gameOver = false;
var savedModes = {};
for (var teamName in this.teams){
teamScores[teamName] = 0;
var team = this.teams[teamName];
for (var i = 0;i < team.length;i++)
{
var player = server.getPlayer(team[i]);
savedModes[player.name] = player.gameMode;
player.gameMode = org.bukkit.GameMode.SURVIVAL;
player.inventory.addItem([snowBalls,snowBalls,snowBalls]);
player.inventory.addItem(ammo);
player.itemInHand = ammo[0];
}
}
var that = this;
@ -56,38 +63,56 @@ SnowBallFight.prototype.start = function()
}
return null;
};
var listener = events.on("entity.EntityDamageByEntityEvent",function(l,e){
var damager = e.damager;
var damagee = e.entity;
var damage = e.damage;
var shooter = damager.shooter;
if (damager instanceof org.bukkit.entity.Snowball){
var damagerTeam = _getTeam(shooter);
if (!damagerTeam)
return; // shooter wasn't in game
//
// this function is called every time a player is damaged by another entity/player
//
var listener = events.on("entity.EntityDamageByEntityEvent",function(l,event){
var snowball = event.damager;
if (!snowball)
return;
var damagee = event.entity;
var thrower = snowball.shooter;
if (snowball instanceof org.bukkit.entity.Snowball){
var throwersTeam = _getTeam(thrower);
if (!throwersTeam)
return; // thrower wasn't in game
var damageeTeam = _getTeam(damagee);
if (!damageeTeam)
return; // damagee wasn't in game
if (damagerTeam != damageeTeam){
teamScores[damagerTeam]++;
}else{
teamScores[damagerTeam]--;
}
if (throwersTeam != damageeTeam)
teamScores[throwersTeam]++;
else
teamScores[throwersTeam]--;
}
if (gameOver)
e.handlers.unregister(l);
});
//
// this function is called every second and counts down to the end of the game.
//
var tick = function(){
while (that.duration--){
java.lang.Thread.sleep(1000);
}
if (that.duration <=0){
for (var tn in teamScores){
server.broadcastMessage("Team " + tn + " scored " + teamScores[tn]);
while (that.duration--)
java.lang.Thread.sleep(1000); // sleep 1,000 millisecs (1 second)
//
// game over
//
var scores = [];
for (var tn in teamScores)
scores.push("Team " + tn + " scored " + teamScores[tn]);
for (var teamName in that.teams){
var team = that.teams[teamName];
for (var i = 0;i < team.length;i++)
{
// restore player's previous game mode and take back snowballs
var player = server.getPlayer(team[i]);
player.gameMode = savedModes[player.name];
player.inventory.removeItem(ammo);
player.sendMessage("GAME OVER.");
player.sendMessage(scores);
}
gameOver = true;
}
var handlerList = org.bukkit.event.entity.EntityDamageByEntityEvent.getHandlerList();
handlerList.unregister(listener);
};
new java.lang.Thread(tick).start();
};