Skip to content

Commit

Permalink
Fix bug: remove AI for current player
Browse files Browse the repository at this point in the history
  • Loading branch information
mhluska committed Jun 24, 2016
1 parent b88d430 commit 03ca9d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
9 changes: 7 additions & 2 deletions source/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Game {
this._cameraFace = this._world._faceVectors[3];
this._cameraUpCached = this._camera.up.clone();

this._snake = new Snake(this._world, this._camera.up.clone(), this._cameraFace, { name: 'player' });
this._snakeEnemy = new Snake(this._world, this._camera.up.clone(), this._cameraFace.clone().negate(), { name: 'enemy', color: Const.Colors.ENEMY });
this._snake = this._initSnake({ type: 'player' });
this._snakeEnemy = this._initSnake({ type: 'enemy', color: Const.Colors.ENEMY });

this._lastTime = window.performance.now();

Expand Down Expand Up @@ -61,6 +61,11 @@ class Game {
this._animate();
}

_initSnake(options) {
assertTruthy(this._world, this._camera, this._cameraFace);
return new Snake(this._world, this._camera.up.clone(), this._cameraFace, options);
}

// TODO(maros): Move camera to its own class.
_setupScene(container) {
let scene = new THREE.Scene();
Expand Down
16 changes: 10 additions & 6 deletions source/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ let assertTruthy = require('./utils/assert-truthy');
let { Graph } = require('./graph');

module.exports = class Snake {
constructor(world, direction, face, { startPosition = null, color = Const.Colors.SNAKE, name = '' } = {}) {
constructor(world, direction, face, { startPosition = null, color = Const.Colors.SNAKE, type = 'player' } = {}) {
if (!startPosition) {
startPosition = Voxel.middleVoxel(face);
}

this.world = world;
this.speed = 0.15;
this.name = name;
this.type = type;
this.color = color;
this.face = face;
this.size = 6;
Expand Down Expand Up @@ -161,18 +161,22 @@ module.exports = class Snake {
assertTruthy(this.position, this._direction);

const voxel = Voxel.findOrCreate(this.position);
const next = voxel.next(this._direction);
const nextVoxel = voxel.next(this._direction);

if (next.type === 'snake') {
if (this.type === 'player' && nextVoxel.type === 'snake') {
return this.die();
}

if (nextVoxel.type === 'snake') {
for (let neighbor of voxel._next.values()) {
if (neighbor.type !== 'snake') {
return neighbor.position;
}
}

this.die();
return this.die();
} else {
return next.position;
return nextVoxel.position;
}
}

Expand Down

0 comments on commit 03ca9d5

Please sign in to comment.