Skip to content

Commit

Permalink
Entities!
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kelly committed May 10, 2012
1 parent 89292af commit 2c454a1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
Binary file added img/fly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 48 additions & 2 deletions js/demo/entities.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
define(function(require, exports) {
var Entity = require('flux/entity');
var TiledGraphic = require('flux/graphics/tiled');

function Door(x, y, width, height, to, to_x, to_y) {
var loader = require('demo/loader');

function ZeldaEntity(x, y) {
Entity.call(this, x, y);
this.orig_x = x;
this.orig_y = y;
}
ZeldaEntity.prototype = Object.create(Entity.prototype);
ZeldaEntity.prototype.reset = function() {
this.x = this.orig_x;
this.y = this.orig_y;
};

function Door(x, y, width, height, to, to_x, to_y) {
ZeldaEntity.call(this, x, y);
this.setHitbox(0, 0, width, height);
this.type = 'door';
this.to = to;
this.to_x = to_x;
this.to_y = to_y;
}
Door.prototype = Object.create(Entity.prototype);
Door.prototype = Object.create(ZeldaEntity.prototype);
exports.Door = Door;


loader.register('fly', 'img/fly.png', 'image');
function Fly(x, y) {
ZeldaEntity.call(this, x, y);
this.graphic = new TiledGraphic(loader.get('fly'), 8, 8);
this.graphic.addAnimationName('fly', [0, 10, 1, 10]);
this.graphic.currentTile = 'fly';
}
Fly.prototype = Object.create(Entity.prototype);
exports.Fly = Fly;
Fly.prototype.tick = function() {
ZeldaEntity.prototype.tick.call(this);
var range = 100;
this.angle += (((Math.random() * range) - (range / 2)) % 360);

this.speed += (Math.random() * 0.05);
if (this.speed < 0) this.speed = 0;
if (this.speed > 0.4) this.speed = 0.2;

var rad = this.angle * (Math.PI / 180);
var dx = Math.cos(rad) * this.speed;
var dy = Math.sin(rad) * this.speed;

this.x += dx;
this.y += dy;
};
Fly.prototype.reset = function() {
ZeldaEntity.prototype.reset.call(this);
this.speed = 0.3;
this.angle = 90;
};
});
14 changes: 10 additions & 4 deletions js/demo/tilemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ define(function(require) {

var map_entities = map.objectGroups['entities'].objects;
map_entities.forEach(function(entity) {
var cell_x = Math.floor(entity.x / cell_width);
var cell_y = Math.floor(entity.y / cell_height);
var x = entity.x % cell_width;
var y = entity.y % cell_height;

if (entity.type === 'door') {
var cell_x = Math.floor(entity.x / cell_width);
var cell_y = Math.floor(entity.y / cell_height);
var door = new entities.Door(
entity.x % cell_width, entity.y % cell_height,
var door = new entities.Door(x, y,
entity.width, entity.height,
entity.properties.to,
parseInt(entity.properties.x),
parseInt(entity.properties.y));
self.entities[cell_y][cell_x].push(door);
} else if (entity.type === 'entity') {
var type = entity.properties.type;
self.entities[cell_y][cell_x].push(new entities[type](x, y));
}
});
}
Expand All @@ -59,6 +64,7 @@ define(function(require) {
ZeldaTilemap.prototype.addEntities = function(engine) {
this.entities[this.cell_y][this.cell_x].forEach(function(entity) {
engine.addEntity(entity);
entity.reset();
});
};

Expand Down
40 changes: 40 additions & 0 deletions maps/overworld.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,45 @@
<property name="y" value="100"/>
</properties>
</object>
<object type="entity" x="372" y="148" width="8" height="8">
<properties>
<property name="type" value="Fly"/>
</properties>
</object>
<object type="entity" x="356" y="180" width="8" height="8">
<properties>
<property name="type" value="Fly"/>
</properties>
</object>
<object type="entity" x="436" y="212" width="8" height="8">
<properties>
<property name="type" value="Fly"/>
</properties>
</object>
<object type="entity" x="420" y="292" width="8" height="8">
<properties>
<property name="type" value="Fly"/>
</properties>
</object>
<object type="entity" x="436" y="324" width="8" height="8">
<properties>
<property name="type" value="Fly"/>
</properties>
</object>
<object type="entity" x="372" y="308" width="8" height="8">
<properties>
<property name="type" value="Fly"/>
</properties>
</object>
<object type="entity" x="52" y="164" width="8" height="8">
<properties>
<property name="type" value="Fly"/>
</properties>
</object>
<object type="entity" x="132" y="212" width="8" height="8">
<properties>
<property name="type" value="Fly"/>
</properties>
</object>
</objectgroup>
</map>

0 comments on commit 2c454a1

Please sign in to comment.