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. 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... 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 // 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 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 teamScores = {};
var gameOver = false; var savedModes = {};
for (var teamName in this.teams){ for (var teamName in this.teams){
teamScores[teamName] = 0; teamScores[teamName] = 0;
var team = this.teams[teamName]; var team = this.teams[teamName];
for (var i = 0;i < team.length;i++) for (var i = 0;i < team.length;i++)
{ {
var player = server.getPlayer(team[i]); var player = server.getPlayer(team[i]);
savedModes[player.name] = player.gameMode;
player.gameMode = org.bukkit.GameMode.SURVIVAL; player.gameMode = org.bukkit.GameMode.SURVIVAL;
player.inventory.addItem([snowBalls,snowBalls,snowBalls]); player.inventory.addItem(ammo);
player.itemInHand = ammo[0];
} }
} }
var that = this; var that = this;
@ -56,38 +63,56 @@ SnowBallFight.prototype.start = function()
} }
return null; return null;
}; };
var listener = events.on("entity.EntityDamageByEntityEvent",function(l,e){ //
var damager = e.damager; // this function is called every time a player is damaged by another entity/player
var damagee = e.entity; //
var damage = e.damage; var listener = events.on("entity.EntityDamageByEntityEvent",function(l,event){
var shooter = damager.shooter; var snowball = event.damager;
if (damager instanceof org.bukkit.entity.Snowball){ if (!snowball)
var damagerTeam = _getTeam(shooter); return;
if (!damagerTeam) var damagee = event.entity;
return; // shooter wasn't in game 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); var damageeTeam = _getTeam(damagee);
if (!damageeTeam) if (!damageeTeam)
return; // damagee wasn't in game return; // damagee wasn't in game
if (damagerTeam != damageeTeam){ if (throwersTeam != damageeTeam)
teamScores[damagerTeam]++; teamScores[throwersTeam]++;
}else{ else
teamScores[damagerTeam]--; 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(){ var tick = function(){
while (that.duration--){ while (that.duration--)
java.lang.Thread.sleep(1000); java.lang.Thread.sleep(1000); // sleep 1,000 millisecs (1 second)
} //
if (that.duration <=0){ // game over
for (var tn in teamScores){ //
server.broadcastMessage("Team " + tn + " scored " + teamScores[tn]); 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(); new java.lang.Thread(tick).start();
}; };