tidy up code - hide all but .start() method

This commit is contained in:
walterhiggins 2013-01-29 22:03:10 +00:00
parent a621c473c1
commit 7c393ea070

View file

@ -8,6 +8,10 @@ load(__folder + "../events/events.js");
/js var greenTeam = ['<player5>','<player6>,...etc]
/js new SnowBallFight({red: redTeam,blue: blueTeam,green: greenTeam},60).start();
Alternatively you can just have all players play against each other...
/js new SnowBallFight(['player1','player2','player3'],60).start();
(where <player1> etc are the names of actual players)
You specify the teams in the game as an object where each property's name is a team name and
@ -17,103 +21,137 @@ load(__folder + "../events/events.js");
I need to work on a better in-game mechanism for players to choose teams and start the game
but this will do for now.
When the game starts, each player is put in survival mode and given 192 snowballs. The aim of the
When the game starts, each player is put in survival mode and given snowballs. The aim of the
game is to hit players on opposing teams. If you hit a player on your own team, you lose a point.
At the end of the game the scores for each team are broadcast. Create a small arena
with a couple of small buildings for cover to make the game more fun :-)
*/
var SnowBallFight = function(teams,duration)
{
this.teams = teams;
this.duration = duration;
};
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 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(ammo);
player.itemInHand = ammo[0];
}
}
var that = this;
var _getTeam = function(player){
for (var teamName in that.teams){
var team = that.teams[teamName];
for (var i = 0;i < team.length; i++){
if (team[i] == player.name)
return teamName;
}
}
return null;
};
//
// 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)
var SnowBallFight = function(teams, duration){};
SnowBallFight.prototype.start = function(){};
(function(){
var _snowBalls = new org.bukkit.inventory.ItemStack(org.bukkit.Material.SNOW_BALL, 64);
/*
setup game
*/
var _startGame = function(gameState){
// don't let game start if already in progress (wait for game to finish)
if (gameState.inProgress)
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
gameState.inProgress = true;
// reset timer
gameState.duration = gameState.originalDuration;
// put all players in survival mode and give them each 200 snowballs
// 64 snowballs for every 30 seconds should be more than enough
for (var i = 30;i < gameState.duration;i+=30)
gameState.ammo.push(_snowBalls);
if (throwersTeam != damageeTeam)
teamScores[throwersTeam]++;
else
teamScores[throwersTeam]--;
}
});
//
// 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); // 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 teamName in gameState.teams){
gameState.teamScores[teamName] = 0;
var team = gameState.teams[teamName];
for (var i = 0;i < team.length;i++)
{
var player = server.getPlayer(team[i]);
gameState.savedModes[player.name] = player.gameMode;
player.gameMode = org.bukkit.GameMode.SURVIVAL;
player.inventory.addItem(gameState.ammo);
}
}
};
/*
end the game
*/
var _endGame = function(gameState){
var scores = [];
for (var tn in gameState.teamScores)
scores.push("Team " + tn + " scored " + gameState.teamScores[tn]);
for (var teamName in gameState.teams) {
var team = gameState.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.gameMode = gameState.savedModes[player.name];
player.inventory.removeItem(gameState.ammo);
player.sendMessage("GAME OVER.");
player.sendMessage(scores);
}
}
var handlerList = org.bukkit.event.entity.EntityDamageByEntityEvent.getHandlerList();
handlerList.unregister(listener);
handlerList.unregister(gameState.listener);
gameState.inProgress = false;
};
new java.lang.Thread(tick).start();
};
/*
get the team the player belongs to
*/
var _getTeam = function(player,pteams) {
for (var teamName in pteams) {
var team = pteams[teamName];
for (var i = 0;i < team.length; i++)
if (team[i] == player.name)
return teamName;
}
return null;
};
/*
construct a new game
*/
var _constructor = function(teams, duration) {
var _gameState = {
teams: teams,
duration: duration,
originalDuration: duration,
inProgress: false,
teamScores: {},
listener: null,
savedModes: {},
ammo: [_snowBalls]
};
//
// allow for teams param to be either {red:['player1','player2'],blue:['player3']} or
// ['player1','player2','player3'] if all players are against each other (no teams)
//
if (teams instanceof Array){
_gameState.teams = {};
for (var i = 0;i < teams.length; i++)
_gameState.teams[teams[i]] = [teams[i]];
}
/*
this function is called every time a player is damaged by another entity/player
*/
var _onSnowballHit = function(l,event){
var snowball = event.damager;
if (!snowball || !(snowball instanceof org.bukkit.entity.Snowball))
return;
var throwersTeam = _getTeam(snowball.shooter,_gameState.teams);
var damageeTeam = _getTeam(event.entity,_gameState.teams);
if (!throwersTeam || !damageeTeam)
return; // thrower/damagee wasn't in game
if (throwersTeam != damageeTeam)
_gameState.teamScores[throwersTeam]++;
else
_gameState.teamScores[throwersTeam]--;
};
return {
start: function() {
_startGame(_gameState);
_gameState.listener = events.on("entity.EntityDamageByEntityEvent",_onSnowballHit);
new java.lang.Thread(function(){
while (_gameState.duration--)
java.lang.Thread.sleep(1000); // sleep 1,000 millisecs (1 second)
_endGame(_gameState);
}).start();
}
};
};
SnowBallFight = _constructor;
}());