Browse files

Merge pull request #2 from Deradon/refactor

Some refactoring
  • Loading branch information...
2 parents 1ae7531 + 84b8408 commit f1ef15cea78fc7b68cda9bfc1fd8f90b96321e4a @kolja committed Mar 12, 2012
Showing with 1,011 additions and 883 deletions.
  1. +1 −0 .gitignore
  2. +23 −33 Cakefile
  3. +21 −0 build.sh
  4. +34 −0 examples/asteroids/Cakefile
  5. 0 { → examples/asteroids}/assets/images/beach3d.png
  6. 0 { → examples/asteroids}/assets/images/erde.png
  7. 0 { → examples/asteroids}/assets/images/jumpnrun.png
  8. 0 { → examples/asteroids}/assets/images/mond.png
  9. 0 { → examples/asteroids}/assets/images/sea_beach.png
  10. 0 { → examples/asteroids}/assets/images/test.png
  11. 0 { → examples/asteroids}/assets/images/wall_map_template.png
  12. 0 { → examples/asteroids}/assets/images/wall_shadow.png
  13. 0 { → examples/asteroids}/assets/images/walls.png
  14. 0 { → examples/asteroids}/assets/images/weltraum.jpg
  15. 0 { → examples/asteroids}/assets/jumpnrun_map.png
  16. 0 { → examples/asteroids}/assets/map.json
  17. 0 { → examples/asteroids}/assets/map.png
  18. 0 { → examples/asteroids}/assets/maze.png
  19. 0 { → examples/asteroids}/assets/minimap.png
  20. +20 −0 examples/asteroids/index.html
  21. +433 −0 examples/asteroids/lib/asteroids.js
  22. +11 −13 {script → examples/asteroids/src/actors}/hero.coffee
  23. +4 −6 {script → examples/asteroids/src/actors}/spaceship.coffee
  24. +11 −8 {script → examples/asteroids/src}/asteroids.coffee
  25. +12 −8 script/demoscene_bigbackground.coffee → examples/asteroids/src/scenes/bigbackground.coffee
  26. +5 −7 script/demoscene_height.coffee → examples/asteroids/src/scenes/height.coffee
  27. +7 −8 script/demoscene_iso.coffee → examples/asteroids/src/scenes/iso.coffee
  28. +9 −10 script/demoscene_jumpnrun.coffee → examples/asteroids/src/scenes/jumpnrun.coffee
  29. +8 −8 script/demoscene_maze.coffee → examples/asteroids/src/scenes/maze.coffee
  30. 0 { → examples/asteroids}/stylesheets/css/main.css
  31. 0 { → examples/asteroids}/stylesheets/main.sass
  32. +26 −0 examples/asteroids/watch.rb
  33. +233 −644 script/js/game.js → lib/irf.js
  34. +0 −13 main.html
  35. +0 −8 script/lib/scene.coffee
  36. +0 −13 script/lib/scenemanager.coffee
  37. +30 −0 src/irf.coffee
  38. +1 −6 {script/lib → src/irf}/background.coffee
  39. +11 −11 {script/lib → src/irf}/boundingbox.coffee
  40. +3 −8 {script/lib → src/irf}/camera.coffee
  41. +4 −4 {script/lib → src/irf}/eventmanager.coffee
  42. +3 −5 {script/lib → src/irf}/game.coffee
  43. +4 −3 {script/lib → src/irf}/helpers.coffee
  44. +11 −11 {script/lib → src/irf}/keyboard.coffee
  45. +15 −15 {script/lib → src/irf}/map.coffee
  46. +8 −0 src/irf/scene.coffee
  47. +15 −0 src/irf/scenemanager.coffee
  48. +14 −14 {script/lib → src/irf}/sprite.coffee
  49. +5 −4 {script/lib → src/irf}/timer.coffee
  50. +18 −20 {script/lib → src/irf}/vector.coffee
  51. 0 {script/js → vendor}/jquery.min.js
  52. +11 −3 watch.rb
View
1 .gitignore
@@ -1,2 +1,3 @@
.sass-cache
.DS_Store
+**~
View
56 Cakefile
@@ -1,51 +1,41 @@
+# https://github.com/jashkenas/coffee-script/wiki/[HowTo]-Compiling-and-Setting-Up-Build-Tools
+
fs = require 'fs'
{exec} = require 'child_process'
libFiles = [
- 'lib/helpers'
- 'lib/timer'
- 'lib/vector'
- 'lib/boundingbox'
- 'lib/eventmanager'
- 'lib/keyboard'
- 'lib/game'
- 'lib/map'
- 'lib/background'
- 'lib/sprite'
- 'lib/scene'
- 'lib/scenemanager'
- 'lib/camera'
-]
-appFiles = [
- 'asteroids'
- 'demoscene_bigbackground'
- 'demoscene_height'
- 'demoscene_iso'
- 'demoscene_jumpnrun'
- 'demoscene_maze'
- 'spaceship'
- 'hero'
+ 'irf/background'
+ 'irf/boundingbox'
+ 'irf/camera'
+ 'irf/eventmanager'
+ 'irf/game'
+ 'irf/helpers'
+ 'irf/keyboard'
+ 'irf/map'
+ 'irf/scene'
+ 'irf/scenemanager'
+ 'irf/sprite'
+ 'irf/timer'
+ 'irf/vector'
]
+libFiles.push 'irf' # THIS MUST BE LAST REQUIRED FILE
+
-task 'build:coffee', 'Build single application file from source files', ->
- files = libFiles.concat appFiles
+task 'build', 'Build single application file from source files', ->
+ files = libFiles
appContents = new Array remaining = files.length
for file, index in files then do (file, index) ->
- fs.readFile "script/#{file}.coffee", 'utf8', (err, fileContents) ->
+ fs.readFile "src/#{file}.coffee", 'utf8', (err, fileContents) ->
throw err if err
appContents[index] = fileContents
process() if --remaining is 0
process = ->
- fs.writeFile 'script/js/game.coffee', appContents.join('\n\n'), 'utf8', (err) ->
+ fs.writeFile 'lib/irf.coffee', appContents.join('\n\n'), 'utf8', (err) ->
throw err if err
- exec 'coffee --compile script/js/game.coffee', (err, stdout, stderr) ->
+ exec 'coffee --compile lib/irf.coffee', (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
- fs.unlink 'script/js/game.coffee', (err) ->
+ fs.unlink 'lib/irf.coffee', (err) ->
throw err if err
console.log 'Done.'
-task 'build:sass', 'Compile sass files into css', ->
- exec 'sass stylesheets/main.sass stylesheets/css/main.css', (err, stdout, stderr) ->
- throw err if err
- console.log stdout + stderr
View
21 build.sh
@@ -0,0 +1,21 @@
+#!/usr/bin/env ruby
+
+# Get running cake command
+cake_cmd = "cake"
+begin
+ `#{cake_cmd}`
+rescue Errno::ENOENT => e
+ cake_cmd = "cake.coffeescript"
+ `#{cake_cmd}` rescue raise "Cake not found"
+end
+
+puts "[BUILDING] InstantRocketFuel..."
+puts `#{cake_cmd} build`
+
+Dir.new("examples").each do |example|
+ next if ["..", "."].include?(example)
+
+ puts "[BUILDING] #{example}"
+ puts `cd examples/#{example}; #{cake_cmd} build`
+end
+
View
34 examples/asteroids/Cakefile
@@ -0,0 +1,34 @@
+# https://github.com/jashkenas/coffee-script/wiki/[HowTo]-Compiling-and-Setting-Up-Build-Tools
+
+fs = require 'fs'
+{exec} = require 'child_process'
+
+appFiles = [
+ 'asteroids'
+ 'actors/hero'
+ 'actors/spaceship'
+ 'scenes/bigbackground'
+ 'scenes/height'
+ 'scenes/iso'
+ 'scenes/jumpnrun'
+ 'scenes/maze'
+]
+
+task 'build', 'Build single application file from source files', ->
+ files = appFiles
+ appContents = new Array remaining = files.length
+ for file, index in files then do (file, index) ->
+ fs.readFile "src/#{file}.coffee", 'utf8', (err, fileContents) ->
+ throw err if err
+ appContents[index] = fileContents
+ process() if --remaining is 0
+ process = ->
+ fs.writeFile 'lib/asteroids.coffee', appContents.join('\n\n'), 'utf8', (err) ->
+ throw err if err
+ exec 'coffee --compile lib/asteroids.coffee', (err, stdout, stderr) ->
+ throw err if err
+ console.log stdout + stderr
+ fs.unlink 'lib/asteroids.coffee', (err) ->
+ throw err if err
+ console.log 'Done.'
+
View
0 assets/images/beach3d.png → examples/asteroids/assets/images/beach3d.png
File renamed without changes
View
0 assets/images/erde.png → examples/asteroids/assets/images/erde.png
File renamed without changes
View
0 assets/images/jumpnrun.png → ...ples/asteroids/assets/images/jumpnrun.png
File renamed without changes
View
0 assets/images/mond.png → examples/asteroids/assets/images/mond.png
File renamed without changes
View
0 assets/images/sea_beach.png → ...les/asteroids/assets/images/sea_beach.png
File renamed without changes
View
0 assets/images/test.png → examples/asteroids/assets/images/test.png
File renamed without changes
View
0 assets/images/wall_map_template.png → ...roids/assets/images/wall_map_template.png
File renamed without changes
View
0 assets/images/wall_shadow.png → ...s/asteroids/assets/images/wall_shadow.png
File renamed without changes
View
0 assets/images/walls.png → examples/asteroids/assets/images/walls.png
File renamed without changes
View
0 assets/images/weltraum.jpg → ...ples/asteroids/assets/images/weltraum.jpg
File renamed without changes
View
0 assets/jumpnrun_map.png → examples/asteroids/assets/jumpnrun_map.png
File renamed without changes
View
0 assets/map.json → examples/asteroids/assets/map.json
File renamed without changes.
View
0 assets/map.png → examples/asteroids/assets/map.png
File renamed without changes
View
0 assets/maze.png → examples/asteroids/assets/maze.png
File renamed without changes
View
0 assets/minimap.png → examples/asteroids/assets/minimap.png
File renamed without changes
View
20 examples/asteroids/index.html
@@ -0,0 +1,20 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>Asteroids</title>
+ <meta name="description" content="Demo of Game Boilerplate">
+ <link rel="stylesheet" href="stylesheets/css/main.css">
+ <script type="text/javascript" src="../../vendor/jquery.min.js" charset="utf-8"></script>
+ <script type="text/javascript" src="../../lib/irf.js" charset="utf-8"></script>
+ <script type="text/javascript" src="lib/asteroids.js" charset="utf-8"></script>
+ </head>
+ <body>
+ <script>
+ $(document).ready(function(){
+ //console.log(IRF);
+ });
+ </script>
+ </body>
+</html>
+
View
433 examples/asteroids/lib/asteroids.js
@@ -0,0 +1,433 @@
+(function() {
+ var Asteroids, Hero, SceneBigBackground, SceneHeight, SceneIso, SceneJumpNRun, SceneMaze, Spaceship, k, sceneclass, v;
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
+ function ctor() { this.constructor = child; }
+ ctor.prototype = parent.prototype;
+ child.prototype = new ctor;
+ child.__super__ = parent.prototype;
+ return child;
+ }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ for (k in IRF) {
+ v = IRF[k];
+ eval("var " + k + " = v");
+ }
+ sceneclass = sceneclass != null ? sceneclass : {};
+ Asteroids = (function() {
+ __extends(Asteroids, Game);
+ function Asteroids(width, height) {
+ Asteroids.__super__.constructor.call(this, width, height);
+ this.eventManager = new EventManager;
+ this.keyboard = new Keyboard;
+ this.sceneManager = new SceneManager(this, sceneclass);
+ this.sceneManager.setScene("maze");
+ }
+ Asteroids.prototype.update = function() {
+ Asteroids.__super__.update.call(this);
+ return this.sceneManager.currentScene.update(this.timer.delta);
+ };
+ Asteroids.prototype.render = function() {
+ Asteroids.__super__.render.call(this);
+ this.sceneManager.currentScene.render(this.ctx);
+ return this.ctx.fillText(this.timer.fps().toFixed(1), this.width - 50, 20);
+ };
+ return Asteroids;
+ })();
+ jQuery(function() {
+ var asteroids;
+ asteroids = new Asteroids(800, 600);
+ return asteroids.start();
+ });
+ Hero = (function() {
+ function Hero(eventManager, keyboard) {
+ this.eventManager = eventManager;
+ this.keyboard = keyboard;
+ this.state = "normal";
+ this.sprite = new Sprite({
+ "texture": "assets/images/test.png",
+ "width": 50,
+ "height": 50,
+ "key": {
+ "normal": 3,
+ "jumping": 5
+ }
+ });
+ this.coor = new Vector(100, 100);
+ this.speed = new Vector(0, 0);
+ this.force = new Vector(0.01, 0);
+ this.gravity = new Vector(0, 0.01);
+ this.bb = new BoundingBox(this.coor, new Vector(50, 50));
+ this.bb.color = "red";
+ this.eventManager.register("touchdown", this.touchdown);
+ }
+ Hero.prototype.touchdown = function() {
+ return console.log("Hero says: Touchdown occurred");
+ };
+ Hero.prototype.update = function(delta, map) {
+ var tileBelow;
+ tileBelow = map.tileAtVector(this.coor).neighbor["s"];
+ this.speed.add_(this.gravity);
+ if (this.bb.intersect(tileBelow.bb) && !(typeof tileBelow.isWalkable === "function" ? tileBelow.isWalkable() : void 0)) {
+ this.speed.y = 0;
+ this.state = "normal";
+ }
+ if (this.keyboard.key("right")) {
+ this.speed.add_(this.force);
+ } else if (this.keyboard.key("left")) {
+ this.speed.subtract_(this.force);
+ } else {
+ if (this.speed.x > 0) {
+ this.speed.subtract_(this.force);
+ } else {
+ this.speed.add_(this.force);
+ }
+ }
+ if (this.keyboard.key("space") && this.state !== "jumping") {
+ this.state = "jumping";
+ this.speed.y = -0.7;
+ }
+ this.coor = this.coor.add(this.speed.mult(delta));
+ return this.bb.coor = this.coor;
+ };
+ Hero.prototype.render = function(ctx) {
+ ctx.save();
+ ctx.translate(this.coor.x, this.coor.y);
+ this.sprite.render(this.state, ctx);
+ ctx.restore();
+ return this.bb.render(ctx);
+ };
+ return Hero;
+ })();
+ Spaceship = (function() {
+ function Spaceship(eventManager, keyboard) {
+ this.eventManager = eventManager;
+ this.keyboard = keyboard;
+ this.state = "normal";
+ this.sprite = new Sprite({
+ "texture": "assets/images/test.png",
+ "width": 50,
+ "height": 50
+ });
+ this.sprite.addImage("normal", Math.floor(Math.random() * 10));
+ this.coor = new Vector(Math.random() * 1024, Math.random() * 768);
+ this.speed = new Vector(0.1, 0.1);
+ if (Math.random() > 0.5) {
+ this.speed = this.speed.mult(-1);
+ }
+ }
+ Spaceship.prototype.update = function(delta) {
+ this.coor = this.coor.add(this.speed.mult(delta));
+ if (this.coor.x > 1024) {
+ this.speed.x = this.speed.x * -1;
+ this.coor.x = 1024;
+ this.eventManager.trigger("touchdown");
+ }
+ if (this.coor.x < 0) {
+ this.speed.x = this.speed.x * -1;
+ this.coor.x = 0;
+ }
+ if (this.coor.y > 768) {
+ this.speed.y = this.speed.y * -1;
+ this.coor.y = 768;
+ }
+ if (this.coor.y < 0) {
+ this.speed.y = this.speed.y * -1;
+ return this.coor.y = 0;
+ }
+ };
+ Spaceship.prototype.touchdown = function() {
+ return console.log("Spaceship says: Touchdown");
+ };
+ Spaceship.prototype.render = function(ctx) {
+ ctx.save();
+ ctx.translate(this.coor.x, this.coor.y);
+ this.sprite.render(this.state, ctx);
+ return ctx.restore();
+ };
+ Spaceship.prototype.hello = function() {
+ return console.log("hello!");
+ };
+ return Spaceship;
+ })();
+ sceneclass["bigbg"] = SceneBigBackground = (function() {
+ __extends(SceneBigBackground, Scene);
+ function SceneBigBackground(parent) {
+ var backgroundsprite, i;
+ this.parent = parent;
+ backgroundsprite = new Sprite({
+ "texture": "assets/images/weltraum.jpg",
+ "width": 500,
+ "height": 500
+ });
+ this.background = new Background(backgroundsprite);
+ console.log(this.background);
+ this.spaceships = [];
+ for (i = 0; i <= 3; i++) {
+ this.spaceships[i] = new Spaceship(this.parent.eventManager);
+ }
+ }
+ SceneBigBackground.prototype.update = function(delta) {
+ var spaceship, _i, _len, _ref, _results;
+ _ref = this.spaceships;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ spaceship = _ref[_i];
+ _results.push(spaceship.update(delta));
+ }
+ return _results;
+ };
+ SceneBigBackground.prototype.render = function(ctx) {
+ var spaceship, _i, _len, _ref, _results;
+ ctx.save();
+ ctx.translate(250, 250);
+ this.background.render(ctx);
+ ctx.restore();
+ _ref = this.spaceships;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ spaceship = _ref[_i];
+ _results.push(spaceship.render(ctx));
+ }
+ return _results;
+ };
+ return SceneBigBackground;
+ })();
+ sceneclass["height"] = SceneHeight = (function() {
+ __extends(SceneHeight, Scene);
+ function SceneHeight(parent) {
+ var simple;
+ this.parent = parent;
+ simple = new Sprite({
+ "texture": "assets/images/beach3d.png",
+ "width": 107,
+ "height": 107,
+ "innerWidth": 87,
+ "innerHeight": 87,
+ "key": {
+ "00": 12,
+ "dd": 12
+ }
+ });
+ this.background = new Map({
+ "mapfile": "assets/minimap.png",
+ "pattern": "simple",
+ "sprite": simple
+ });
+ this.camera = new Camera({
+ "projection": "normal",
+ "vpWidth": this.parent.width,
+ "vpHeight": this.parent.height
+ });
+ }
+ SceneHeight.prototype.update = function(delta) {};
+ SceneHeight.prototype.render = function(ctx) {
+ return this.background.render(ctx, this.camera);
+ };
+ return SceneHeight;
+ })();
+ sceneclass["iso"] = SceneIso = (function() {
+ __extends(SceneIso, Scene);
+ function SceneIso(parent) {
+ var beach3d;
+ this.parent = parent;
+ this.camera = new Camera({
+ "projection": "iso",
+ "vpWidth": this.parent.width,
+ "vpHeight": this.parent.height
+ });
+ beach3d = new Sprite({
+ "texture": "assets/images/beach3d.png",
+ "width": 107,
+ "height": 107,
+ "innerWidth": 87,
+ "innerHeight": 87,
+ "key": {
+ "dd00dddd": 0,
+ "00dddddd": 1,
+ "dddd00dd": 2,
+ "dddddd00": 3,
+ "dd00dd00": 4,
+ "0000dddd": 5,
+ "00dd00dd": 6,
+ "dddd0000": 7,
+ "0000dd00": 8,
+ "000000dd": 9,
+ "00dd0000": 10,
+ "dd000000": 11,
+ "dddddddd": 12,
+ "00000000": 13,
+ "dd0000dd": 14,
+ "00dddd00": 15
+ }
+ });
+ this.background = new Map({
+ "mapfile": "assets/map.png",
+ "pattern": "square",
+ "sprite": beach3d
+ });
+ }
+ SceneIso.prototype.update = function(delta) {};
+ SceneIso.prototype.render = function(ctx) {
+ return this.camera.apply(ctx, __bind(function() {
+ return this.background.render(ctx, this.camera);
+ }, this));
+ };
+ return SceneIso;
+ })();
+ sceneclass["jumpnrun"] = SceneJumpNRun = (function() {
+ __extends(SceneJumpNRun, Scene);
+ function SceneJumpNRun(parent) {
+ var customReadFunction, i, jumpnrunSprite;
+ this.parent = parent;
+ this.hero = new Hero(this.parent.eventManager, this.parent.keyboard);
+ this.camera = new Camera({
+ "projection": "normal",
+ "vpWidth": this.parent.width,
+ "vpHeight": this.parent.height
+ });
+ jumpnrunSprite = new Sprite({
+ "texture": "assets/images/jumpnrun.png",
+ "width": 100,
+ "height": 100,
+ "innerWidth": 95,
+ "innerHeight": 95,
+ "key": {
+ "00": 0,
+ "11": 1,
+ '22': 2,
+ "33": 3,
+ "44": 4,
+ '55': 5,
+ "66": 6,
+ "77": 7,
+ '88': 8,
+ "99": 9,
+ "aa": 10,
+ "bb": 11
+ }
+ });
+ customReadFunction = function() {
+ var col, green, row, type, z, _ref, _results;
+ _results = [];
+ for (row = 0, _ref = this.map.height - 1; 0 <= _ref ? row <= _ref : row >= _ref; 0 <= _ref ? row++ : row--) {
+ _results.push((function() {
+ var _ref2, _results2;
+ _results2 = [];
+ for (col = 0, _ref2 = this.map.width - 1; 0 <= _ref2 ? col <= _ref2 : col >= _ref2; 0 <= _ref2 ? col++ : col--) {
+ type = "" + this.mapData[row][col][0];
+ green = parseInt(this.mapData[row][col][1], 16);
+ z = parseInt(this.mapData[row][col][2], 16);
+ _results2.push(this.tiles.push(new Tile(this.sprite, type, row, col, green, z)));
+ }
+ return _results2;
+ }).call(this));
+ }
+ return _results;
+ };
+ this.background = new Map({
+ "mapfile": "assets/jumpnrun_map.png",
+ "pattern": customReadFunction,
+ "sprite": jumpnrunSprite
+ });
+ this.spaceships = [];
+ for (i = 0; i <= 3; i++) {
+ this.spaceships[i] = new Spaceship(this.parent.eventManager, this.parent.keyboard);
+ }
+ }
+ SceneJumpNRun.prototype.update = function(delta) {
+ var spaceship, _i, _len, _ref, _results;
+ this.hero.update(delta, this.background);
+ this.camera.coor = this.hero.coor;
+ _ref = this.spaceships;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ spaceship = _ref[_i];
+ _results.push(spaceship.update(delta));
+ }
+ return _results;
+ };
+ SceneJumpNRun.prototype.render = function(ctx) {
+ return this.camera.apply(ctx, __bind(function() {
+ var spaceship, _i, _len, _ref, _results;
+ this.background.render(ctx, this.camera);
+ this.hero.render(ctx);
+ _ref = this.spaceships;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ spaceship = _ref[_i];
+ _results.push(spaceship.render(ctx));
+ }
+ return _results;
+ }, this));
+ };
+ return SceneJumpNRun;
+ })();
+ sceneclass["maze"] = SceneMaze = (function() {
+ __extends(SceneMaze, Scene);
+ function SceneMaze(parent) {
+ var i, maze;
+ this.parent = parent;
+ this.camera = new Camera({
+ "projection": "normal",
+ "vpWidth": this.parent.width,
+ "vpHeight": this.parent.height
+ });
+ maze = new Sprite({
+ "texture": "assets/images/walls.png",
+ "width": 100,
+ "height": 100,
+ "innerWidth": 50,
+ "innerHeight": 50,
+ "key": {
+ "dddddddd": 0,
+ "dd00dddd": 1,
+ "dddd00dd": 2,
+ "dddddd00": 3,
+ "00dddddd": 4,
+ "00000000": 5,
+ "00dddd00": 6,
+ "0000dddd": 7,
+ "dd0000dd": 8,
+ "dddd0000": 9,
+ "00dd00dd": 12,
+ "dd00dd00": 13,
+ "00dd0000": 14,
+ "0000dd00": 15,
+ "000000dd": 16,
+ "dd000000": 17
+ }
+ });
+ this.background = new Map({
+ "mapfile": "assets/maze.png",
+ "pattern": "cross",
+ "sprite": maze
+ });
+ this.spaceships = [];
+ for (i = 0; i <= 3; i++) {
+ this.spaceships[i] = new Spaceship(this.parent.eventManager, this.parent.keyboard);
+ }
+ }
+ SceneMaze.prototype.update = function(delta) {
+ var spaceship, _i, _len, _ref, _results;
+ _ref = this.spaceships;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ spaceship = _ref[_i];
+ _results.push(spaceship.update(delta));
+ }
+ return _results;
+ };
+ SceneMaze.prototype.render = function(ctx) {
+ var spaceship, _i, _len, _ref, _results;
+ this.background.render(ctx, this.camera);
+ _ref = this.spaceships;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ spaceship = _ref[_i];
+ _results.push(spaceship.render(ctx));
+ }
+ return _results;
+ };
+ return SceneMaze;
+ })();
+}).call(this);
View
24 script/hero.coffee → examples/asteroids/src/actors/hero.coffee
@@ -2,7 +2,7 @@
class Hero
constructor: (@eventManager, @keyboard) ->
-
+
@state = "normal"
@sprite = new Sprite
"texture": "assets/images/test.png"
@@ -22,12 +22,12 @@ class Hero
# event Manager
@eventManager.register "touchdown", @touchdown
-
+
touchdown: ->
- console.log "Hero says: Touchdown occurred"
-
+ console.log "Hero says: Touchdown occurred"
+
update: (delta, map) ->
-
+
# apply gravity
tileBelow = map.tileAtVector(@coor).neighbor["s"]
# tileBelow.bb.color = "red"
@@ -36,23 +36,23 @@ class Hero
if @bb.intersect(tileBelow.bb) and not tileBelow.isWalkable?()
@speed.y = 0
@state = "normal"
-
+
# left/right movement
if @keyboard.key("right")
- @speed.add_ @force
+ @speed.add_ @force
else if @keyboard.key("left")
- @speed.subtract_ @force
+ @speed.subtract_ @force
else
if @speed.x > 0
@speed.subtract_ @force
else
@speed.add_ @force
-
+
# jump
if @keyboard.key("space") and @state isnt "jumping"
@state = "jumping"
@speed.y = -0.7
-
+
@coor = @coor.add( @speed.mult delta )
@bb.coor = @coor
@@ -61,10 +61,8 @@ class Hero
ctx.translate @coor.x, @coor.y
@sprite.render( @state, ctx )
ctx.restore()
-
+
# the BoundingBox has Coordinates. A sprite doesn't. So the bounding-box is rendered outside of the save/resotore area.
# it will take care of the propper displacement itself.
@bb.render ctx
-
-
View
10 script/spaceship.coffee → ...les/asteroids/src/actors/spaceship.coffee
@@ -1,21 +1,20 @@
class Spaceship
-
constructor: (@eventManager, @keyboard) ->
@state = "normal"
@sprite = new Sprite
"texture": "assets/images/test.png"
"width": 50
"height": 50
-
+
@sprite.addImage "normal", Math.floor Math.random() * 10
# @sprite.addAnimation "normal", { frames: [0,1,2,3,4].shuffle(), fps: 3, loop: true, callback: @hello }
@coor = new Vector( Math.random() * 1024, Math.random() * 768 )
@speed = new Vector( 0.1, 0.1 )
if Math.random() > 0.5
@speed = @speed.mult -1
-
+
update: (delta) ->
@coor = @coor.add( @speed.mult delta )
@@ -41,8 +40,7 @@ class Spaceship
ctx.translate @coor.x, @coor.y
@sprite.render( @state, ctx )
ctx.restore()
-
+
hello: ->
console.log "hello!"
-
-
+
View
19 script/asteroids.coffee → examples/asteroids/src/asteroids.coffee
@@ -1,17 +1,21 @@
+# Require all Class of IRF, so we won't need namespace here
+# TODO: get rid of eval
+eval("var #{k} = v") for k,v of IRF
# Main Game Controller / Root entry point
# Adjustments you make here will affect your whole Game.
-
+sceneclass = sceneclass ? {}
class Asteroids extends Game
-
+
constructor: (width, height) ->
super width, height
-
+
@eventManager = new EventManager
@keyboard = new Keyboard
-
- @sceneManager = new SceneManager this, ["bigbg", "jumpnrun", "iso", "maze", "height"] # Add your own Scenes or Levels
+
+ #@sceneManager = new SceneManager this, ["bigbg", "jumpnrun", "iso", "maze", "height"] # Add your own Scenes or Levels
+ @sceneManager = new SceneManager(this, sceneclass)
@sceneManager.setScene "jumpnrun"
update: ->
@@ -22,9 +26,8 @@ class Asteroids extends Game
super()
@sceneManager.currentScene.render @ctx
@ctx.fillText( @timer.fps().toFixed(1), @width - 50, 20 )
-
+
jQuery ->
- asteroids = new Asteroids( 1024, 768 )
+ asteroids = new Asteroids(800, 600)
asteroids.start()
-
View
20 script/demoscene_bigbackground.coffee → ...asteroids/src/scenes/bigbackground.coffee
@@ -1,24 +1,28 @@
sceneclass["bigbg"] = class SceneBigBackground extends Scene
constructor: (@parent) ->
-
- console.log "width: #{@parent.width} -- height: #{@parent.height}"
-
backgroundsprite = new Sprite
"texture": "assets/images/weltraum.jpg"
"width": 500
"height": 500
@background = new Background backgroundsprite
-
+ console.log @background
+
@spaceships = []
for i in [0..3]
- @spaceships[i] = new Spaceship
-
+ @spaceships[i] = new Spaceship(@parent.eventManager)
+
update: (delta) ->
for spaceship in @spaceships
spaceship.update delta
-
- render: (ctx) ->
+
+ render: (ctx) ->
+ # HACK: otherwise background was not shown completly
+ ctx.save()
+ ctx.translate 250, 250
@background.render(ctx)
+ ctx.restore()
+
for spaceship in @spaceships
spaceship.render ctx
+
View
12 script/demoscene_height.coffee → examples/asteroids/src/scenes/height.coffee
@@ -1,7 +1,5 @@
-
sceneclass["height"] = class SceneHeight extends Scene
constructor: (@parent) ->
-
simple = new Sprite
"texture": "assets/images/beach3d.png"
"width": 107
@@ -11,14 +9,14 @@ sceneclass["height"] = class SceneHeight extends Scene
"key":
"00": 12
"dd": 12
-
@background = new Map
"mapfile": "assets/minimap.png"
"pattern": "simple"
"sprite": simple
-
+ @camera = new Camera {"projection": "normal", "vpWidth": @parent.width, "vpHeight": @parent.height}
+
update: (delta) ->
-
- render: (ctx) ->
- @background.render(ctx)
+
+ render: (ctx) ->
+ @background.render(ctx, @camera)
View
15 script/demoscene_iso.coffee → examples/asteroids/src/scenes/iso.coffee
@@ -1,10 +1,8 @@
-
sceneclass["iso"] = class SceneIso extends Scene
constructor: (@parent) ->
-
@camera = new Camera {"projection": "iso", "vpWidth": @parent.width, "vpHeight": @parent.height}
# @camera.coor = new Vector(2500,1050)
-
+
beach3d = new Sprite
"texture": "assets/images/beach3d.png"
"width": 107
@@ -28,15 +26,16 @@ sceneclass["iso"] = class SceneIso extends Scene
"00000000": 13
"dd0000dd": 14
"00dddd00": 15
-
+
@background = new Map
"mapfile": "assets/map.png"
"pattern": "square"
"sprite": beach3d
-
-
+
+
update: (delta) ->
-
+
render: (ctx) ->
@camera.apply ctx, =>
- @background.render(ctx, @camera)
+ @background.render(ctx, @camera)
+
View
19 script/demoscene_jumpnrun.coffee → ...ples/asteroids/src/scenes/jumpnrun.coffee
@@ -1,12 +1,10 @@
-
sceneclass["jumpnrun"] = class SceneJumpNRun extends Scene
constructor: (@parent) ->
-
- @hero = new Hero @parent.eventManager, @parent.keyboard
-
+ @hero = new Hero(@parent.eventManager, @parent.keyboard)
+
@camera = new Camera {"projection": "normal", "vpWidth": @parent.width, "vpHeight": @parent.height}
# @camera.coor = new Vector(2500,1050)
-
+
jumpnrunSprite = new Sprite
"texture": "assets/images/jumpnrun.png"
"width": 100
@@ -27,10 +25,10 @@ sceneclass["jumpnrun"] = class SceneJumpNRun extends Scene
"aa": 10
"bb": 11
- # this whole function can actually be replaced by the string "sinple"
+ # this whole function can actually be replaced by the string "simple"
# you can, however use it to override the Maps standard Mapfile interpretation mechanism.
customReadFunction = ->
- for row in [0..@map.height-1]
+ for row in [0..@map.height-1]
for col in [0..@map.width-1]
type = "#{@mapData[row][col][0]}"
green = parseInt( @mapData[row][col][1], 16 )
@@ -41,20 +39,21 @@ sceneclass["jumpnrun"] = class SceneJumpNRun extends Scene
"mapfile": "assets/jumpnrun_map.png"
"pattern": customReadFunction
"sprite": jumpnrunSprite
-
+
@spaceships = []
for i in [0..3]
@spaceships[i] = new Spaceship @parent.eventManager, @parent.keyboard
-
+
update: (delta) ->
@hero.update(delta, @background)
@camera.coor = @hero.coor
for spaceship in @spaceships
spaceship.update delta
-
+
render: (ctx) ->
@camera.apply ctx, =>
@background.render(ctx, @camera)
@hero.render(ctx)
for spaceship in @spaceships
spaceship.render ctx
+
View
16 script/demoscene_maze.coffee → examples/asteroids/src/scenes/maze.coffee
@@ -1,7 +1,6 @@
-
sceneclass["maze"] = class SceneMaze extends Scene
constructor: (@parent) ->
-
+ @camera = new Camera {"projection": "normal", "vpWidth": @parent.width, "vpHeight": @parent.height}
maze = new Sprite
"texture": "assets/images/walls.png"
"width": 100
@@ -30,16 +29,17 @@ sceneclass["maze"] = class SceneMaze extends Scene
"mapfile": "assets/maze.png"
"pattern": "cross"
"sprite": maze
-
+
@spaceships = []
for i in [0..3]
- @spaceships[i] = new Spaceship
-
+ @spaceships[i] = new Spaceship(@parent.eventManager, @parent.keyboard)
+
update: (delta) ->
for spaceship in @spaceships
spaceship.update delta
-
- render: (ctx) ->
- @background.render(ctx)
+
+ render: (ctx) ->
+ @background.render(ctx, @camera)
for spaceship in @spaceships
spaceship.render ctx
+
View
0 stylesheets/css/main.css → examples/asteroids/stylesheets/css/main.css
File renamed without changes.
View
0 stylesheets/main.sass → examples/asteroids/stylesheets/main.sass
File renamed without changes.
View
26 examples/asteroids/watch.rb
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+
+require 'directory_watcher'
+
+cake_cmd = "cake"
+
+dw = DirectoryWatcher.new '.'
+dw.interval = 1.0
+dw.glob = 'src/**/*.coffee'
+dw.reset true
+dw.add_observer do |*args|
+ args.each do |event|
+ puts "#{Time.now.strftime("%I:%M:%S")} #{event.path} #{event.type}"
+ begin
+ `#{cake_cmd} build`
+ rescue Errno::ENOENT => e
+ warn "[WARNING] '#{cake_cmd}' not found. Trying 'cake.coffeescript'"
+ cake_cmd = "cake.coffeescript"
+ end
+ end
+end
+
+dw.start
+gets # when the user hits "enter" the script will terminate
+dw.stop
+
View
877 script/js/game.js → lib/irf.js
@@ -1,178 +1,28 @@
(function() {
- var Animation, Asteroids, Background, BoundingBox, Camera, EventManager, Game, Hero, Keyboard, Map, Scene, SceneBigBackground, SceneHeight, SceneIso, SceneJumpNRun, SceneManager, SceneMaze, Shape, Spaceship, Sprite, Tile, Timer, Vector, root, sceneclass;
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- };
- root = this;
- sceneclass = {};
- Array.prototype.shuffle = function() {
- return this.sort(function() {
- return 0.5 - Math.random();
- });
- };
- Number.prototype.toHex = function(padding) {
- var hex;
- if (padding == null) {
- padding = 2;
- }
- hex = this.toString(16);
- while (hex.length < padding) {
- hex = "0" + hex;
- }
- return hex;
- };
- Timer = (function() {
- function Timer() {
- this.last_time = new Date().getTime();
- this.delta = 0;
- }
- Timer.prototype.punch = function() {
- var this_time;
- this_time = new Date().getTime();
- this.delta = this_time - this.last_time;
- this.last_time = this_time;
- return this.delta;
- };
- Timer.prototype.timeSinceLastPunch = function() {
- var this_time;
- this_time = new Date().getTime();
- return this_time - this.last_time;
- };
- Timer.prototype.fps = function() {
- return 1000 / this.delta;
- };
- return Timer;
- })();
- Vector = (function() {
- function Vector(x, y) {
- if (x == null) {
- x = 0;
- }
- if (y == null) {
- y = 0;
- }
- this.x = x;
- this.y = y;
+ var Animation, Background, BoundingBox, Camera, EventManager, Game, Keyboard, Map, Scene, SceneManager, Shape, Sprite, Tile, Timer, Vector, c, classes, root, sceneclass, _i, _len;
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ Background = (function() {
+ function Background(sprite) {
+ this.sprite = sprite;
+ this.sprite.addImage("background", 0);
}
- Vector.prototype.clone = function() {
- return new Vector(this.x, this.y);
- };
- Vector.prototype.add = function(vec) {
- return new Vector(this.x + vec.x, this.y + vec.y);
- };
- Vector.prototype.add_ = function(vec) {
- this.x += vec.x;
- return this.y += vec.y;
- };
- Vector.prototype.subtract = function(vec) {
- return new Vector(this.x - vec.x, this.y - vec.y);
- };
- Vector.prototype.subtract_ = function(vec) {
- this.x -= vec.x;
- return this.y -= vec.y;
- };
- Vector.prototype.mult = function(num) {
- return new Vector(this.x * num, this.y * num);
- };
- Vector.prototype.mult_ = function(num) {
- this.x *= num;
- return this.y *= num;
- };
- Vector.prototype.length = function() {
- return Math.sqrt(this.x * this.x + this.y * this.y);
- };
- Vector.prototype.lengthSquared = function() {
- return this.x * this.x + this.y * this.y;
- };
- Vector.prototype.norm = function(factor) {
- if (factor == null) {
- factor = 1;
- }
- if (this.length() > 0) {
- return this.mult(factor / l);
- } else {
- return null;
- }
- };
- Vector.prototype.norm_ = function(factor) {
- if (factor == null) {
- factor = 1;
- }
- if (this.length() > 0) {
- return this.mult_(factor / l);
- } else {
- return null;
- }
- };
- Vector.prototype.scalarProduct = function(vec) {
- return this.x * vec.x + this.y * vec.y;
- };
- Vector.prototype.sameDirection = function(vec) {
- if (this.lengthSquared() < this.add(vec).lengthSquared()) {
- return true;
- } else {
- return false;
- }
- };
- Vector.prototype.angleWith = function(vec) {
- return Math.acos(this.scalarProduct(vec) / this.length() * vec.length());
- };
- Vector.prototype.vectorProduct = function(vec) {
- return this;
- };
- Vector.prototype.projectTo = function(vec) {
- return vec.mult(this.scalarProduct(vec) / vec.lengthSquared());
- };
- Vector.prototype.projectTo_ = function(vec) {
- var m;
- m = this.scalarProduct(vec) / vec.lengthSquared();
- this.x *= m;
- return this.y *= m;
- };
- Vector.intersecting = function(oa, a, ob, b) {
- var c, col, l, m, mu, mult, n;
- c = ob.subtract(oa);
- b = b.mult(-1);
- col = [];
- col[0] = a.clone();
- col[1] = b.clone();
- col[2] = c.clone();
- l = 0;
- m = 1;
- n = 2;
- mult = col[0].y / col[0].x;
- col[0].y = 0;
- col[1].y = col[1].y - (mult * col[1].x);
- col[2].y = col[2].y - (mult * col[2].x);
- mu = col[n].y / col[m].y;
- return ob.subtract(b.mult(mu));
- };
- Vector.prototype.print = function() {
- return "(" + this.x + ", " + this.y + ")";
+ Background.prototype.render = function(ctx) {
+ return this.sprite.render("background", ctx);
};
- return Vector;
+ return Background;
})();
BoundingBox = (function() {
function BoundingBox(coor, dim, color) {
var _ref, _ref2;
this.coor = coor;
this.dim = dim;
this.color = color != null ? color : "grey";
- if ((_ref = this.coor) != null) {
- _ref;
- } else {
+ if ((_ref = this.coor) == null) {
this.coor = new Vector;
- };
- if ((_ref2 = this.dim) != null) {
- _ref2;
- } else {
+ }
+ if ((_ref2 = this.dim) == null) {
this.dim = new Vector;
- };
+ }
}
BoundingBox.prototype.intersect = function(otherBB) {
return this.intersectv(otherBB) && this.intersecth(otherBB);
@@ -213,6 +63,34 @@
};
return BoundingBox;
})();
+ Camera = (function() {
+ function Camera(hash) {
+ var _ref;
+ this.projection = hash["projection"];
+ this.vpWidth = hash["vpWidth"];
+ this.vpHeight = hash["vpHeight"];
+ this.zoomFactor = (_ref = hash["zoomFactor"]) != null ? _ref : 1;
+ this.coor = new Vector(100, 100);
+ }
+ Camera.prototype.update = function(delta) {};
+ Camera.prototype.apply = function(ctx, callback) {
+ switch (this.projection) {
+ case "normal":
+ ctx.save();
+ ctx.translate(this.vpWidth / 2 - this.coor.x, this.vpHeight / 2 - this.coor.y);
+ callback();
+ return ctx.restore();
+ case "iso":
+ ctx.save();
+ ctx.scale(1, 0.5);
+ ctx.rotate(Math.PI / 4);
+ ctx.translate(200, -400);
+ callback();
+ return ctx.restore();
+ }
+ };
+ return Camera;
+ })();
EventManager = (function() {
function EventManager() {
this.eventlist = {};
@@ -235,6 +113,58 @@
};
return EventManager;
})();
+ Game = (function() {
+ function Game(width, height) {
+ var canvas;
+ this.width = width;
+ this.height = height;
+ this.gameloop = __bind(this.gameloop, this);
+ canvas = $('<canvas/>').attr({
+ "width": this.width,
+ "height": this.height
+ });
+ $("body").append(canvas);
+ this.ctx = canvas[0].getContext('2d');
+ this.ctx.font = '400 18px Helvetica, sans-serif';
+ this.loop = null;
+ this.timer = new Timer;
+ }
+ Game.prototype.gameloop = function() {
+ this.update();
+ return this.render();
+ };
+ Game.prototype.start = function() {
+ return this.loop = setInterval(this.gameloop, 1);
+ };
+ Game.prototype.stop = function() {
+ return this.loop.clearInterval();
+ };
+ Game.prototype.update = function() {
+ return this.timer.punch();
+ };
+ Game.prototype.render = function() {
+ return this.ctx.clearRect(0, 0, this.width, this.height);
+ };
+ return Game;
+ })();
+ root = this;
+ sceneclass = {};
+ Array.prototype.shuffle = function() {
+ return this.sort(function() {
+ return 0.5 - Math.random();
+ });
+ };
+ Number.prototype.toHex = function(padding) {
+ var hex;
+ if (padding == null) {
+ padding = 2;
+ }
+ hex = this.toString(16);
+ while (hex.length < padding) {
+ hex = "0" + hex;
+ }
+ return hex;
+ };
Keyboard = (function() {
function Keyboard() {
var code, name, _ref;
@@ -311,40 +241,6 @@
};
return Keyboard;
})();
- Game = (function() {
- function Game(width, height) {
- var canvas;
- this.width = width;
- this.height = height;
- this.gameloop = __bind(this.gameloop, this);
- canvas = $('<canvas/>').attr({
- "width": this.width,
- "height": this.height
- });
- $("body").append(canvas);
- this.ctx = canvas[0].getContext('2d');
- this.ctx.font = '400 18px Helvetica, sans-serif';
- this.loop = null;
- this.timer = new Timer;
- }
- Game.prototype.gameloop = function() {
- this.update();
- return this.render();
- };
- Game.prototype.start = function() {
- return this.loop = setInterval(this.gameloop, 1);
- };
- Game.prototype.stop = function() {
- return this.loop.clearInterval();
- };
- Game.prototype.update = function() {
- return this.timer.punch();
- };
- Game.prototype.render = function() {
- return this.ctx.clearRect(0, 0, this.width, this.height);
- };
- return Game;
- })();
Map = (function() {
function Map(hash) {
this.sprite = hash["sprite"];
@@ -394,11 +290,9 @@
for (i = 0, _len = data.length, _step = 4; i < _len; i += _step) {
p = data[i];
row = Math.floor((i / 4) / this.map.width);
- if ((_ref = (_base = this.mapData)[row]) != null) {
- _ref;
- } else {
+ if ((_ref = (_base = this.mapData)[row]) == null) {
_base[row] = [];
- };
+ }
this.mapData[row].push([Number(data[i]).toHex(), Number(data[i + 1]).toHex(), Number(data[i + 2]).toHex(), Number(data[i + 3]).toHex()]);
}
this.read();
@@ -451,13 +345,13 @@
return _results;
};
Map.prototype.readCross = function() {
- var col, green, row, type, z, _ref, _results, _step;
+ var col, green, row, type, z, _ref, _results;
_results = [];
- for (row = 1, _ref = this.map.height - 2, _step = 2; 1 <= _ref ? row <= _ref : row >= _ref; row += _step) {
+ for (row = 1, _ref = this.map.height - 2; row <= _ref; row += 2) {
_results.push((function() {
- var _ref2, _results2, _step2;
+ var _ref2, _results2;
_results2 = [];
- for (col = 1, _ref2 = this.map.width - 2, _step2 = 2; 1 <= _ref2 ? col <= _ref2 : col >= _ref2; col += _step2) {
+ for (col = 1, _ref2 = this.map.width - 2; col <= _ref2; col += 2) {
_results2.push(this.mapData[row][col][0] !== "00" ? (type = "" + this.mapData[row - 1][col][0] + this.mapData[row][col + 1][0] + this.mapData[row + 1][col][0] + this.mapData[row][col - 1][0], green = parseInt(this.mapData[row][col][1], 16), z = parseInt(this.mapData[row][col][2], 16), this.tiles.push(new Tile(this.sprite, type, row / 2, col / 2, green, z))) : void 0);
}
return _results2;
@@ -503,15 +397,33 @@
};
return Tile;
})();
- Background = (function() {
- function Background(sprite) {
- this.sprite = sprite;
- this.sprite.addImage("background", 0);
+ Scene = (function() {
+ function Scene() {}
+ Scene.prototype.update = function() {};
+ Scene.prototype.render = function() {};
+ return Scene;
+ })();
+ SceneManager = (function() {
+ function SceneManager(parent, scenes) {
+ var klass, name;
+ this.parent = parent;
+ this.scenearray = {};
+ this.currentScene = null;
+ for (name in scenes) {
+ klass = scenes[name];
+ this.addScene(name, klass);
+ }
}
- Background.prototype.render = function(ctx) {
- return this.sprite.render("background", ctx);
+ SceneManager.prototype.addScene = function(name, klass) {
+ this.scenearray[name] = new klass(this.parent);
+ if (this.currentScene == null) {
+ return this.setScene(name);
+ }
};
- return Background;
+ SceneManager.prototype.setScene = function(scene) {
+ return this.currentScene = this.scenearray[scene];
+ };
+ return SceneManager;
})();
Sprite = (function() {
function Sprite(hash) {
@@ -614,464 +526,141 @@
};
return Animation;
})();
- Scene = (function() {
- function Scene() {}
- Scene.prototype.update = function() {};
- Scene.prototype.render = function() {};
- return Scene;
- })();
- SceneManager = (function() {
- function SceneManager(parent, scenes) {
- var scene, _i, _len;
- this.parent = parent;
- this.scenearray = {};
- this.currentScene = null;
- for (_i = 0, _len = scenes.length; _i < _len; _i++) {
- scene = scenes[_i];
- this.addScene(scene);
- }
- }
- SceneManager.prototype.addScene = function(scene) {
- this.scenearray[scene] = new sceneclass[scene](this.parent);
- if (this.currentScene == null) {
- return this.setScene(scene);
- }
- };
- SceneManager.prototype.setScene = function(scene) {
- return this.currentScene = this.scenearray[scene];
- };
- return SceneManager;
- })();
- Camera = (function() {
- function Camera(hash) {
- var _ref;
- this.projection = hash["projection"];
- this.vpWidth = hash["vpWidth"];
- this.vpHeight = hash["vpHeight"];
- this.zoomFactor = (_ref = hash["zoomFactor"]) != null ? _ref : 1;
- this.coor = new Vector(100, 100);
+ Timer = (function() {
+ function Timer() {
+ this.last_time = new Date().getTime();
+ this.delta = 0;
}
- Camera.prototype.update = function(delta) {};
- Camera.prototype.apply = function(ctx, callback) {
- switch (this.projection) {
- case "normal":
- ctx.save();
- ctx.translate(this.vpWidth / 2 - this.coor.x, this.vpHeight / 2 - this.coor.y);
- callback();
- return ctx.restore();
- case "iso":
- ctx.save();
- ctx.scale(1, 0.5);
- ctx.rotate(Math.PI / 4);
- ctx.translate(200, -400);
- callback();
- return ctx.restore();
- }
+ Timer.prototype.punch = function() {
+ var this_time;
+ this_time = new Date().getTime();
+ this.delta = this_time - this.last_time;
+ this.last_time = this_time;
+ return this.delta;
};
- return Camera;
- })();
- Asteroids = (function() {
- __extends(Asteroids, Game);
- function Asteroids(width, height) {
- Asteroids.__super__.constructor.call(this, width, height);
- this.eventManager = new EventManager;
- this.keyboard = new Keyboard;
- this.sceneManager = new SceneManager(this, ["bigbg", "jumpnrun", "iso", "maze", "height"]);
- this.sceneManager.setScene("jumpnrun");
- }
- Asteroids.prototype.update = function() {
- Asteroids.__super__.update.call(this);
- return this.sceneManager.currentScene.update(this.timer.delta);
+ Timer.prototype.timeSinceLastPunch = function() {
+ var this_time;
+ this_time = new Date().getTime();
+ return this_time - this.last_time;
};
- Asteroids.prototype.render = function() {
- Asteroids.__super__.render.call(this);
- this.sceneManager.currentScene.render(this.ctx);
- return this.ctx.fillText(this.timer.fps().toFixed(1), this.width - 50, 20);
+ Timer.prototype.fps = function() {
+ return 1000 / this.delta;
};
- return Asteroids;
+ return Timer;
})();
- jQuery(function() {
- var asteroids;
- asteroids = new Asteroids(1024, 768);
- return asteroids.start();
- });
- sceneclass["bigbg"] = SceneBigBackground = (function() {
- __extends(SceneBigBackground, Scene);
- function SceneBigBackground(parent) {
- var backgroundsprite, i;
- this.parent = parent;
- console.log("width: " + this.parent.width + " -- height: " + this.parent.height);
- backgroundsprite = new Sprite({
- "texture": "assets/images/weltraum.jpg",
- "width": 500,
- "height": 500
- });
- this.background = new Background(backgroundsprite);
- this.spaceships = [];
- for (i = 0; i <= 3; i++) {
- this.spaceships[i] = new Spaceship;
+ Vector = (function() {
+ function Vector(x, y) {
+ if (x == null) {
+ x = 0;
}
- }
- SceneBigBackground.prototype.update = function(delta) {
- var spaceship, _i, _len, _ref, _results;
- _ref = this.spaceships;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- spaceship = _ref[_i];
- _results.push(spaceship.update(delta));
+ if (y == null) {
+ y = 0;
}
- return _results;
+ this.x = x;
+ this.y = y;
+ }
+ Vector.prototype.clone = function() {
+ return new Vector(this.x, this.y);
};
- SceneBigBackground.prototype.render = function(ctx) {
- var spaceship, _i, _len, _ref, _results;
- this.background.render(ctx);
- _ref = this.spaceships;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- spaceship = _ref[_i];
- _results.push(spaceship.render(ctx));
- }
- return _results;
+ Vector.prototype.add = function(vec) {
+ return new Vector(this.x + vec.x, this.y + vec.y);
};
- return SceneBigBackground;
- })();
- sceneclass["height"] = SceneHeight = (function() {
- __extends(SceneHeight, Scene);
- function SceneHeight(parent) {
- var simple;
- this.parent = parent;
- simple = new Sprite({
- "texture": "assets/images/beach3d.png",
- "width": 107,
- "height": 107,
- "innerWidth": 87,
- "innerHeight": 87,
- "key": {
- "00": 12,
- "dd": 12
- }
- });
- this.background = new Map({
- "mapfile": "assets/minimap.png",
- "pattern": "simple",
- "sprite": simple
- });
- }
- SceneHeight.prototype.update = function(delta) {};
- SceneHeight.prototype.render = function(ctx) {
- return this.background.render(ctx);
+ Vector.prototype.add_ = function(vec) {
+ this.x += vec.x;
+ return this.y += vec.y;
};
- return SceneHeight;
- })();
- sceneclass["iso"] = SceneIso = (function() {
- __extends(SceneIso, Scene);
- function SceneIso(parent) {
- var beach3d;
- this.parent = parent;
- this.camera = new Camera({
- "projection": "iso",
- "vpWidth": this.parent.width,
- "vpHeight": this.parent.height
- });
- beach3d = new Sprite({
- "texture": "assets/images/beach3d.png",
- "width": 107,
- "height": 107,
- "innerWidth": 87,
- "innerHeight": 87,
- "key": {
- "dd00dddd": 0,
- "00dddddd": 1,
- "dddd00dd": 2,
- "dddddd00": 3,
- "dd00dd00": 4,
- "0000dddd": 5,
- "00dd00dd": 6,
- "dddd0000": 7,
- "0000dd00": 8,
- "000000dd": 9,
- "00dd0000": 10,
- "dd000000": 11,
- "dddddddd": 12,
- "00000000": 13,
- "dd0000dd": 14,
- "00dddd00": 15
- }
- });
- this.background = new Map({
- "mapfile": "assets/map.png",
- "pattern": "square",
- "sprite": beach3d
- });
- }
- SceneIso.prototype.update = function(delta) {};
- SceneIso.prototype.render = function(ctx) {
- return this.camera.apply(ctx, __bind(function() {
- return this.background.render(ctx, this.camera);
- }, this));
+ Vector.prototype.subtract = function(vec) {
+ return new Vector(this.x - vec.x, this.y - vec.y);
};
- return SceneIso;
- })();
- sceneclass["jumpnrun"] = SceneJumpNRun = (function() {
- __extends(SceneJumpNRun, Scene);
- function SceneJumpNRun(parent) {
- var customReadFunction, i, jumpnrunSprite;
- this.parent = parent;
- this.hero = new Hero(this.parent.eventManager, this.parent.keyboard);
- this.camera = new Camera({
- "projection": "normal",
- "vpWidth": this.parent.width,
- "vpHeight": this.parent.height
- });
- jumpnrunSprite = new Sprite({
- "texture": "assets/images/jumpnrun.png",
- "width": 100,
- "height": 100,
- "innerWidth": 95,
- "innerHeight": 95,
- "key": {
- "00": 0,
- "11": 1,
- '22': 2,
- "33": 3,
- "44": 4,
- '55': 5,
- "66": 6,
- "77": 7,
- '88': 8,
- "99": 9,
- "aa": 10,
- "bb": 11
- }
- });
- customReadFunction = function() {
- var col, green, row, type, z, _ref, _results;
- _results = [];
- for (row = 0, _ref = this.map.height - 1; 0 <= _ref ? row <= _ref : row >= _ref; 0 <= _ref ? row++ : row--) {
- _results.push((function() {
- var _ref2, _results2;
- _results2 = [];
- for (col = 0, _ref2 = this.map.width - 1; 0 <= _ref2 ? col <= _ref2 : col >= _ref2; 0 <= _ref2 ? col++ : col--) {
- type = "" + this.mapData[row][col][0];
- green = parseInt(this.mapData[row][col][1], 16);
- z = parseInt(this.mapData[row][col][2], 16);
- _results2.push(this.tiles.push(new Tile(this.sprite, type, row, col, green, z)));
- }
- return _results2;
- }).call(this));
- }
- return _results;
- };
- this.background = new Map({
- "mapfile": "assets/jumpnrun_map.png",
- "pattern": customReadFunction,
- "sprite": jumpnrunSprite
- });
- this.spaceships = [];
- for (i = 0; i <= 3; i++) {
- this.spaceships[i] = new Spaceship(this.parent.eventManager, this.parent.keyboard);
- }
- }
- SceneJumpNRun.prototype.update = function(delta) {
- var spaceship, _i, _len, _ref, _results;
- this.hero.update(delta, this.background);
- this.camera.coor = this.hero.coor;
- _ref = this.spaceships;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- spaceship = _ref[_i];
- _results.push(spaceship.update(delta));
- }
- return _results;
+ Vector.prototype.subtract_ = function(vec) {
+ this.x -= vec.x;
+ return this.y -= vec.y;
};
- SceneJumpNRun.prototype.render = function(ctx) {
- return this.camera.apply(ctx, __bind(function() {
- var spaceship, _i, _len, _ref, _results;
- this.background.render(ctx, this.camera);
- this.hero.render(ctx);
- _ref = this.spaceships;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- spaceship = _ref[_i];
- _results.push(spaceship.render(ctx));
- }
- return _results;
- }, this));
+ Vector.prototype.mult = function(num) {
+ return new Vector(this.x * num, this.y * num);
};
- return SceneJumpNRun;
- })();
- sceneclass["maze"] = SceneMaze = (function() {
- __extends(SceneMaze, Scene);
- function SceneMaze(parent) {
- var i, maze;
- this.parent = parent;
- maze = new Sprite({
- "texture": "assets/images/walls.png",
- "width": 100,
- "height": 100,
- "innerWidth": 50,
- "innerHeight": 50,
- "key": {
- "dddddddd": 0,
- "dd00dddd": 1,
- "dddd00dd": 2,
- "dddddd00": 3,
- "00dddddd": 4,
- "00000000": 5,
- "00dddd00": 6,
- "0000dddd": 7,
- "dd0000dd": 8,
- "dddd0000": 9,
- "00dd00dd": 12,
- "dd00dd00": 13,
- "00dd0000": 14,
- "0000dd00": 15,
- "000000dd": 16,
- "dd000000": 17
- }
- });
- this.background = new Map({
- "mapfile": "assets/maze.png",
- "pattern": "cross",
- "sprite": maze
- });
- this.spaceships = [];
- for (i = 0; i <= 3; i++) {
- this.spaceships[i] = new Spaceship;
- }
- }
- SceneMaze.prototype.update = function(delta) {
- var spaceship, _i, _len, _ref, _results;
- _ref = this.spaceships;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- spaceship = _ref[_i];
- _results.push(spaceship.update(delta));
- }
- return _results;
+ Vector.prototype.mult_ = function(num) {
+ this.x *= num;
+ return this.y *= num;
};
- SceneMaze.prototype.render = function(ctx) {
- var spaceship, _i, _len, _ref, _results;
- this.background.render(ctx);
- _ref = this.spaceships;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- spaceship = _ref[_i];
- _results.push(spaceship.render(ctx));
- }
- return _results;
+ Vector.prototype.length = function() {
+ return Math.sqrt(this.x * this.x + this.y * this.y);
};
- return SceneMaze;
- })();
- Spaceship = (function() {
- function Spaceship(eventManager, keyboard) {
- this.eventManager = eventManager;
- this.keyboard = keyboard;
- this.state = "normal";
- this.sprite = new Sprite({
- "texture": "assets/images/test.png",
- "width": 50,
- "height": 50
- });
- this.sprite.addImage("normal", Math.floor(Math.random() * 10));
- this.coor = new Vector(Math.random() * 1024, Math.random() * 768);
- this.speed = new Vector(0.1, 0.1);
- if (Math.random() > 0.5) {
- this.speed = this.speed.mult(-1);
+ Vector.prototype.lengthSquared = function() {
+ return this.x * this.x + this.y * this.y;
+ };
+ Vector.prototype.norm = function(factor) {
+ if (factor == null) {
+ factor = 1;
}
- }
- Spaceship.prototype.update = function(delta) {
- this.coor = this.coor.add(this.speed.mult(delta));
- if (this.coor.x > 1024) {
- this.speed.x = this.speed.x * -1;
- this.coor.x = 1024;
- this.eventManager.trigger("touchdown");
+ if (this.length() > 0) {
+ return this.mult(factor / l);
+ } else {
+ return null;
}
- if (this.coor.x < 0) {
- this.speed.x = this.speed.x * -1;
- this.coor.x = 0;
+ };
+ Vector.prototype.norm_ = function(factor) {
+ if (factor == null) {
+ factor = 1;
}
- if (this.coor.y > 768) {
- this.speed.y = this.speed.y * -1;
- this.coor.y = 768;
+ if (this.length() > 0) {
+ return this.mult_(factor / l);
+ } else {
+ return null;
}
- if (this.coor.y < 0) {
- this.speed.y = this.speed.y * -1;
- return this.coor.y = 0;
+ };
+ Vector.prototype.scalarProduct = function(vec) {
+ return this.x * vec.x + this.y * vec.y;
+ };
+ Vector.prototype.sameDirection = function(vec) {
+ if (this.lengthSquared() < this.add(vec).lengthSquared()) {
+ return true;
+ } else {
+ return false;
}
};
- Spaceship.prototype.touchdown = function() {
- return console.log("Spaceship says: Touchdown");
+ Vector.prototype.angleWith = function(vec) {
+ return Math.acos(this.scalarProduct(vec) / this.length() * vec.length());
};
- Spaceship.prototype.render = function(ctx) {
- ctx.save();
- ctx.translate(this.coor.x, this.coor.y);
- this.sprite.render(this.state, ctx);
- return ctx.restore();
+ Vector.prototype.vectorProduct = function(vec) {
+ return this;
};
- Spaceship.prototype.hello = function() {
- return console.log("hello!");
+ Vector.prototype.projectTo = function(vec) {
+ return vec.mult(this.scalarProduct(vec) / vec.lengthSquared());
};
- return Spaceship;
- })();
- Hero = (function() {
- function Hero(eventManager, keyboard) {
- this.eventManager = eventManager;
- this.keyboard = keyboard;
- this.state = "normal";
- this.sprite = new Sprite({
- "texture": "assets/images/test.png",
- "width": 50,
- "height": 50,
- "key": {
- "normal": 3,
- "jumping": 5
- }
- });
- this.coor = new Vector(100, 100);
- this.speed = new Vector(0, 0);
- this.force = new Vector(0.01, 0);
- this.gravity = new Vector(0, 0.01);
- this.bb = new BoundingBox(this.coor, new Vector(50, 50));
- this.bb.color = "red";
- this.eventManager.register("touchdown", this.touchdown);
- }
- Hero.prototype.touchdown = function() {
- return console.log("Hero says: Touchdown occurred");
+ Vector.prototype.projectTo_ = function(vec) {
+ var m;
+ m = this.scalarProduct(vec) / vec.lengthSquared();
+ this.x *= m;
+ return this.y *= m;
};
- Hero.prototype.update = function(delta, map) {
- var tileBelow;
- tileBelow = map.tileAtVector(this.coor).neighbor["s"];
- this.speed.add_(this.gravity);
- if (this.bb.intersect(tileBelow.bb) && !(typeof tileBelow.isWalkable === "function" ? tileBelow.isWalkable() : void 0)) {
- this.speed.y = 0;
- this.state = "normal";
- }
- if (this.keyboard.key("right")) {
- this.speed.add_(this.force);
- } else if (this.keyboard.key("left")) {
- this.speed.subtract_(this.force);
- } else {
- if (this.speed.x > 0) {
- this.speed.subtract_(this.force);
- } else {
- this.speed.add_(this.force);
- }
- }
- if (this.keyboard.key("space") && this.state !== "jumping") {
- this.state = "jumping";
- this.speed.y = -0.7;
- }
- this.coor = this.coor.add(this.speed.mult(delta));
- return this.bb.coor = this.coor;
+ Vector.intersecting = function(oa, a, ob, b) {
+ var c, col, l, m, mu, mult, n;
+ c = ob.subtract(oa);
+ b = b.mult(-1);
+ col = [];
+ col[0] = a.clone();
+ col[1] = b.clone();
+ col[2] = c.clone();
+ l = 0;
+ m = 1;
+ n = 2;
+ mult = col[0].y / col[0].x;
+ col[0].y = 0;
+ col[1].y = col[1].y - (mult * col[1].x);
+ col[2].y = col[2].y - (mult * col[2].x);
+ mu = col[n].y / col[m].y;
+ return ob.subtract(b.mult(mu));
};
- Hero.prototype.render = function(ctx) {
- ctx.save();
- ctx.translate(this.coor.x, this.coor.y);
- this.sprite.render(this.state, ctx);
- ctx.restore();
- return this.bb.render(ctx);
+ Vector.prototype.print = function() {
+ return "(" + this.x + ", " + this.y + ")";
};
- return Hero;
+ return Vector;
})();
+ classes = ["Background", "BoundingBox", "Camera", "EventManager", "Game", "Keyboard", "Map", "Scene", "SceneManager", "Sprite", "Shape", "Animation", "Tile", "Timer", "Vector"];