Created
March 21, 2012 20:39
-
-
Save DavidAntaramian/2152674 to your computer and use it in GitHub Desktop.
This is the current code for a JavaScript module that I'm working on. It is supposed to create a singleton named Parousia.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parousia.minion = {}; | |
(function(instance) { | |
var module = instance || {}; | |
module.unit = function() { | |
/* | |
* Graphical stats: | |
*/ | |
this.id = 0; | |
this.pixelx = 0; | |
this.pixely = 0; | |
this.tilex = 0; | |
this.tiley = 0; | |
/* | |
* the next tile on the unit's path somewhere | |
*/ | |
this.enRouteX = -1; | |
this.enRouteY = -1; | |
/* | |
* -1 means they're going nowhere | |
*/ | |
this.destinationX = -1; | |
this.destinationY = -1; | |
this.color = "rgba(153, 153, 255, 1)"; | |
this.radius = 7; | |
/* | |
* game stats specific to the instance of the creature | |
*/ | |
this.typeID = 2; | |
/* | |
* The player who controls the creature | |
*/ | |
this.team = 0; | |
/* | |
* 1 is alive | |
*/ | |
this.alive = 1; | |
/* | |
* If the creature is on the battlefield yet or not. 1 if yes, 0 if not. | |
* Defaults to 0 | |
*/ | |
this.onField = 0; | |
/* | |
* array of buffs this creature has game stats from creatureType -- | |
* unitType contains the base stats. After they go here, they can be | |
* changed by various buffs in battle. To reference the base stats for | |
* whatever reason, just use creatureType[creature[i].typeID].stat this | |
* "creatureType[this.typeID].ap;" thing doesn't seem to work. Have to | |
* set this in init() because this stuff is all defined before the | |
* creatures are created in their array. | |
*/ | |
this.buffs = new Array(); | |
this.ap = creatureType[this.typeID].ap; | |
this.hp = creatureType[this.typeID].hp; | |
this.atk = creatureType[this.typeID].atk; | |
this.def = creatureType[this.typeID].def; | |
this.attackCost = creatureType[this.typeID].attackCost; | |
/* | |
* ap cost of moving this.abilities = 0 | |
*/ | |
this.movementCost = creatureType[this.typeID].movementCost; | |
/* | |
* this.draw = function(){ var drawFRONT = | |
* $('drawArea').getContext("2d"); drawFRONT.fillStyle = this.color; | |
* drawFRONT.beginPath(); drawFRONT.arc(this.pixelx, this.pixely, | |
* this.radius, 0, Math.PI * 2, true); drawFRONT.closePath(); | |
* drawFRONT.fill(); } | |
*/ | |
this.draw = function() { // experimental sprite use function | |
if (this.alive == 1) { // if alive | |
var sprite = new Image(); | |
sprite.src = 'http://chunkofwhat.com/games/Parousia/sprites/' | |
+ creatureType[this.typeID].name + '.gif'; | |
var drawFRONT = $('drawArea').getContext("2d"); | |
drawFRONT.drawImage(sprite, this.pixelx - 13, this.pixely - 22) | |
} else { // if dead | |
var sprite = new Image(); | |
sprite.src = 'http://chunkofwhat.com/games/Parousia/sprites/rip.gif'; | |
var drawFRONT = $('drawArea').getContext("2d"); | |
drawFRONT.drawImage(sprite, this.pixelx - 13, this.pixely - 22) | |
} | |
} | |
this.attack = function(x, y) { | |
creature[grid[x][y].inhabitant].hp -= this.atk; | |
} | |
this.ability = function(x, y, abilityID) { | |
console.log(abilityID); | |
/* | |
* adds the ability's buff to the target creature's list of buffs | |
*/ | |
creature[grid[x][y].inhabitant].buffs | |
.push(abilityType[abilityID].buff); | |
} | |
} | |
return module; | |
}(minion)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Notice: This file utilizes many of the tips provided at | |
* http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth and | |
* http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/ | |
* | |
* The way this works is by creating an anonymous function that takes the | |
* argument "instance" which is the variable that holds the singleton of the | |
* entire module. This singleton can than be referred to throughout the | |
* application. --(DEA) | |
* | |
*/ | |
var Parousia = {}; | |
(function (instance) { | |
var module = instance || {}; | |
module.CANVASHEIGHT = 500; | |
module.CANVASWIDTH = 500; | |
module.GRIDHEIGHT = 25; | |
module.GRIDWIDTH = 25; | |
module.SCALE = 20; | |
// spawnpoints | |
module.SPAWN_A1X = 3; | |
module.SPAWN_A1Y = 3; | |
module.SPAWN_A2X = 8; | |
module.SPAWN_A2Y = 3; | |
module.SPAWN_A3X = 3; | |
module.SPAWN_A3Y = 8; | |
module.SPAWN_B1X = module.GRIDWIDTH - 4; | |
module.SPAWN_B1Y = module.GRIDHEIGHT - 4; | |
module.SPAWN_B2X = module.GRIDWIDTH - 9; | |
module.SPAWN_B2Y = module.GRIDHEIGHT - 4; | |
module.SPAWN_B3X = module.GRIDWIDTH - 4; | |
module.SPAWN_B3Y = module.GRIDHEIGHT - 9; | |
// controls | |
module.rightDown = false; | |
module.leftDown = false; | |
module.upDown = false; | |
module.downDown = false; | |
module.spaceDown = false; | |
module.shiftDown = false; | |
module.clicky = false; | |
// mouse position | |
module.mx = 10; | |
module.my = 10; | |
module.mouseTileX = 0; // selected tile | |
module.mouseTileY = 0; | |
module.MOUSEDOWHAT = "null"; | |
// arrow key positioning | |
module.kx = 110; | |
module.ky = 110; | |
module.grid = new Array(module.GRIDWIDTH); | |
module.selectedX = 0; // tile that has been selected by mouse | |
module.selectedY = 0; | |
module.BRUSHSELECT = "n"; // the brush currently selected to paint with | |
module.EDITMODE = false; | |
module.LOADDATA = ""; | |
module.TURN = 1; | |
/* | |
* number of points Blue player has to spend on summoning creatures. | |
* Innitially 3. Get 1 with each spawnpoint capture. | |
*/ | |
module.PLAYERA_SPAWN_TOLKENS = 0; | |
/* | |
* number of points Pink player has to spend on summoning creatures. | |
* Innitially 3. Get 1 with each spawnpoint capture. | |
*/ | |
module.PLAYERB_SPAWN_TOLKENS = 0; | |
// unit stuff | |
module.numPlayaz = 12; // counts from 1 instead of 0. Creature 0 is default, | |
// does not exist, is null. | |
module.creature = new Array(); | |
for ( var i = 1; i <= module.numPlayaz; i++) { | |
module.creature[i] = new Parousia.Unit(); | |
module.creature[i].id = i; | |
} | |
/* | |
* all of the below defining of creatures will eventually come from the | |
* server instead. Will be this way until accounts figured out. | |
*/ | |
// Blue Team's on-field creatures | |
module.creature[1].tilex = 3; | |
module.creature[1].tiley = 3; | |
module.creature[1].team = 1; | |
module.creature[1].typeID = 1; | |
module.creature[1].onField = 1; | |
module.creature[2].tilex = 8; | |
module.creature[2].tiley = 3; | |
module.creature[2].team = 1; | |
module.creature[2].typeID = 2; | |
module.creature[2].onField = 1; | |
module.creature[3].tilex = 3; | |
module.creature[3].tiley = 8; | |
module.creature[3].team = 1; | |
module.creature[3].typeID = 3; | |
module.creature[3].onField = 1; | |
// off-field Blue creatures | |
module.creature[4].team = 1; | |
module.creature[4].typeID = 1; | |
module.creature[4].onField = 0; | |
module.creature[5].team = 1; | |
module.creature[5].typeID = 2; | |
module.creature[5].onField = 0; | |
creature[6].team = 1; | |
creature[6].typeID = 3; | |
creature[6].onField = 0; | |
// Pink Team on-field creatures | |
module.creature[7].tilex = module.GRIDWIDTH - 4; | |
module.creature[7].tiley = module.GRIDHEIGHT - 4; | |
module.creature[7].team = 2; | |
module.creature[7].typeID = 1; | |
module.creature[7].onField = 1; | |
module.creature[8].tilex = module.GRIDWIDTH - 9; | |
module.creature[8].tiley = module.GRIDHEIGHT - 4; | |
module.creature[8].team = 2; | |
module.creature[8].typeID = 2; | |
module.creature[8].onField = 1; | |
module.creature[9].tilex = module.GRIDWIDTH - 4; | |
module.creature[9].tiley = module.GRIDHEIGHT - 9; | |
module.creature[9].color = "rgba(255, 105, 180, 1)"; | |
module.creature[9].team = 2; | |
module.creature[9].typeID = 3; | |
module.creature[9].onField = 1; | |
// off-field Blue creatures | |
module.creature[10].team = 2; | |
module.creature[10].typeID = 1; | |
module.creature[10].onField = 0; | |
module.creature[11].team = 2; | |
module.creature[11].typeID = 2; | |
module.creature[11].onField = 0; | |
module.creature[12].team = 2; | |
module.creature[12].typeID = 3; | |
module.creature[12].onField = 0; | |
for ( var i = 1; i <= module.numPlayaz; i++) { | |
module.creature[i].enRouteX = module.creature[i].tilex | |
module.creature[i].enRouteY = module.creature[i].tiley | |
} | |
module.randomFromTo = function(from, to) { | |
return Math.floor(Math.random() * (to - from + 1) + from); | |
} | |
module.init = function() { | |
document.onmousemove = mouseMoved; | |
document.onmousedown = mouseDownified; | |
document.onmouseup = mouseUpified; | |
document.onkeydown = onKeyDown; | |
document.onkeyup = onKeyUp; | |
module.drawFRONT = module.$('drawArea').getContext("2d"); | |
module.drawBACK = module.$('terrainArea').getContext("2d"); | |
for ( var x = 0; x < module.GRIDWIDTH; x++) { | |
module.grid[x] = new Array(module.GRIDHEIGHT); | |
} | |
for ( var y = 0; y < module.GRIDHEIGHT; y++) { | |
for ( var x = 0; x < module.GRIDWIDTH; x++) { | |
var t = new Tile(); | |
var pos = getPos(x, y); | |
t.x = pos[0]; | |
t.y = pos[1]; | |
module.grid[x][y] = t; | |
} | |
} | |
/* | |
* the creature[] array has been created, and the creatures have been | |
* put on the canvas, but their creatureType stats haven't updated yet: | |
*/ | |
for ( var i = 1; i <= module.numPlayaz; i++) { | |
module.creature[i].ap = creatureType[module.creature[i].typeID].ap; | |
module.creature[i].hp = creatureType[module.creature[i].typeID].hp; | |
module.creature[i].atk = creatureType[module.creature[i].typeID].atk; | |
module.creature[i].def = creatureType[module.creature[i].typeID].def; | |
module.creature[i].attackCost = creatureType[module.creature[i].typeID].attackCost; | |
module.creature[i].movementCost = creatureType[module.creature[i].typeID].movementCost; | |
module.creature[i].abilities = creatureType[module.creature[i].typeID].abilities; | |
} | |
// setting the spawnpointss | |
module.grid[module.SPAWN_A1X][module.SPAWN_A1Y].is_spawn = 1; | |
module.grid[module.SPAWN_A1X][module.SPAWN_A1Y].team = 1; | |
module.grid[module.SPAWN_A2X][module.SPAWN_A2Y].is_spawn = 1; | |
module.grid[module.SPAWN_A2X][module.SPAWN_A2Y].team = 1; | |
module.grid[module.SPAWN_A3X][module.SPAWN_A3Y].is_spawn = 1; | |
module.grid[module.SPAWN_A3Y][module.SPAWN_A3Y].team = 1; | |
module.grid[module.SPAWN_B1X][module.SPAWN_B1Y].is_spawn = 1; | |
module.grid[module.SPAWN_B1X][module.SPAWN_B1Y].team = 2; | |
module.grid[module.SPAWN_B2X][module.SPAWN_B2Y].is_spawn = 1; | |
module.grid[module.SPAWN_B2X][module.SPAWN_B2Y].team = 2; | |
module.grid[module.SPAWN_B3X][module.SPAWN_B3Y].is_spawn = 1; | |
module.grid[module.SPAWN_B3Y][module.SPAWN_B3Y].team = 2; | |
module.drawTerrain(); | |
/* | |
* for(var i = 0; i < 600; i++) { console.log(Math.round((i - (SCALE / | |
* 2))/SCALE)); } | |
*/ | |
module.load(); | |
return module.setInterval(draw, 1); | |
}; | |
module.drawTerrain = function() { | |
var drawBACK = module.$('terrainArea').getContext("2d"); | |
for ( var y = 0; y < module.GRIDHEIGHT; y++) { | |
for ( var x = 0; x < module.GRIDWIDTH; x++) { | |
drawBACK.strokeRect(module.grid[x][y].x,module. grid[x][y].y, module.SCALE, module.SCALE); | |
} | |
} | |
/* | |
* telling the creatures what their pixel locations are. NOT needed for | |
* drawing the creatures, because that is done by the tiles. This is for | |
* movement information. | |
*/ | |
for ( var i = 1; i <= module.numPlayaz; i++) { | |
module.creature[i].pixelx = module.grid[module.creature[i].tilex][module.creature[i].tiley].x | |
+ module.SCALE * .5; | |
module.creature[i].pixely = module.grid[module.creature[i].tilex][module.creature[i].tiley].y | |
+ module.SCALE * .5; | |
} | |
} | |
module.draw = function() { | |
var drawFRONT = module.$('drawArea').getContext("2d"); | |
var drawBACK = module.$('terrainArea').getContext("2d"); | |
drawFRONT.clearRect(0, 0, CANVASHEIGHT, CANVASWIDTH); | |
module.mouseTileX = module.pixel2tile(mx); | |
module.mouseTileY = module.pixel2tile(my); | |
/* | |
* Highlight the attack tiles but only if you own the creature. | |
*/ | |
if (module.MOUSEDOWHAT == "attack" | |
&& module.grid[module.selectedX][module.selectedY].inhabitant != 0) { | |
if (module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].team == module.TURN) { | |
module.grid[module.selectedX + 0][module.selectedY - 1] | |
.highlight("rgba(180, 0, 0, .3)"); // north | |
module.grid[module.selectedX + 1][module.selectedY + 0] | |
.highlight("rgba(180, 0, 0, .3)"); // east | |
module.grid[module.selectedX + 0][module.selectedY + 1] | |
.highlight("rgba(180, 0, 0, .3)"); // south | |
module.grid[module.selectedX - 1][module.selectedY + 0] | |
.highlight("rgba(180, 0, 0, .3)"); // west | |
} | |
} | |
/* | |
* // failed attempt at movement highlight: //need to take the ap | |
* available and divy it between x or y in every combination. so if | |
* ap=10: (X+10,y+0) (X+9,y+1) (X+8,y+2) etc if (MOUSEDOWHAT == "move" && | |
* grid[selectedX][selectedY].inhabitant != 0){//highlight the moveable | |
* tiles if(creature[grid[selectedX][selectedY].inhabitant].team == | |
* TURN){ //but only if you own the creature var apDirectional = | |
* (creature[grid[selectedX][selectedY].inhabitant].ap)/2; //ap to be | |
* split between x and y //console.log(apDirectional); for(var i=0;i<apDirectional;i++){ | |
* console.log(i); if (selectedX+i < GRIDWIDTH && selectedY-i < | |
* GRIDHEIGHT && selectedX+i >= 0 && selectedY-i >= 0) //if the place to | |
* be highlighted is on the canvas | |
* grid[selectedX+i][selectedY-i].highlight(); // if ap is 10, initially | |
* will be (10,0), then (9,1),(8,2)...(1,9),(0,10) } } } } | |
*/ | |
if (module.mouseTileX < module.GRIDWIDTH && module.mouseTileY < module.GRIDHEIGHT | |
&& module.mouseTileX >= 0 && module.mouseTileY >= 0) { | |
module.grid[module.mouseTileX][module.mouseTileY].highlight("rgba(255, 255, 0, .5)"); | |
} | |
if (module.TURN == 1) { | |
// if blue's turn, make their cursor blue | |
module.grid[module.selectedX][module.selectedY].highlight("rgba(100, 50, 200, .5)"); | |
} | |
if (module.TURN == 2) { | |
// if pink's turn, make their cursor pink | |
module.grid[module.selectedX][module.selectedY].highlight("rgba(255, 105, 180, .5)"); | |
} | |
if (module.EDITMODE == false) { | |
// if it's not in edit mode, then you can play the game | |
if (module.mouseTileX < module.GRIDWIDTH && module.mouseTileY < module.GRIDHEIGHT | |
&& module.clicky == true) { | |
if (module.MOUSEDOWHAT != "attack" && module.MOUSEDOWHAT == "null") { | |
module.selectedX = module.mouseTileX; | |
module.selectedY = module.mouseTileY; | |
} | |
// if the mouse is in move mode | |
if (module.MOUSEDOWHAT == "move") { | |
if (module.grid[module.selectedX][module.selectedY].inhabitant != 0 | |
&& module.grid[module.mouseTileX][module.mouseTileY].inhabitant == 0) { | |
/* | |
* If the previously selected tile has a creature, move | |
* it to the currently highlighted tile. If the | |
* currently selected tile has a creature on it already, | |
* move on to the else statement below, slectint the | |
* newly clicked on creature instead. | |
*/ | |
if (module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].team == module.TURN) { | |
/* | |
* If the selected unit belongs to the player whose | |
* turn it is | |
*/ | |
if (module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].ap >= module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].movementCost) { | |
module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].destinationX = module.mouseTileX; | |
module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].destinationY = module.mouseTileY; | |
module.MOUSEDOWHAT = "null"; | |
} else { | |
/* | |
* if you don't have the ap to move | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
/* | |
* tells the old tile no one's on it anymore | |
*/ | |
module.grid[module.selectedX][module.selectedY].inhabitant = 0; | |
} else { | |
/* | |
* If you don't own the thing you're trying to move | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} else { | |
/* | |
* If you're trying to move with a tile has no creature | |
* on it, stop. | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} | |
if (module.MOUSEDOWHAT == "attack") { | |
if (module.grid[module.selectedX][module.selectedY].inhabitant != 0 | |
&& module.grid[module.mouseTileX][module.mouseTileY].inhabitant != 0) { | |
/* | |
* If the previously selected tile has a creature, move | |
* it to the currently highlighted tile. If the | |
* currently selected tile has a creature on it already, | |
* move on to the else statement below, selectint the | |
* newly clicked on creature instead. | |
*/ | |
if (module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].team == module.TURN) { | |
/* | |
* If the selected unti belongs to the player whose | |
* turn it is | |
*/ | |
if (module.grid[module.mouseTileX][module.mouseTileY] == module.grid[module.selectedX + 0][module.selectedY - 1] | |
|| // north // if the creature player is | |
// attempt6ing to attack is in range | |
module.grid[module.mouseTileX][module.mouseTileY] == module.grid[module.selectedX + 1][module.selectedY + 0] | |
|| // east | |
module.grid[module.mouseTileX][module.mouseTileY] == module.grid[module.selectedX + 0][module.selectedY + 1] | |
|| // south | |
module.grid[module.mouseTileX][module.mouseTileY] == module.grid[module.selectedX - 1][module.selectedY + 0]) { // west | |
/* | |
* ap cost of attack | |
*/ | |
if (module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].ap >= module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].attackCost) { | |
module.creature[module.grid[module.selectedX][module.selectedY].inhabitant] | |
.attack(module.mouseTileX, module.mouseTileY); | |
module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].ap -= module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].attackCost; | |
module.MOUSEDOWHAT = "null"; | |
} else { | |
/* | |
* if you don't have the AP to attack | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} else { | |
/* | |
* if you suck and selected something not in | |
* range, unselect creature | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} else { | |
/* | |
* if it doesn't belong to you you can't attack with | |
* it | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} else { | |
/* | |
* if you have a tile selected that has no one on it, | |
* you can't attack with it obviously | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} | |
if (module.MOUSEDOWHAT == "ability") { | |
if (module.grid[module.selectedX][module.selectedY].inhabitant != 0 | |
&& module.grid[module.mouseTileX][module.mouseTileY].inhabitant != 0) { | |
/* | |
* If the previously selected tile has a creature, move | |
* it to the currently highlighted tile. If the | |
* currently selected tile has a creature on it already, | |
* move on to the else statement below, selectint the | |
* newly clicked on creature instead. | |
*/ | |
if (module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].team == module.TURN) { | |
/* | |
* If the selected unit belongs to the player whose | |
* turn it is | |
*/ | |
if (module.grid[module.mouseTileX][module.mouseTileY] == module.grid[module.selectedX + 0][module.selectedY - 1] | |
|| // north // if the creature player is | |
// attempt6ing to attack is in range | |
module.grid[module.mouseTileX][module.mouseTileY] == module.grid[module.selectedX + 1][module.selectedY + 0] | |
|| // east | |
module.grid[module.mouseTileX][module.mouseTileY] == module.grid[module.selectedX + 0][module.selectedY + 1] | |
|| // south | |
module.grid[module.mouseTileX][module.mouseTileY] == module.grid[module.selectedX - 1][module.selectedY + 0]) { // west | |
/* | |
* ap cost of attack | |
*/ | |
if (creature[grid[selectedX][selectedY].inhabitant].ap >= creatureType[creature[grid[selectedX][selectedY].inhabitant].typeID].attackCost) { | |
creature[grid[selectedX][selectedY].inhabitant] | |
.ability( | |
mouseTileX, | |
mouseTileY, | |
creatureType[creature[grid[selectedX][selectedY].inhabitant].typeID].ability1); | |
creature[grid[selectedX][selectedY].inhabitant].ap -= creatureType[creature[grid[selectedX][selectedY].inhabitant].typeID].apCost; | |
/* | |
* Now that someone's had an ability used on | |
* them, update their buffs | |
*/ | |
module.applyBuff(module.creature[module.grid[module.mouseTileX][module.mouseTileY].inhabitant].id); | |
module.MOUSEDOWHAT = "null"; | |
} else { | |
/* | |
* if you don't have the AP to attack | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} else { | |
/* | |
* if you suck and selected something not in | |
* range, unselect creature | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} else { | |
/* | |
* if it doesn't belong to you you can't attack with | |
* it | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} else { | |
/* | |
* if you have a tile selected that has no one on it, | |
* you can't attack with it obviously | |
*/ | |
module.MOUSEDOWHAT = "null"; | |
module.selectedX = 0; | |
module.selectedY = 0; | |
} | |
} | |
} | |
} | |
if (module.EDITMODE == true) { | |
if (module.mouseTileX < module.GRIDWIDTH && module.mouseTileY < module.GRIDHEIGHT | |
&& module.clicky == true) { | |
if (module.shiftDown) { | |
module.grid[module.mouseTileX][module.mouseTileY].clear(); | |
} else { | |
module.grid[module.mouseTileX][module.mouseTileY].setType(module.BRUSHSELECT); | |
} | |
} | |
} | |
for ( var i = 1; i <= module.numPlayaz; i++) { | |
/* | |
* If the creature is on the field | |
*/ | |
if (module.creature[i].onField == 1) { | |
/* | |
* dead creature can't be drawn | |
*/ | |
if (module.creature[i].hp > 0) { | |
if (module.creature[i].destinationX >= 0 | |
&& module.creature[i].destinationY >= 0 | |
&& module.creature[i].ap >= 1) { | |
/* | |
* if it has a destination takes the final destination | |
* (destinationX&Y) and uses pathfinding to figure out | |
* which tiles to go to first (enRoutX&Y) on way there. | |
* This routine is really really fucking slow. Maybe try | |
* changing "x+1" etc to a numeric key, so "x+1" = 1, | |
* "x-1" = 2, "y+1" =3, "y-1" = 4* | |
*/ | |
var direction = module.pathfind(module.creature[i].tilex, | |
module.creature[i].tiley, module.creature[i].destinationX, | |
module.creature[i].destinationY); | |
/* | |
* pathfind tells the creature the next tile it needs to | |
* go to on its way to its final destination. Prints out | |
* something like "x+1" | |
*/ | |
if (direction == "x+1") { | |
module.creature[i].enRouteX = module.creature[i].tilex + 1; | |
module.grid[module.creature[i].tilex][module.creature[i].tiley].inhabitant = 0; | |
module.startWalken(i, module.creature[i].enRouteX, | |
module.creature[i].enRouteY); | |
} else if (direction == "x-1") { | |
module.creature[i].enRouteX = module.creature[i].tilex - 1; | |
module.grid[module.creature[i].tilex][module.creature[i].tiley].inhabitant = 0; | |
module.startWalken(i, module.creature[i].enRouteX, | |
module.creature[i].enRouteY); | |
} else if (direction == "y+1") { | |
module.creature[i].enRouteY = module.creature[i].tiley + 1; | |
module.grid[creature[i].tilex][creature[i].tiley].inhabitant = 0; | |
module.startWalken(i, module.creature[i].enRouteX, | |
module.creature[i].enRouteY); | |
} else if (direction == "y-1") { | |
module.creature[i].enRouteY = module.creature[i].tiley - 1; | |
module.grid[module.creature[i].tilex][module.creature[i].tiley].inhabitant = 0; | |
module.startWalken(i, module.creature[i].enRouteX, | |
module.creature[i].enRouteY); | |
} | |
} | |
if (module.creature[i].destinationX == module.creature[i].tilex) { | |
// creature[i].destinationX = 0; | |
} | |
if (module.creature[i].destinationY == module.creature[i].tiley) { | |
// creature[i].destinationY = 0; | |
} | |
module.creature[i].draw(); | |
// creature[i].tilex = pixel2tile(creature[i].pixelx); way | |
// too slow | |
if (module.creature[i].tilex >= 0 && module.creature[i].tiley >= 0) { | |
module.grid[module.creature[i].tilex][module.creature[i].tiley].inhabitant = i; | |
/* | |
* Tell the tile that the creature is inhabiting it. | |
* Faster than going to every tile to check if it has a | |
* creature. Currently, I have to do this too anyways, | |
* just to tell them they're empty. | |
*/ | |
} | |
/* | |
* Detecting if spawnpoint captured | |
*/ | |
/* | |
* If the creature is blue team | |
*/ | |
if (module.creature[i].team == 1) { | |
/* | |
* and if that blue team creature is on a pink | |
* spawnpoint | |
*/ | |
if (module.grid[module.creature[i].tilex][module.creature[i].tiley].tileType == "ps") { | |
/* | |
* Kill that spawnpoint | |
*/ | |
module.grid[module.creature[i].tilex][module.creature[i].tiley] | |
.setType("ds"); | |
} | |
} | |
/* | |
* If the creature is out of AP, set the destination to the | |
* current location so it doesn't try to continue going | |
* there next turn | |
*/ | |
if (module.creature[i].ap < creatureType[module.creature[i].typeID].movementCost) { | |
module.creature[i].destinationX = module.creature[i].tilex; | |
module.creature[i].destinationY = module.creature[i].tiley; | |
} | |
} else { | |
/* | |
* the creature is dead. Do dead stuff | |
*/ | |
module.creature[i].alive = 0; | |
// creature[i].typeID = 0; | |
module.creature[i].draw(); | |
} | |
/* | |
* If on the battlefield | |
*/ | |
} else { | |
/* | |
* where should a creature be if it's not on the battlefield? | |
* Will this create problems? | |
*/ | |
} | |
} | |
/* | |
* display for off-field creatures | |
*/ | |
/* | |
* victory conditions. TODO: Shoddily done. Will obviously be done | |
* differently | |
*/ | |
if (module.creature[1].alive == 0 && module.creature[2].alive == 0 | |
&& module.creature[3].alive == 0) { | |
drawFRONT.strokeText("PINK VICTORY", (module.CANVASWIDTH / 2) - 20, | |
(Cmodule.ANVASHEIGHT / 2) - 2); | |
drawFRONT.strokeText("BLUE DEFEAT", (CANVASWIDTH / 2) - 20, | |
(CANVASHEIGHT / 2) + 8); | |
} | |
if (module.creature[4].alive == 0 && module.creature[5].alive == 0 | |
&& module.creature[6].alive == 0) { | |
drawFRONT.strokeText("BLUE VICTORY", (CANVASWIDTH / 2) - 20, | |
(CANVASHEIGHT / 2) - 2); | |
drawFRONT.strokeText("PINK DEFEAT", (CANVASWIDTH / 2) - 20, | |
(CANVASHEIGHT / 2) + 8); | |
} | |
/* | |
* Clear it first, or else it'll add endlessly! | |
*/ | |
module.$("creatures").innerHTML = ""; | |
for ( var i = 1; i <= module.numPlayaz; i++) { | |
/* | |
* TODO: Could combine this with the other cycle through creatures | |
* thing above...maybe do that later? | |
*/ | |
/* | |
* If the creature belongs to the player whose turn it is | |
*/ | |
if (module.creature[i].team == module.TURN) { | |
/* | |
* if the creature is alive and on the field | |
*/ | |
if (module.creature[i].alive == 1 && module.creature[i].onField == 1) { | |
module.$("creatures").innerHTML += "<font color=\"green\">" | |
+ creatureType[creature[i].typeID].name | |
+ "</font> (alive & onfield)<br/>"; | |
} | |
/* | |
* if the creature is off the field | |
*/ | |
if (module.creature[i].alive == 1 && module.creature[i].onField == 0) { | |
module.$("creatures").innerHTML += "<font color=\"blue\">" | |
+ creatureType[module.creature[i].typeID].name | |
+ "</font> (alive & offfield)<br/>"; | |
} | |
/* | |
* if the creature is dead | |
*/ | |
if (module.creature[i].alive == 0 && module.creature[i].onField == 1) { | |
module.$("creatures").innerHTML += "<font color=\"red\">" | |
+ creatureType[module.creature[i].typeID].name | |
+ "</font> (dead)<br/>"; | |
} | |
} | |
} | |
/* | |
* Print the team color | |
*/ | |
if (TURN == 1) { | |
module.$("TURN").innerHTML = "BLUE"; | |
} else if (TURN == 2) { | |
module.$("TURN").innerHTML = "PINK"; | |
} | |
if (module.grid[module.selectedX][module.selectedY].inhabitant != 0) { | |
module.$("id").innerHTML = module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].id; | |
module.$("team").innerHTML = module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].team; | |
/* | |
* $("tileX").innerHTML= | |
* creature[grid[selectedX][selectedY].inhabitant].tilex; | |
* $("tileY").innerHTML= | |
* creature[grid[selectedX][selectedY].inhabitant].tiley; | |
* $("enRouteX").innerHTML= | |
* creature[grid[selectedX][selectedY].inhabitant].destinationX; | |
* $("enRouteY").innerHTML= | |
* creature[grid[selectedX][selectedY].inhabitant].destinationY; | |
*/ | |
module.$("AP").innerHTML = module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].ap; | |
module.$("HP").innerHTML = module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].hp; | |
module.$("type").innerHTML = creatureType[module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].typeID].name; | |
module.$("atk").innerHTML = module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].atk; | |
module.$("def").innerHTML = module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].def; | |
// prints the attack cost in the attack button | |
module.$("attackButton").value = "Attack (" | |
+ module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].attackCost | |
+ "ap) " | |
+ module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].atk | |
+ " damage"; | |
// prints the move cost in the move button | |
module.$("moveButton").value = "Move (" | |
+ module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].movementCost | |
+ "ap)"; | |
module.$("buffs").innerHTML = module.creature[module.grid[module.selectedX][module.selectedY].inhabitant].buffs; | |
} | |
if (module.mouseTileX < module.GRIDWIDTH && module.mouseTileY < module.GRIDHEIGHT | |
&& module.mouseTileX >= 0 && module.mouseTileY >= 0) { | |
module.$("printsomething").innerHTML = grid[mouseTileX][mouseTileY].inhabitant; | |
} | |
module.$("brushType").innerHTML = module.BRUSHSELECT; | |
module.$("selectedxprint").innerHTML = module.selectedX; | |
module.$("selectedyprint").innerHTML = module.selectedY; | |
} | |
module.startWalken = function(unitID, destTileX, destTileY) { | |
// console.log(unitID + "," + destTileX + "," + destTileY); | |
var destPixelX = module.grid[destTileX][destTileY].x; | |
var destPixelY = module.grid[destTileX][destTileY].y; | |
// if North | |
if (module.creature[unitID].pixely < destPixelY + module.SCALE * .5) { | |
module.creature[unitID].pixely = module.creature[unitID].pixely + 1; | |
module.creature[unitID].draw(); | |
/* | |
* When the creature arrives at tile destination in pixels make the | |
* the creature's tile | |
*/ | |
if (module.creature[unitID].pixely == destPixelY + module.SCALE * .5) { | |
module.creature[unitID].tiley = destTileY; | |
module.creature[unitID].ap -= module.creature[unitID].movementCost; | |
} | |
} | |
// if East | |
if (module.creature[unitID].pixelx < destPixelX + module.SCALE * .5) { | |
module.creature[unitID].pixelx = module.creature[unitID].pixelx + 1; | |
module.creature[unitID].draw(); | |
/* | |
* When the creature arrvies at tile de | |
*/ | |
if (module.creature[unitID].pixelx == destPixelX + module.SCALE * .5) { | |
/* | |
* When the creature arrives at tile destination in pixels make | |
* the the creature's tile | |
*/ | |
module.creature[unitID].tilex = destTileX; | |
module.creature[unitID].ap -= module.creature[unitID].movementCost; | |
} | |
} | |
// if South | |
if (module.creature[unitID].pixely > destPixelY + module.SCALE * .5) { | |
module.creature[unitID].pixely = module.creature[unitID].pixely - 1; | |
module.creature[unitID].draw(); | |
if (module.creature[unitID].pixely == destPixelY + module.SCALE * .5) { | |
/* | |
* When the creature arrives at tile destination in pixels make | |
* the the creature's tile | |
*/ | |
module.creature[unitID].tiley = destTileY; | |
module.creature[unitID].ap -= module.creature[unitID].movementCost; | |
} | |
} | |
// if West | |
if (module.creature[unitID].pixelx > destPixelX + module.SCALE * .5) { | |
module.creature[unitID].pixelx = module.creature[unitID].pixelx - 1; | |
module.creature[unitID].draw(); | |
if (module.creature[unitID].pixelx == destPixelX + module.SCALE * .5) { | |
/* | |
* When the creature arrives at tile destination in pixels make | |
* the the creature's tile | |
*/ | |
module.creature[unitID].tilex = destTileX; | |
module.creature[unitID].ap -= module.creature[unitID].movementCost; | |
} | |
} | |
} | |
module.pathfind = function(startX, startY, endX, endY) { | |
// if X first | |
if (Math.abs(startX - endX) > Math.abs(startY - endY)) { | |
if (startX - endX < 0) { | |
return "x+1"; | |
} | |
if (startX - endX > 0) { | |
return "x-1"; | |
} | |
// if Y first | |
} else if (Math.abs(startX - endX) < Math.abs(startY - endY)) { | |
if (startY - endY < 0) { | |
return "y+1"; | |
} | |
if (startY - endY > 0) { | |
return "y-1"; | |
} | |
/* | |
* If the distance to travel in xs and ys is equal just start | |
* working on the x | |
*/ | |
} else if (Math.abs(startX - endX) == Math.abs(startY - endY) | |
&& Math.abs(startX - endX) != 0) { | |
if (startX - endX < 0) { | |
return "x+1"; | |
} | |
if (startX - endX > 0) { | |
return "x-1"; | |
} | |
} else { | |
return "0" | |
} | |
} | |
module.$ = function(id) { | |
return document.getElementById(id); | |
} | |
/** | |
* | |
* @param rnum | |
* number to round | |
* @param rlength | |
* number of decimal places | |
*/ | |
module.roundNumber = function(rnum, rlength) { | |
var newnumber = Math.round(rnum * Math.pow(10, rlength)) | |
/ Math.pow(10, rlength); | |
// parseFloat(newnumber); | |
return newnumber; | |
} | |
/* | |
* Find the distance between two pixels | |
*/ | |
module.pixelDistance = function(x1, y1, x2, y2) { | |
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); | |
} | |
module.pixel2tile = function(banana) { | |
return Math.round((banana - (SCALE / 2)) / SCALE); | |
} | |
/* | |
* Communicating with the pallette buttons in HTML | |
*/ | |
module.palette = function(brushType) { | |
module.BRUSHSELECT = brushType; | |
} | |
module.editMode = function() { | |
module.EDITMODE = true; | |
} | |
module.gameMode = function() { | |
module.EDITMODE = false; | |
} | |
/* | |
* TODO: @Chunkofwhat - Fuck, don't remember what this does, but it's | |
* important. | |
*/ | |
module.getPos = function(x, y) { | |
var ret = new Array(x * module.SCALE, y * module.SCALE); | |
return ret; | |
} | |
module.addUnit = function() { | |
} | |
module.endTurn = function() { | |
if (module.TURN == 2) { // swaps whos turn it is | |
module.TURN = 1; | |
for ( var i = 1; i <= module.numPlayaz; i++) { | |
if (module.creature[i].team == 1) { | |
module.creature[i].ap = creatureType[module.creature[i].typeID].ap; | |
} | |
} | |
} else if (module.TURN == 1) { | |
module.TURN = 2; | |
for ( var i = 1; i <= module.numPlayaz; i++) { | |
if (module.creature[i].team == 2) { | |
module.creature[i].ap = creatureType[module.creature[i].typeID].ap; | |
} | |
} | |
} | |
} | |
module.attackToggle = function() { | |
module.MOUSEDOWHAT = "attack"; | |
} | |
module.moveToggle = function() { | |
module.MOUSEDOWHAT = "move"; | |
} | |
module.abilityToggle = function() { | |
module.MOUSEDOWHAT = "ability"; | |
} | |
module.applyBuff = function(creatureID) { | |
// number of buffs on creature | |
var numBuffs = module.creature[creatureID].buffs.length; | |
for ( var i = 0; i < numBuffs; i++) { | |
var buffID = module.creature[creatureID].buffs[i]; | |
module.creature[creatureID].ap; | |
module.creature[creatureID].hp; | |
module.creature[creatureID].atk; | |
module.creature[creatureID].def = module.creature[creatureID].def | |
* module.buffType[buffID].defMulti + module.buffType[buffID].def; | |
creature[creatureID].attackCost; | |
creature[creatureID].movementCost; | |
} | |
} | |
module.save = function() { | |
var SAVEDATA = "<lev w='" + module.GRIDWIDTH + "'></br>"; | |
for ( var y = 0; y < module.GRIDHEIGHT; y++) { | |
for ( var x = 0; x < module.GRIDWIDTH; x++) { | |
module.SAVEDATA += (module.grid[x][y].tileType + "*"); | |
if (x == module.GRIDWIDTH - 1) { | |
module.SAVEDATA += "<br/>" | |
} | |
} | |
} | |
module.$("saveout").innerHTML = SAVEDATA + "</lev>"; | |
} | |
module.load = function() { | |
// drawFRONT.clearRect(0,0,CANVASHEIGHT,CANVASWIDTH); | |
LOADDATA = module.$("loadin").value; | |
var loadArray = LOADDATA.split("*"); | |
var i = 0; | |
for ( var x = 0; x < module.GRIDWIDTH; x++) { | |
for ( var y = 0; y < module.GRIDHEIGHT; y++) { | |
module.grid[x][y].setType(loadArray[i]); | |
i++; | |
} | |
} | |
// setting spawn zones | |
module.grid[module.SPAWN_A1X][module.SPAWN_A1Y].setType("bs"); | |
module.grid[module.SPAWN_A2X][module.SPAWN_A2Y].setType("bs"); | |
module.grid[module.SPAWN_A3X][module.SPAWN_A3Y].setType("bs"); | |
module.grid[module.SPAWN_B1X][module.SPAWN_B1Y].setType("ps"); | |
module.grid[module.SPAWN_B2X][module.SPAWN_B2Y].setType("ps"); | |
module.grid[module.SPAWN_B3X][module.SPAWN_B3Y].setType("ps"); | |
} | |
}(Parousia)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment