mirror of
https://github.com/flynx/Course-JavaScript.git
synced 2025-10-29 19:10:09 +00:00
some refactoring...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
4232171669
commit
2ab556118f
@ -11,6 +11,24 @@
|
|||||||
</style>
|
</style>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
// XXX automate clearing of handlers...
|
||||||
|
function makeEvent(handler_attr){
|
||||||
|
return function(func){
|
||||||
|
if(func instanceof Function){
|
||||||
|
var handlers = this[handler_attr] = this[handler_attr] || []
|
||||||
|
handlers.push(func)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
var that = this
|
||||||
|
var args = [].slice.call(arguments)
|
||||||
|
this[handler_attr]
|
||||||
|
&& this[handler_attr]
|
||||||
|
.forEach(function(handler){ handler.apply(that, args) })
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// XXX normalize x/y everywhere we input coordinates...
|
// XXX normalize x/y everywhere we input coordinates...
|
||||||
var Snake = {
|
var Snake = {
|
||||||
config: {
|
config: {
|
||||||
@ -102,11 +120,14 @@ var Snake = {
|
|||||||
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
|
||||||
next.style.backgroundColor = ''
|
next.style.backgroundColor = ''
|
||||||
// NOTE: we are not deleteing .direction here as
|
// NOTE: we are not deleteing .direction here as
|
||||||
// we can have upto 4 snakes colliding...
|
// we can have upto 4 snakes colliding...
|
||||||
next.direction = ''
|
next.direction = ''
|
||||||
delete next.age
|
delete next.age
|
||||||
|
that.snakeKilled(other)
|
||||||
|
that.snakeKilled(color)
|
||||||
|
|
||||||
// apples...
|
// apples...
|
||||||
} else if(next.style.backgroundColor == that.config.apple_color){
|
} else if(next.style.backgroundColor == that.config.apple_color){
|
||||||
@ -118,8 +139,13 @@ var Snake = {
|
|||||||
// NOTE: anything but an apple will kill the snake...
|
// NOTE: anything but an apple will kill the snake...
|
||||||
} else if(next.style.backgroundColor == ''){
|
} else if(next.style.backgroundColor == ''){
|
||||||
move = true
|
move = true
|
||||||
|
|
||||||
|
// kill...
|
||||||
|
} else {
|
||||||
|
that.snakeKilled(color)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// do the move...
|
||||||
if(move){
|
if(move){
|
||||||
next.tick = tick
|
next.tick = tick
|
||||||
next.style.backgroundColor = color
|
next.style.backgroundColor = color
|
||||||
@ -152,6 +178,7 @@ var Snake = {
|
|||||||
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
|
||||||
|
delete this.__killHandlers
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -200,21 +227,8 @@ var Snake = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// events...
|
// events...
|
||||||
appleEaten: function(func){
|
appleEaten: makeEvent('__appleEatenHandlers'),
|
||||||
if(func){
|
snakeKilled: makeEvent('__killHandlers'),
|
||||||
var handlers = this.__appleEatenHandlers = this.__appleEatenHandlers || []
|
|
||||||
handlers.push(func)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
var that = this
|
|
||||||
this.__appleEatenHandlers
|
|
||||||
&& this.__appleEatenHandlers
|
|
||||||
.forEach(function(handler){
|
|
||||||
handler.call(that)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return this
|
|
||||||
},
|
|
||||||
|
|
||||||
// actions...
|
// actions...
|
||||||
start: function(t){
|
start: function(t){
|
||||||
@ -236,10 +250,8 @@ var Snake = {
|
|||||||
this.players[color || Object.keys(this.players)[0]] = 'right'
|
this.players[color || Object.keys(this.players)[0]] = 'right'
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
}
|
|
||||||
|
|
||||||
|
// levels...
|
||||||
var Levels = {
|
|
||||||
basicLevel: function(){
|
basicLevel: function(){
|
||||||
return this
|
return this
|
||||||
.wall({x:3, y:10}, 's', 11)
|
.wall({x:3, y:10}, 's', 11)
|
||||||
@ -253,7 +265,6 @@ var Levels = {
|
|||||||
.wall(null, 'e', 11)
|
.wall(null, 'e', 11)
|
||||||
.wall(null, 'e', 11) },
|
.wall(null, 'e', 11) },
|
||||||
}
|
}
|
||||||
Snake.__proto__ = Levels
|
|
||||||
|
|
||||||
|
|
||||||
var HANDLER_SET = false
|
var HANDLER_SET = false
|
||||||
@ -278,12 +289,14 @@ function setup(){
|
|||||||
.clear()
|
.clear()
|
||||||
|
|
||||||
//.randomLevel()
|
//.randomLevel()
|
||||||
//.basicLevel()
|
.basicLevel()
|
||||||
|
|
||||||
.appleEaten(function(){ this.apple() })
|
.appleEaten(function(){ this.apple() })
|
||||||
.apple()
|
.apple()
|
||||||
.apple()
|
.apple()
|
||||||
|
|
||||||
|
// XXX do something better with direction...
|
||||||
|
.snakeKilled(function(color){ this.snake(color, null, 's', 3) })
|
||||||
.snake('blue', null, 's', 3)
|
.snake('blue', null, 's', 3)
|
||||||
|
|
||||||
.start(300)
|
.start(300)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user