mirror of
https://github.com/flynx/Course-JavaScript.git
synced 2025-10-29 19:10:09 +00:00
some refactoring + working on bug...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
a76b97a271
commit
4db1c6254a
@ -64,11 +64,9 @@ var Snake = {
|
|||||||
// ...looks like the only corner case...
|
// ...looks like the only corner case...
|
||||||
_tick: function(){
|
_tick: function(){
|
||||||
var that = this
|
var that = this
|
||||||
|
|
||||||
var l = this._cells.length
|
var l = this._cells.length
|
||||||
var w = this.field_size.width
|
var w = this.field_size.width
|
||||||
var h = this.field_size.height
|
var h = this.field_size.height
|
||||||
|
|
||||||
var tick = this.__tick = (this.__tick + 1 || 0)
|
var tick = this.__tick = (this.__tick + 1 || 0)
|
||||||
var directions = 'neswn'
|
var directions = 'neswn'
|
||||||
|
|
||||||
@ -94,12 +92,12 @@ var Snake = {
|
|||||||
// head...
|
// head...
|
||||||
var direction = cell.direction
|
var direction = cell.direction
|
||||||
var next =
|
var next =
|
||||||
direction == 'n' ? (i < w ? l - w + i : i - w)
|
direction == 'n' ? (i < w ? l - w + i : i - w)
|
||||||
: direction == 's' ? (i > (l-w) ? i - (l-w) : i + w)
|
// XXX BUG: this returns 256 when at x = 0...
|
||||||
: direction == 'e' ? ((i+1)%w == 0 ? i - (w-1) : i + 1)
|
: direction == 's' ? (i > (l-w) ? i - (l-w) : i + w)
|
||||||
: direction == 'w' ? (i%w == 0 ? i + (w-1) : i - 1)
|
: direction == 'e' ? ((i+1)%w == 0 ? i - (w-1) : i + 1)
|
||||||
|
: direction == 'w' ? (i%w == 0 ? i + (w-1) : i - 1)
|
||||||
: null
|
: null
|
||||||
|
|
||||||
if(next != null){
|
if(next != null){
|
||||||
next = that._cells[next]
|
next = that._cells[next]
|
||||||
|
|
||||||
@ -115,6 +113,7 @@ var Snake = {
|
|||||||
|
|
||||||
var age = cell.age
|
var age = cell.age
|
||||||
var move = false
|
var move = false
|
||||||
|
|
||||||
// special case: other snake's head -- kill both...
|
// special case: other snake's head -- kill both...
|
||||||
if(next.direction){
|
if(next.direction){
|
||||||
var other = next.style.backgroundColor
|
var other = next.style.backgroundColor
|
||||||
@ -171,7 +170,9 @@ var Snake = {
|
|||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
clear: function(){
|
clear: function(){
|
||||||
|
// this resets the cell object attrs...
|
||||||
this.setup()
|
this.setup()
|
||||||
|
// reset the actual cells...
|
||||||
this._cells.forEach(function(c){ c.style.backgroundColor = '' })
|
this._cells.forEach(function(c){ c.style.backgroundColor = '' })
|
||||||
this.players = {}
|
this.players = {}
|
||||||
delete this.__appleEatenHandlers
|
delete this.__appleEatenHandlers
|
||||||
@ -239,6 +240,8 @@ var Snake = {
|
|||||||
delete this.__tick
|
delete this.__tick
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
|
pause: function(){
|
||||||
|
return this.__timer ? this.stop() : this.start() },
|
||||||
left: function(color){
|
left: function(color){
|
||||||
this.players[color || Object.keys(this.players)[0]] = 'left'
|
this.players[color || Object.keys(this.players)[0]] = 'left'
|
||||||
return this
|
return this
|
||||||
@ -270,11 +273,12 @@ var HANDLER_SET = false
|
|||||||
var KEY_CONFIG = {
|
var KEY_CONFIG = {
|
||||||
ArrowLeft: ['left'],
|
ArrowLeft: ['left'],
|
||||||
ArrowRight: ['right'],
|
ArrowRight: ['right'],
|
||||||
|
' ': ['pause'],
|
||||||
}
|
}
|
||||||
function kbHandler(event){
|
function kbHandler(event){
|
||||||
var action = KEY_CONFIG[event.key]
|
var action = KEY_CONFIG[event.key]
|
||||||
action
|
action
|
||||||
&& action in Snake
|
&& action[0] in Snake
|
||||||
&& Snake[action[0]].apply(Snake, action.slice(1))
|
&& Snake[action[0]].apply(Snake, action.slice(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user