Skip to content

Commit

Permalink
inherited files from canvas.experiment, replaced lucidjs with minimal…
Browse files Browse the repository at this point in the history
… on/fire event system
  • Loading branch information
Armen138 committed Aug 13, 2012
1 parent bef1adf commit 349e249
Show file tree
Hide file tree
Showing 19 changed files with 825 additions and 0 deletions.
Binary file added doc/scribbles_1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/scribbles_2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/scribbles_3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/scribbles_4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/scribbles_5.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/scribbles_6.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/scribbles_7.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions index.html
@@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- script src='js/astar.js'></script -->
<script src='js/events.js'></script>
<script src='js/nodes.js'></script>
<script src='js/basictypes.js'></script>
<script src='js/tilesystem.js'></script>
<script src='js/simplex.js'></script>
<script src='js/procedural.js'></script>
<script src='js/gameview.js'></script>
<script src='js/unit.js'></script>
<script src='js/game.js'></script>
<script src='js/test.js'></script>
</head>
<body style="margin: 0; padding: 0; overflow: hidden; width: 100%; height: 100%" oncontextmenu="return false;">
<div id='fps' style='z-index: 1001; position: fixed; color: yellow; font-size: 44px'></div>
</body>
</html>
86 changes: 86 additions & 0 deletions js/astar.js
@@ -0,0 +1,86 @@
function Vector(x, y) {
var v = {
X: x,
Y: y,
is: function(o) { return v.X === o.X && v.Y === o.Y },
distanceTo: function(o) { return Math.sqrt(Math.pow(Math.abs(v.X - o.X), 2) + Math.pow(Math.abs(v.Y - o.Y), 2)); }
}
return v;
}
function isInList(item, list){
for(var i = 0; i < list.length; i++){
if(item.P.is(list[i].P)){
return i;
}
}
return -1;
}
function findPath(collisionMap, s, e) {
var openList = [],
closedList = [],
start = Vector(s.X, s.Y),
end = Vector(e.X, e.Y),
currentNode = start,
parent = { G: 0, H: start.distanceTo(end), F: start.distanceTo(end), P: start },
n, node, path, i, lowest;
openList.push(parent);
while(openList.length > 0){
currentNode = parent.P;
var neighbors = [ { G: parent.G + 1, H: 0, F: 0, P: Vector(currentNode.X, currentNode.Y - 1) },
{ G: parent.G + 1, H: 0, F: 0, P: Vector(currentNode.X - 1, currentNode.Y - 1)},
{ G: parent.G + 1, H: 0, F: 0, P: Vector(currentNode.X - 1, currentNode.Y)},
{ G: parent.G + 1, H: 0, F: 0, P: Vector(currentNode.X, currentNode.Y + 1)},
{ G: parent.G + 1, H: 0, F: 0, P: Vector(currentNode.X + 1, currentNode.Y + 1)},
{ G: parent.G + 1, H: 0, F: 0, P: Vector(currentNode.X + 1, currentNode.Y)},
{ G: parent.G + 1, H: 0, F: 0, P: Vector(currentNode.X + 1, currentNode.Y - 1)},
{ G: parent.G + 1, H: 0, F: 0, P: Vector(currentNode.X - 1, currentNode.Y + 1)} ];

closedList.push(parent);
openList.splice(isInList(parent, openList), 1);
for(n = 0; n < neighbors.length; n++){
if( neighbors[n].P.X > 0 &&
neighbors[n].P.Y > 0 &&
neighbors[n].P.X < collisionMap.length &&
neighbors[n].P.Y < collisionMap[0].length ) {
node = neighbors[n];
if(node.P.is(end)){

path = [];
node.parent = parent;
path.unshift({X: node.P.X, Y: node.P.Y});
while(!node.P.is(start)){
node = node.parent;
path.unshift({X: node.P.X, Y: node.P.Y});
}
return path;
}
if(isInList(node, closedList) === -1 && collisionMap[node.P.X][node.P.Y] === 0 /* collision.PASSABLE */){
node.H = node.P.distanceTo(end);
node.F = node.G + node.H;
var listNode = openList[isInList(node, openList)];
if(listNode && listNode.F > node.F){
listNode.parent = parent;
listNode.F = node.F;
listNode.G = node.G;
}
else if(!listNode){
node.parent = parent;
openList.push(node);
}
}
}
}
lowest = 0;
for(i = 0; i < openList.length; i++){
if(openList[i].F < openList[lowest].F){
lowest = i;
}
}
parent = openList[lowest];
}
//No path found
return [];
}
onmessage = function(e) {
postMessage(findPath(e.data.collisionMap, Vector(e.data.x1, e.data.y1), Vector(e.data.x2, e.data.y2)));
};
81 changes: 81 additions & 0 deletions js/basictypes.js
@@ -0,0 +1,81 @@
/* bt (Basic Types) depends on ns (nodes) for pooling */

/* ns.Vector inherits from ns.Pooled through Object.create.
In addition, it checks if a discarded object is available,
and returns it instead of a new instance
*/
var bt = {};
bt.Vector = function(x, y) {
"use strict";
var vector = ns.Pooled.pull("vector");
if(!vector) {
vector = Object.create(ns.Pooled(), {
shallow: { value: function() { return { X: vector.X, Y: vector.Y }; }},
add: { value: function(other) { return bt.Vector(vector.X + other.X, vector.Y + other.Y); }},
subtract: { value: function(other) { return bt.Vector(vector.X - other.X, vector.Y - other.Y); }},
multiply: { value: function(other) { return bt.Vector(vector.X * other.X, vector.Y * other.Y); }},
divide: { value: function(other) { return bt.Vector(vector.X / other.X, vector.Y / other.Y); }},
is: { value: function(other) { return (vector.X === other.X && vector.Y === other.Y); }},
isInside: { value: function(rect) { return (vector.X > rect[0] && vector.X < rect[0] + rect[2] && vector.Y > rect[1] && vector.Y < rect[1] + rect[3]); }},
distanceTo: { value: function(other) {
var xdiff = Math.abs(vector.X - other.X),
ydiff = Math.abs(vector.Y - other.Y);
return Math.sqrt(Math.pow(xdiff, 2) + Math.pow(ydiff, 2)); }
},
type: { value: "vector" }
});
}
vector.X = x || 0;
vector.Y = y || 0;
return vector;
};

bt.Color = function(input) {
"use strict";
var r, g, b;
if(input[0] === '#') {
if(input.length === 4) {
r = (parseInt(input[1], 16) + 1) * 16;
g = (parseInt(input[2], 16) + 1) * 16;
b = (parseInt(input[3], 16) + 1) * 16;
} else {
r = (parseInt(input.substr(1, 2), 16) + 1);
g = (parseInt(input.substr(3, 2), 16) + 1);
b = (parseInt(input.substr(5, 2), 16) + 1);
}
}
return Object.create(ns.Pooled(),{
array: {
get: function() {
return [r, g, b];
},
set: function(a) {
r = a[0];
g = a[1];
b = a[2];
}
},
red: {
get: function() { return r; },
set: function(red) { r = red; }
},
green: {
get: function() { return g; },
set: function(green) { g = green; }
},
blue: {
get: function() { return b; },
set: function(blue) { b = blue; }
},
toString: {
value : function() {
return "rgba(" + r + "," + g + "," + b + ",1.0)" }
},
mul: {
value: function(x) {
r = r * x | 0;
g = g * x | 0;
b = b * x | 0; }
}
});
};
21 changes: 21 additions & 0 deletions js/events.js
@@ -0,0 +1,21 @@
var Events = {
attach: function(obj) {
var eventList = {},
eventTarget = {
on: function(ev, f) {
if(!eventList[ev]) eventList[ev] = [];
eventList[ev].push(f);
},
fire: function(ev, obj) {
if(eventList[ev]) {
for(var i = 0; i < eventList[ev].length; i++) {
eventList[ev][i](obj);
}
}
}
};
for(prop in eventTarget) {
obj[prop] = eventTarget[prop];
}
}
};
44 changes: 44 additions & 0 deletions js/game.js
@@ -0,0 +1,44 @@
var tileSize = 32,
game = {
tileSize: 32,
root: ns.Node(),
count: 0,
frames: 0,
selectedUnits: ns.Node(),
units: ns.Node(),
fps: 0,
collisionMap: [],
map: []
}, collision = {
PASSABLE: 0,
UNPASSABLE: 1,
UNIT: 2,
RESERVED: 3
};

game.deselectAll = function() {
game.selectedUnits.each(function() {
this.deselect();
});
game.selectedUnits.clear();
};

game.addUnit = function(x, y) {
var unit = Unit(x, y);
game.units.draw = function() {
game.units.each(function() {
this.draw();
});
}
//game.selectedUnits.add(unit);
unit.on("click", (function(unit) {
return function() {
game.deselectAll();
unit.select();
game.selectedUnits.add(unit);
}
}(unit)));
game.units.add(unit);
game.root.add(game.units);
}

39 changes: 39 additions & 0 deletions js/gameview.js
@@ -0,0 +1,39 @@
//merge into Map object
function gameView(w, h) {
var c = makeCanvas(w, h);
gameView.canvas = c.canvas;
gameView.context = c.context;
gameView.width = w;
gameView.height = h;

var tiles = [
procedural.terrain(tileSize, tileSize, bt.Color("#618C32")),
procedural.terrain(tileSize, tileSize, bt.Color("#CCE010")),
procedural.terrain(tileSize, tileSize, bt.Color("#E6DFC8")),
procedural.terrain(tileSize, tileSize, bt.Color("#7A6212"))
];
game.map = ts.TileSet(tiles, procedural.noiseMap(4096, 4096, 40, 4), gameView.canvas, w, h);
game.mousePosition = bt.Vector(0, 0);
game.root.add(gameView);
console.log(game.map.height);
game.map.draw();
setInterval(gameView.scrollHandler, 32);
}

gameView.draw = function() {

game.context.drawImage(gameView.canvas, 0 ,0);
}

gameView.scrollHandler = function() {
if(!game.map) return;
if(game.mousePosition.X < tileSize * 2)
if(game.map.offset.X > 0) game.map.horizontal(1);
if(game.mousePosition.X > gameView.width - tileSize * 2)
if(game.map.offset.X < game.map.width - gameView.width / tileSize) game.map.horizontal(-1);
if(game.mousePosition.Y < tileSize * 2)
if(game.map.offset.Y > 0) game.map.vertical(1);
if(game.mousePosition.Y > game.canvas.height - tileSize * 2)
if(game.map.offset.Y < game.map.height - ((game.canvas.height / tileSize + .5) | 0)) game.map.vertical(-1);
};

84 changes: 84 additions & 0 deletions js/nodes.js
@@ -0,0 +1,84 @@

/* ns(nodes) is a module that facilitates building a hierarchy of nodes such as for drawing to a canvas */
var ns = {}; //namespace

/* ns.Pooled is a self executing function (IIFE),
which returns a constructor. This enables the use of static variables.
*/
ns.Pooled = (function() {
"use strict";
//(static) privates for ns.Pooled
var pool = {},
pull = function(name) {
if(pool[name]) {
return pool[name].shift();
}
return null;
},
available = function(name) {
return pool[name];
},
//constructor for ns.Pooled
self = function() {
return {
release: function() {
if(!this.type) {
throw("Cannot release to pool: object has no type property.");
}
if(!pool[this.type]) {
pool[this.type] = [];
}
pool[this.type].push(this);
},
};
};
//set static public methods for ns.Pooled
self.pull = pull;
self.available = available;
return self;
}());

ns.Node = function() {
"use strict";
var node = ns.Pooled.pull("node");
if(!node) {
var children = [];
node = Object.create(ns.Pooled(), {
visible: { value: true, enumerable: true, configurable: true },
each: {
value: function(func) {
for(var i = 0; i < children.length; i++) {
func.apply(children[i]);
}
},
enumerable: true
},
clear: {
value: function() {
children.length = 0;
}
},
add: {
value : function(child) {
children.push(child);
child.parent = node;
},
enumerable: true
},
remove: {
value: function(child) {
children.splice(children.indexOf(child), 1);
},
enumerable: true
},
up: {
get: function() {
return Object.getPrototypeOf(this);
}
}
});
}
return node;
};

// end of engine land.

0 comments on commit 349e249

Please sign in to comment.