Permalink
Browse files

Some realllllllly simple behavior

  • Loading branch information...
Dorthu committed Mar 13, 2016
1 parent f7fb832 commit a9b3e7adbc470b9b365a657e85e1baf1188aa9f5
Showing with 48 additions and 0 deletions.
  1. +21 −0 src/game/ai.js
  2. +13 −0 src/grid.js
  3. +14 −0 src/grid_object.js
View
@@ -12,6 +12,27 @@ class AI extends SolidItem {
This is where AI behavior happens.
In the base AI class, this will do nothing.
*/
this.ai_walk();
}
ai_walk() {
/*
This is the default walk behavior for an AI.
This will cause the AI to wander around the area
as is allowed.
*/
const dirs = [ { x: 0, z: 1 }, { x: 1, z: 0 }, { x: 0, z: -1 }, { x: -1, z: 0 } ];
let options = [ { x: 0, z: 0 } ];
for(let d of dirs) {
if(this.grid.can_move_to({x: this.loc.x + d.x, z: this.loc.z + d.z })) {
options.push(d);
}
}
let choice = options[Math.floor(Math.random() * options.length)];
if(choice.x || choice.z) {
this.grid.object_move(this, { x: this.loc.x + choice.x, y: this.loc.y, z: this.loc.z + choice.z });
}
}
}
View
@@ -45,6 +45,19 @@ class Grid {
return !loc.solid;
}
object_move(thing, pos) {
/*
This is for moving objects within spaces - do not remove thing
from the grid when using this operation.
*/
let o = this.get(thing.loc.x, thing.loc.z);
o.object = null;
let dest = this.get(pos.x, pos.z);
dest.object = thing;
thing.loc = pos;
thing.update_meshes();
}
transition(event) {
if(this.scene_change_callback) {
this.scene_change_callback(event.detail.data);
View
@@ -1,3 +1,4 @@
import assign from 'object-assign'
class GridObject {
constructor(grid, loc, mats, desc, extra) {
@@ -11,6 +12,19 @@ class GridObject {
this._mats = mats;
}
update_meshes() {
let pos = {y: .5};
assign(pos, this.loc);
pos.y = 0;
pos = this.grid.translate(pos);
for(let m of this.meshes) {
m.position.x = pos.x;
m.position.y = pos.y;
m.position.z = pos.z;
}
}
destroy() {
for(let m of this.meshes) {
this.grid.scene.remove(m);

0 comments on commit a9b3e7a

Please sign in to comment.