tweaking...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-04-21 19:43:40 +03:00
parent 90c68b6eb4
commit 08d8841ab2
2 changed files with 56 additions and 37 deletions

View File

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

View File

@ -230,6 +230,7 @@ var Snake = {
this.players[color] = '' this.players[color] = ''
return this return this
.snakeBorn(color)
}, },
apple: function(point){ apple: function(point){
point = this.normalize_point(point || this.random_point) point = this.normalize_point(point || this.random_point)
@ -265,9 +266,12 @@ var Snake = {
}, },
// events... // events...
appleEaten: makeEvent('__appleEatenHandlers'),
snakeKilled: makeEvent('__killHandlers'), snakeKilled: makeEvent('__killHandlers'),
snakeBorn: makeEvent('__birthHandlers'),
appleEaten: makeEvent('__appleEatenHandlers'),
tick: makeEvent('__tickHandlers'), tick: makeEvent('__tickHandlers'),
gameStarted: makeEvent('__startHandlers'),
gameStopped: makeEvent('__stopHandlers'),
// actions... // actions...
setup: function(field, size){ setup: function(field, size){
@ -287,22 +291,22 @@ var Snake = {
.snakeKilled(null) .snakeKilled(null)
}, },
start: function(t){ start: function(t){
this._field.classList.remove('paused')
this.__timer = this.__timer this.__timer = this.__timer
|| setInterval(this._tick.bind(this), t || this.config.interval || 200) || setInterval(this._tick.bind(this), t || this.config.interval || 200)
// reset player control actions... // reset player control actions...
var that = this var that = this
Object.keys(this.players) Object.keys(this.players)
.forEach(function(k){ that.players[k] = '' }) .forEach(function(k){ that.players[k] = '' })
this.tick()
return this return this
.tick()
.gameStarted()
}, },
stop: function(){ stop: function(){
this._field.classList.add('paused')
clearInterval(this.__timer) clearInterval(this.__timer)
delete this.__timer delete this.__timer
delete this.__tick delete this.__tick
return this return this
.gameStopped()
}, },
pause: function(){ pause: function(){
return this.__timer ? this.stop() : this.start() }, return this.__timer ? this.stop() : this.start() },
@ -328,8 +332,6 @@ var Snake = {
/*********************************************************************/ /*********************************************************************/
//var BG_ANIMATION = true
var __CACHE_UPDATE_CHECK = 10*60*1000 var __CACHE_UPDATE_CHECK = 10*60*1000
var __HANDLER_SET = false var __HANDLER_SET = false
var __DEBOUNCE_TIMEOUT = 100 var __DEBOUNCE_TIMEOUT = 100
@ -373,6 +375,20 @@ function makeTapHandler(snake){
function clearHints(){ function clearHints(){
document.body.classList.contains('hints') document.body.classList.contains('hints')
&& document.body.classList.remove('hints') } && document.body.classList.remove('hints') }
function digitizeBackground(snake, walls){
snake._cells.forEach(function(c){
var v = Math.floor(Math.random() * 6)
// bg cell...
c.classList.length == 0 ?
(c.style.backgroundColor =
`rgb(${255 - v}, ${255 - v}, ${255 - v})`)
// wall...
: walls && c.classList.contains('wall') ?
(c.style.backgroundColor =
`rgb(${220 - v*2}, ${220 - v*2}, ${220 - v*2})`)
// skip the rest...
: null })
}
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@ -382,6 +398,20 @@ function clearHints(){
function setup(snake, timer, size){ function setup(snake, timer, size){
snake = snake || Snake snake = snake || Snake
function showScore(color, age){
score = snake.__top_score =
(!snake.__top_score || snake.__top_score.score < age) ?
{
color: color || '',
score: age || 0,
}
: snake.__top_score
snake._field.setAttribute('score', score.score)
snake._field.setAttribute('snake', score.color)
snake._field.setAttribute('state', (
score.score == age && score.color == color) ? '(current)' : '')
}
// setup event handlers (only once)... // setup event handlers (only once)...
if(!__HANDLER_SET){ if(!__HANDLER_SET){
// control handlers... // control handlers...
@ -408,20 +438,6 @@ function setup(snake, timer, size){
__HANDLER_SET = true __HANDLER_SET = true
} }
function showScore(color, age){
score = snake.__top_score =
(!snake.__top_score || snake.__top_score.score < age) ?
{
color: color || '',
score: age || 0,
}
: snake.__top_score
snake._field.setAttribute('score', score.score)
snake._field.setAttribute('snake', score.color)
snake._field.setAttribute('state', (
score.score == age && score.color == color) ? '(current)' : '')
}
// setup the game... // setup the game...
return snake return snake
.setup('.simplesnake', size) .setup('.simplesnake', size)
@ -429,35 +445,38 @@ function setup(snake, timer, size){
.start(timer) .start(timer)
.pause() .pause()
// game events... // game events / meta game rules...
// reconstruct eaten apples...
.appleEaten(function(color, age){ .appleEaten(function(color, age){
this.apple() this.apple()
showScore(color, age) showScore(color, age)
}) })
// one apple per snake...
.snakeBorn(function(color){
var a = this.__snake_apples = this.__snake_apples || []
a.indexOf(color) < 0
&& this.apple()
&& a.push(color)
})
// reconstruct snakes and pause game...
// XXX for multiplayer reconstruct the snake on timeout and do
// not pause...
.snakeKilled(function(color, age){ .snakeKilled(function(color, age){
this this
.pause() .pause()
.snake(color, 3) .snake(color, 3)
showScore(color, 3) showScore(color, 3)
}) })
.tick(function(){ // indicate game state...
// digital noise effect... .gameStarted(function(){
window.BG_ANIMATION digitizeBackground(this)
&& (!snake.__tick || snake.__tick % 2 == 0) this._field.classList.remove('paused') })
&& this._cells.forEach(function(c){ .gameStopped(function(){
var v = Math.floor(Math.random() * 6) delete this.__snake_apples
c.classList.length == 0 ? this._field.classList.add('paused') })
(c.style.backgroundColor =
`rgb(${255 - v}, ${255 - v}, ${255 - v})`)
: c.classList.contains('wall') ?
(c.style.backgroundColor =
`rgb(${220 - v*2}, ${220 - v*2}, ${220 - v*2})`)
: null })
})
// game eleemnts... // game eleemnts...
.apple() .apple()
.apple()
.snake('blue', 3) .snake('blue', 3)
} }