cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-04-12 19:49:30 +03:00
parent 68f77acd88
commit c533d129d4

View File

@ -42,7 +42,7 @@ var Snake = {
field_size: 32, field_size: 32,
apple_color: 'red', apple_color: 'red',
wall_color: 'silver', wall_color: 'silver',
interval: 200, interval: 150,
}, },
_field: null, _field: null,
@ -190,13 +190,13 @@ var Snake = {
}, },
// constructors... // constructors...
snake: function(color, point, direction, age){ snake: function(color, age, point, direction){
point = this.normalize_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 || this.random_direction head.direction = direction || this.random_direction
head.age = (age || 5) - 1 head.age = (age || 5) - 1
this.players[color] = '' this.players[color] = ''
return this return this
@ -306,14 +306,14 @@ var KEY_CONFIG = {
Left: ['left'], Left: ['left'],
Right: ['right'], Right: ['right'],
} }
function kbHandler(snake){ function makeKeyboardHandler(snake){
return function(event){ return function(event){
//console.log('KEY:', event.key) //console.log('KEY:', event.key)
var action = KEY_CONFIG[event.key] var action = KEY_CONFIG[event.key]
action action
&& action[0] in snake && action[0] in snake
&& snake[action[0]].apply(snake, action.slice(1)) }} && snake[action[0]].apply(snake, action.slice(1)) }}
function tapHandler(snake){ function makeTapHandler(snake){
return function(event){ return function(event){
// top of screen... // top of screen...
(event.clientY || event.changedTouches[0].pageY) <= (document.body.clientHeight / 8) ? (event.clientY || event.changedTouches[0].pageY) <= (document.body.clientHeight / 8) ?
@ -329,24 +329,22 @@ function tapHandler(snake){
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// XXX need to place the snake with some headroom in the // XXX need to place the snake with some headroom in the direction of
// direction of travel... // travel...
function setup(snake){ function setup(snake, timer, size){
snake = snake || Snake snake = snake || Snake
// setup kb handler (only once)... // setup kb handler (only once)...
if(!HANDLER_SET){ if(!HANDLER_SET){
document.addEventListener('keydown', kbHandler(snake)) document.addEventListener('keydown', makeKeyboardHandler(snake))
//document.addEventListener('click', tapHandler) document.addEventListener('touchstart', makeTapHandler(snake))
document.addEventListener('touchstart', tapHandler(snake))
HANDLER_SET = true HANDLER_SET = true
} }
return snake return snake
.setup('.simplesnake') .setup('.simplesnake', size)
//.basicLevel()
.randomLevel() .randomLevel()
.start(150) .start(timer)
.pause() .pause()
// stuff... // stuff...
@ -358,9 +356,8 @@ function setup(snake){
.snakeKilled(function(color){ .snakeKilled(function(color){
this this
.pause() .pause()
.snake(color, null, null, 3) .snake(color, 3) })
}) .snake('blue', 3)
.snake('blue', null, null, 3)
} }