mirror of
https://github.com/flynx/Slang.git
synced 2025-10-29 10:40:07 +00:00
cleanup + docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
1d56dfbb1c
commit
0e5fcb9cff
@ -4,7 +4,7 @@
|
|||||||
*
|
*
|
||||||
* This code is designed to illustrate the non-intuitive approach to an
|
* This code is designed to illustrate the non-intuitive approach to an
|
||||||
* implementation, building a snake game as a cellular automaton rather
|
* implementation, building a snake game as a cellular automaton rather
|
||||||
* than the more intuitive, a set of entities (OOP) or a number of sets
|
* than the more obvious, set of entities (OOP) or a number of sets
|
||||||
* of procedures and data structures, directly emulating the "tactile"
|
* of procedures and data structures, directly emulating the "tactile"
|
||||||
* perception of the game, i.e. independent field, snakes, walls, apples
|
* perception of the game, i.e. independent field, snakes, walls, apples
|
||||||
* and their interactions.
|
* and their interactions.
|
||||||
@ -27,6 +27,23 @@
|
|||||||
* clarity...
|
* clarity...
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
|
* This code is structured in a scalable and introspective way:
|
||||||
|
* - Snake object is reusable as a prototype enabling multiple games
|
||||||
|
* to run at the same time
|
||||||
|
* - Snake implements an open external control scheme, i.e. it does not
|
||||||
|
* impose a specific way to implementing the way to control the game
|
||||||
|
* - Simple (but not trivial) code and code structure
|
||||||
|
* - Introspective: no hidden/masked state or functionality
|
||||||
|
* - No external dependencies
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Goals:
|
||||||
|
* - Show that the "intuitive" is not the only approach
|
||||||
|
* - Show that the "intuitive" is not allways the simplest
|
||||||
|
* - Show one approach to a scalable yet simple architecture
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
**********************************************************************/
|
**********************************************************************/
|
||||||
|
|
||||||
function makeEvent(handler_attr){
|
function makeEvent(handler_attr){
|
||||||
@ -104,7 +121,7 @@ var Snake = {
|
|||||||
}).join('\n')
|
}).join('\n')
|
||||||
}\n</table>`
|
}\n</table>`
|
||||||
},
|
},
|
||||||
_step: function(){
|
_tick: function(){
|
||||||
var that = this
|
var that = this
|
||||||
var l = this._cells.length
|
var l = this._cells.length
|
||||||
var w = this.field_size.width
|
var w = this.field_size.width
|
||||||
@ -115,7 +132,7 @@ var Snake = {
|
|||||||
this._cells.forEach(function(cell, i){
|
this._cells.forEach(function(cell, i){
|
||||||
var color = cell.style.backgroundColor
|
var color = cell.style.backgroundColor
|
||||||
|
|
||||||
// skip cells we touched...
|
// skip cells we touched on this tick...
|
||||||
if(cell.tick == tick){
|
if(cell.tick == tick){
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -131,7 +148,7 @@ var Snake = {
|
|||||||
cell.age -= 1
|
cell.age -= 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// head...
|
// snake head -> move...
|
||||||
var direction = cell.direction
|
var direction = cell.direction
|
||||||
if(directions.indexOf(direction) >= 0){
|
if(directions.indexOf(direction) >= 0){
|
||||||
// turn...
|
// turn...
|
||||||
@ -144,7 +161,7 @@ var Snake = {
|
|||||||
that.players[color] = ''
|
that.players[color] = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
// next cell index...
|
// get next cell index...
|
||||||
var next =
|
var next =
|
||||||
direction == 'n' ?
|
direction == 'n' ?
|
||||||
(i < w ? l - w + i : i - w)
|
(i < w ? l - w + i : i - w)
|
||||||
@ -169,7 +186,7 @@ var Snake = {
|
|||||||
that.snakeKilled(other)
|
that.snakeKilled(other)
|
||||||
that.snakeKilled(color)
|
that.snakeKilled(color)
|
||||||
|
|
||||||
// apple -> grow age...
|
// apple -> increment 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
|
||||||
@ -269,7 +286,7 @@ var Snake = {
|
|||||||
},
|
},
|
||||||
start: function(t){
|
start: function(t){
|
||||||
this.__timer = this.__timer
|
this.__timer = this.__timer
|
||||||
|| setInterval(this._step.bind(this), t || this.config.interval || 200)
|
|| setInterval(this._tick.bind(this), t || this.config.interval || 200)
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
stop: function(){
|
stop: function(){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user