mirror of
https://github.com/flynx/Course-JavaScript.git
synced 2025-10-29 19:10:09 +00:00
cleanup and refactoring...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
f51e184074
commit
543cb4bba6
@ -30,7 +30,6 @@ function makeEvent(handler_attr){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX normalize x/y everywhere we input coordinates...
|
|
||||||
var Snake = {
|
var Snake = {
|
||||||
config: {
|
config: {
|
||||||
apple_color: 'red',
|
apple_color: 'red',
|
||||||
@ -44,6 +43,19 @@ var Snake = {
|
|||||||
field_size: null,
|
field_size: null,
|
||||||
|
|
||||||
// utils...
|
// utils...
|
||||||
|
normalize_point: function(point){
|
||||||
|
point = point || {}
|
||||||
|
|
||||||
|
var w = this.field_size.width
|
||||||
|
var x = point.x % w
|
||||||
|
x = x < 0 ? (x + w) : x
|
||||||
|
|
||||||
|
var h = this.field_size.height
|
||||||
|
var y = point.y % h
|
||||||
|
y = y < 0 ? (y + h) : y
|
||||||
|
|
||||||
|
return { x: x, y: y }
|
||||||
|
},
|
||||||
random_point: function(){
|
random_point: function(){
|
||||||
var cells = this._cells
|
var cells = this._cells
|
||||||
var l = cells.length
|
var l = cells.length
|
||||||
@ -59,9 +71,6 @@ var Snake = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// XXX BUG: going down into bottom left corner breaks the snake...
|
|
||||||
// ...check other corner cases!
|
|
||||||
// ...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
|
||||||
@ -116,7 +125,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
|
||||||
next.style.backgroundColor = ''
|
next.style.backgroundColor = ''
|
||||||
@ -127,18 +136,18 @@ var Snake = {
|
|||||||
that.snakeKilled(other)
|
that.snakeKilled(other)
|
||||||
that.snakeKilled(color)
|
that.snakeKilled(color)
|
||||||
|
|
||||||
// apples...
|
// apple -> grow age...
|
||||||
} else if(next.style.backgroundColor == that.config.apple_color){
|
} else if(next.style.backgroundColor == that.config.apple_color){
|
||||||
age += 1
|
age += 1
|
||||||
move = true
|
move = true
|
||||||
that.appleEaten()
|
that.appleEaten()
|
||||||
|
|
||||||
// empty...
|
// empty -> just move...
|
||||||
// NOTE: anything but an apple will kill the snake...
|
|
||||||
} else if(next.style.backgroundColor == ''){
|
} else if(next.style.backgroundColor == ''){
|
||||||
move = true
|
move = true
|
||||||
|
|
||||||
// kill...
|
// other -> kill...
|
||||||
|
// NOTE: anything but an apple or empty will kill the snake...
|
||||||
} else {
|
} else {
|
||||||
that.snakeKilled(color)
|
that.snakeKilled(color)
|
||||||
}
|
}
|
||||||
@ -183,9 +192,8 @@ var Snake = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// constructors...
|
// constructors...
|
||||||
// XXX normalize input x/y...
|
|
||||||
snake: function(color, point, direction, age){
|
snake: function(color, point, direction, age){
|
||||||
point = point || this.random_point()
|
point = this.normalize_point(point || this.random_point())
|
||||||
var head = this._cells[point.x + point.y * this.field_size.width]
|
var head = this._cells[point.x + point.y * this.field_size.width]
|
||||||
head.style.backgroundColor = color
|
head.style.backgroundColor = color
|
||||||
head.direction = direction
|
head.direction = direction
|
||||||
@ -196,13 +204,13 @@ var Snake = {
|
|||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
apple: function(point){
|
apple: function(point){
|
||||||
point = point || this.random_point()
|
point = this.normalize_point(point || this.random_point())
|
||||||
this._cells[point.x + point.y * this.field_size.width]
|
this._cells[point.x + point.y * this.field_size.width]
|
||||||
.style.backgroundColor = this.config.apple_color
|
.style.backgroundColor = this.config.apple_color
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
wall: function(point, direction, length){
|
wall: function(point, direction, length){
|
||||||
point = point || this.random_point()
|
point = this.normalize_point(point || this.random_point())
|
||||||
var x = point.x
|
var x = point.x
|
||||||
var y = point.y
|
var y = point.y
|
||||||
length = length || 1
|
length = length || 1
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user