some refactroing + tweaking...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-04-21 20:31:16 +03:00
parent 08d8841ab2
commit 971b3bf711
2 changed files with 53 additions and 28 deletions

View File

@ -1,5 +1,5 @@
CACHE MANIFEST CACHE MANIFEST
# Timestamp: 20170421194304 # Timestamp: 20170421203054
CACHE: CACHE:
simplesnake.html simplesnake.html

View File

@ -95,16 +95,18 @@ var Snake = {
return ('nesw')[Math.floor(Math.random() * 4)] }, return ('nesw')[Math.floor(Math.random() * 4)] },
// utils... // utils...
call: function(func){
return func.apply(this, [].slice.call(arguments, 1)) },
apply: function(func, args){
return func.apply(this, args) },
normalize_point: function(point){ normalize_point: function(point){
point = point || {} point = point || {}
var w = this.field_size.width var w = this.field_size.width
var x = point.x % w var x = point.x % w
x = x < 0 ? (x + w) : x x = x < 0 ? (x + w) : x
var h = this.field_size.height var h = this.field_size.height
var y = point.y % h var y = point.y % h
y = y < 0 ? (y + h) : y y = y < 0 ? (y + h) : y
return { x: x, y: y } return { x: x, y: y }
}, },
_make_field: function(w){ _make_field: function(w){
@ -247,7 +249,9 @@ var Snake = {
length = length || 1 length = length || 1
while(length > 0){ while(length > 0){
this._cells[x + y * this.field_size.width].classList.add('wall') var c = this._cells[x + y * this.field_size.width]
c.classList.add('wall')
c.style.backgroundColor = ''
x += direction == 'e' ? 1 x += direction == 'e' ? 1
: direction == 'w' ? -1 : direction == 'w' ? -1
@ -264,6 +268,12 @@ var Snake = {
return this return this
}, },
level: function(level){
var that = this
level.forEach(function(wall){
that.wall.apply(that, wall) })
return this
},
// events... // events...
snakeKilled: makeEvent('__killHandlers'), snakeKilled: makeEvent('__killHandlers'),
@ -274,8 +284,9 @@ var Snake = {
gameStopped: makeEvent('__stopHandlers'), gameStopped: makeEvent('__stopHandlers'),
// actions... // actions...
setup: function(field, size){ setup: function(field, size, interval){
this.config.field_size = size || this.config.field_size this.config.field_size = size || this.config.field_size
this.config.interval = interval || this.config.interval
field = field || this._field field = field || this._field
field = this._field = typeof(field) == typeof('str') ? document.querySelector(field) field = this._field = typeof(field) == typeof('str') ? document.querySelector(field)
: field : field
@ -318,27 +329,20 @@ 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...
randomLevel: function(){
var a = Math.round(this.field_size.width/8)
return this
.wall(null, null, a*6)
.wall(null, null, a*6)
.wall(null, null, a*6) },
} }
/*********************************************************************/ /*********************************************************************/
var __CACHE_UPDATE_CHECK = 10*60*1000 var __CACHE_UPDATE_CHECK = 5*60*1000
var __HANDLER_SET = false var __HANDLER_SET = false
var __DEBOUNCE_TIMEOUT = 100 var __DEBOUNCE_TIMEOUT = 100
var __DEBOUNCE = false var __DEBOUNCE = false
var KEY_CONFIG = { var KEY_CONFIG = {
' ': ['pause'], ' ': ['pause'],
n: setup,
ArrowLeft: ['left'], ArrowLeft: ['left'],
ArrowRight: ['right'], ArrowRight: ['right'],
// IE compatibility... // IE compatibility...
@ -350,8 +354,11 @@ function makeKeyboardHandler(snake){
clearHints() clearHints()
var action = KEY_CONFIG[event.key] var action = KEY_CONFIG[event.key]
action action
&& action[0] in snake && (action instanceof Function ?
&& snake[action[0]].apply(snake, action.slice(1)) }} action.call(snake)
: action[0] in snake ?
snake[action[0]].apply(snake, action.slice(1))
: null) }}
function makeTapHandler(snake){ function makeTapHandler(snake){
return function(event){ return function(event){
// prevent clicks and touches from triggering the same action // prevent clicks and touches from triggering the same action
@ -388,16 +395,30 @@ function digitizeBackground(snake, walls){
`rgb(${220 - v*2}, ${220 - v*2}, ${220 - v*2})`) `rgb(${220 - v*2}, ${220 - v*2}, ${220 - v*2})`)
// skip the rest... // skip the rest...
: null }) : null })
return snake
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// XXX need to place the snake with some headroom in the direction of
// travel...
function setup(snake, timer, size){ function setup(snake, timer, size){
snake = snake || Snake snake = snake || Snake
// levels...
var A = Math.round((size || snake.config.field_size)/8)
var RANDOM3_LEVEL = [
[null, null, A*6],
[null, null, A*6],
[null, null, A*6],
]
var HALVES_LEVEL = [
[null, null, A*8],
]
var QUARTERS_LEVEL = [
[null, 's', A*8],
[null, 'e', A*8],
]
function showScore(color, age){ function showScore(color, age){
score = snake.__top_score = score = snake.__top_score =
(!snake.__top_score || snake.__top_score.score < age) ? (!snake.__top_score || snake.__top_score.score < age) ?
@ -440,10 +461,18 @@ function setup(snake, timer, size){
// setup the game... // setup the game...
return snake return snake
.setup('.simplesnake', size) // prepare the field/game...
.randomLevel() .setup('.simplesnake', size, timer)
.start(timer) .call(digitizeBackground, snake)
.pause() .call(function(){
this.__snake_apples = []
return this
})
// load level...
.level(RANDOM3_LEVEL)
//.level(HALVES_LEVEL)
//.level(QUARTERS_LEVEL)
// game events / meta game rules... // game events / meta game rules...
// reconstruct eaten apples... // reconstruct eaten apples...
@ -453,11 +482,9 @@ function setup(snake, timer, size){
}) })
// one apple per snake... // one apple per snake...
.snakeBorn(function(color){ .snakeBorn(function(color){
var a = this.__snake_apples = this.__snake_apples || [] this.__snake_apples.indexOf(color) < 0
a.indexOf(color) < 0
&& this.apple() && this.apple()
&& a.push(color) && this.__snake_apples.push(color) })
})
// reconstruct snakes and pause game... // reconstruct snakes and pause game...
// XXX for multiplayer reconstruct the snake on timeout and do // XXX for multiplayer reconstruct the snake on timeout and do
// not pause... // not pause...
@ -469,10 +496,8 @@ function setup(snake, timer, size){
}) })
// indicate game state... // indicate game state...
.gameStarted(function(){ .gameStarted(function(){
digitizeBackground(this)
this._field.classList.remove('paused') }) this._field.classList.remove('paused') })
.gameStopped(function(){ .gameStopped(function(){
delete this.__snake_apples
this._field.classList.add('paused') }) this._field.classList.add('paused') })
// game eleemnts... // game eleemnts...