Created
September 20, 2011 19:45
-
-
Save dsaveliev/1230126 to your computer and use it in GitHub Desktop.
js
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
(function() { | |
var __indexOf = Array.prototype.indexOf || function(item) { | |
for (var i = 0, l = this.length; i < l; i++) { | |
if (this[i] === item) return i; | |
} | |
return -1; | |
}; | |
window.onload = function() { | |
var generateWorld, getDistance, getRange; | |
Crafty.cfg = { | |
sprite: "source/sprite.png", | |
width: 800, | |
height: 640, | |
tile: 16, | |
w_tiles: 50, | |
h_tiles: 40 | |
}; | |
getDistance = function(tiles) { | |
return tiles * Crafty.cfg["tile"]; | |
}; | |
getRange = function(first, last, axis) { | |
var tiles, _i, _ref, _ref2, _results; | |
tiles = axis === "x" ? Crafty.cfg["w_tiles"] : Crafty.cfg["h_tiles"]; | |
return (function() { | |
_results = []; | |
for (var _i = _ref = 0 + first, _ref2 = tiles + last; _ref <= _ref2 ? _i < _ref2 : _i > _ref2; _ref <= _ref2 ? _i++ : _i--){ _results.push(_i); } | |
return _results; | |
}).apply(this); | |
}; | |
Crafty.init(Crafty.cfg["width"], Crafty.cfg["height"]); | |
Crafty.canvas.init(); | |
Crafty.sprite(Crafty.cfg["tile"], Crafty.cfg["sprite"], { | |
grass1: [0, 0], | |
grass2: [1, 0], | |
grass3: [2, 0], | |
grass4: [3, 0], | |
flower: [0, 1], | |
bush1: [0, 2], | |
bush2: [1, 2], | |
player: [0, 3], | |
enemy: [0, 3], | |
banana: [4, 0], | |
empty: [4, 0] | |
}); | |
generateWorld = function() { | |
var grassType, i, j, _i, _len, _ref, _results; | |
_ref = getRange(0, 0, "x"); | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
i = _ref[_i]; | |
_results.push((function() { | |
var _j, _len2, _ref2, _results2; | |
_ref2 = getRange(0, 0, "y"); | |
_results2 = []; | |
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { | |
j = _ref2[_j]; | |
grassType = Crafty.randRange(1, 4); | |
Crafty.e("2D, Canvas, grass" + grassType).attr({ | |
x: getDistance(i), | |
y: getDistance(j) | |
}); | |
if (__indexOf.call(getRange(1, -1, "x"), i) >= 0 && __indexOf.call(getRange(1, -1, "y"), j) >= 0 && Crafty.randRange(0, 30) > 29) { | |
Crafty.e("2D, DOM, flower, SpriteAnimation, explodable, solid").attr({ | |
x: getDistance(i), | |
y: getDistance(j), | |
z: 1000 | |
}).animate("wind", 0, 1, 3).animate("wind", 80, -1).bind('explode', function() { | |
return this.destroy(); | |
}); | |
} | |
_results2.push(i === 0 || j === 0 || i === Crafty.cfg["w_tiles"] - 1 || j === Crafty.cfg["h_tiles"] - 1 ? Crafty.e("2D, Canvas, solid, bush" + (Crafty.randRange(1, 2))).attr({ | |
x: getDistance(i), | |
y: getDistance(j), | |
z: 2 | |
}) : void 0); | |
} | |
return _results2; | |
})()); | |
} | |
return _results; | |
}; | |
Crafty.scene("loading", function() { | |
Crafty.load([Crafty.cfg["sprite"]], function() { | |
return Crafty.scene("main"); | |
}); | |
Crafty.background("#000"); | |
return Crafty.e("2D, DOM, Text").attr({ | |
w: 100, | |
h: 20, | |
x: 350, | |
y: 310 | |
}).text("Loading").css({ | |
"text-align": "center" | |
}); | |
}); | |
Crafty.scene("main", function() { | |
var player; | |
generateWorld(); | |
Crafty.c("Hero", { | |
init: function() { | |
this.requires("SpriteAnimation, Collision").animate("walk_left", 6, 3, 8).animate("walk_right", 9, 3, 11).animate("walk_up", 3, 3, 5).animate("walk_down", 0, 3, 2).bind("NewDirection", function(direction) { | |
if (direction.x < 0 && !this.isPlaying("walk_left")) { | |
this.stop().animate("walk_left", 10, -1); | |
} | |
if (direction.x > 0 && !this.isPlaying("walk_right")) { | |
this.stop().animate("walk_right", 10, -1); | |
} | |
if (direction.y < 0 && !this.isPlaying("walk_up")) { | |
this.stop().animate("walk_up", 10, -1); | |
} | |
if (direction.y > 0 && !this.isPlaying("walk_down")) { | |
this.stop().animate("walk_down", 10, -1); | |
} | |
if (!direction.x && !direction.y) { | |
return this.stop(); | |
} | |
}).bind("Moved", function(from) { | |
if (this.hit("solid")) { | |
return this.attr({ | |
x: from.x, | |
y: from.y | |
}); | |
} | |
}); | |
return this; | |
} | |
}); | |
Crafty.c("RightControls", { | |
init: function() { | |
return this.requires("Multiway"); | |
}, | |
rightControls: function(speed) { | |
this.multiway(speed, { | |
UP_ARROW: -90, | |
DOWN_ARROW: 90, | |
RIGHT_ARROW: 0, | |
LEFT_ARROW: 180 | |
}); | |
return this; | |
} | |
}); | |
return player = Crafty.e("2D, Canvas, player, RightControls, Hero, Animation, Collision").attr({ | |
x: 392, | |
y: 312, | |
z: 1 | |
}).rightControls(1); | |
}); | |
return Crafty.scene("loading"); | |
}; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, this should be on github not gist. The code is not self-contained and part of a bigger project. good luck.