-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
173 lines (139 loc) · 5.68 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function Game(players, turns, turnInterval, actionInterval, speechInterval, varience, playerTotalLife){
//this.constructor.awesomeness++;
// ************************************************************************
// PRIVATE VARIABLES AND FUNCTIONS
// ONLY PRIVELEGED METHODS MAY VIEW/EDIT/INVOKE
// ***********************************************************************
//create a global [within this scope] reference to this
var self = this;
//var myName=n?n:"John Doe";
//function makeOlder(){ return alive = (++age <= maxAge) }
// ************************************************************************
// PRIVILEGED METHODS
// MAY BE INVOKED PUBLICLY AND MAY ACCESS PRIVATE ITEMS
// MAY NOT BE CHANGED; MAY BE REPLACED WITH PUBLIC FLAVORS
// ************************************************************************
this.init = function() {
for (var i = 0; i < this.playersDom.length; i++) {
this.players[i] = new Player(this, this.playersDom[i], varience, playerTotalLife, speechInterval);
this.players[i].init();
}
}
this.start = function() {
update("game--turn-delay", this.turnInterval);
update("game--action-delay", this.actionInterval);
update("game--speech-delay", speechInterval);
update("game--varience-percent", this.varience);
log("*** game started! (" + this.totalPlayers() + " players)");
update("game--total-players", this.totalPlayers());
update("game--active-players", this.alivePlayerCount());
for (var i = 0; i < this.totalTurns; i++) {
if (this.hasEnded()) {
break;
} else {
self.doTurn();
}
}
this.end();
}
this.end = function() {
log("*** game ended!");
stillAlive = this.alivePlayers();
if (stillAlive.length < 1) {
log("*** No one won, everybody died.");
} else if (stillAlive.length < 2) {
log("*** Congrats '" + stillAlive[0].name + "', the sole survivor.");
stillAlive[0].say("I won!");
} else {
//find highest life left
var highScoreIndex = 0;
for (var i = 1; i < stillAlive.length; i++) {
if (stillAlive[i].score() > stillAlive[highScoreIndex].score()) {
highScoreIndex = i;
}
}
log("*** multiple survivors - Congrats '" + stillAlive[highScoreIndex].name + "'.");
stillAlive[highScoreIndex].say("I won!");
}
}
this.hasEnded = function() {
if ((this.currentTurn <= this.totalTurns) && (this.alivePlayerCount() > 1)) {
return false;
} else {
return true;
}
}
this.doTurn = function() {
update("game--turn-number", this.currentTurn);
log("*** turn #" + this.currentTurn);
for (var i = 0; i < this.players.length; i++) {
var player = this.players[i];
if ((player.isDead()) || (this.otherPlayers(player).length == 0))
continue;
this.doPlayerTurn(i);
}
update("game--active-players", this.alivePlayerCount());
this.nextTurn();
}
this.doPlayerTurn = function(pl) {
var player = this.players[pl];
player.powerUp();
// alert("turn " + this.currentTurn + ") player: " + player.name);
player.attackRandom(this.otherPlayers(player));
player.standBy();
}
this.nextTurn = function() {
this.currentTurn++;
}
this.totalPlayers = function() {
return players.length;
}
this.alivePlayers = function() {
var alivePlayers = [];
for (var i = 0; i < this.players.length; i++) {
if (!this.players[i].isDead())
alivePlayers.push(this.players[i]);
}
return alivePlayers;
}
this.alivePlayerCount = function() {
return this.alivePlayers().length;
}
this.otherPlayers = function(player) {
for (var i = 0; i < this.alivePlayers().length; i++) {
if (this.alivePlayers()[i].name == player.name)
return removeElementFromArray(this.alivePlayers(), i);
}
}
this.clearSpeech = function() {
for (var i = 0; i < this.players.length; i++) {
this.players[i].clearSay();
}
}
//this.toString=this.getName=function(){ return myName }
//this.eat=function(){
//}
// ************************************************************************
// PUBLIC PROPERTIES -- ANYONE MAY READ/WRITE
// ************************************************************************
this.actionInterval = actionInterval ? actionInterval : 100;
this.turnInterval = turnInterval ? turnInterval : 600;
this.totalTurns = turns ? turns : 10;
this.currentTurn = 1;
this.playersDom = players;
this.players = [];
this.varience = varience;
//this.attribute = "some-default";
};
// ************************************************************************
// PUBLIC METHODS -- ANYONE MAY READ/WRITE
// ************************************************************************
//Thing.prototype.beAwesome = function(){ this.attribute = "AWESOME!" }
// ************************************************************************
// PROTOTYOPE PROERTIES -- ANYONE MAY READ/WRITE (but may be overridden)
// ************************************************************************
//Thing.prototype.legs = 2;
// ************************************************************************
// STATIC PROPERTIES -- ANYONE MAY READ/WRITE
// ************************************************************************
//Thing.awesomeness = 0;