Permalink
Browse files

fixed life pause, testing life voxel edit

  • Loading branch information...
1 parent 7a19298 commit c7a8143fb78de817bae07277e9acb10617f62387 @theoxylo committed Mar 29, 2013
View
0 .gitignore 100755 → 100644
No changes.
View
0 README.md 100755 → 100644
No changes.
View
0 controller.pie 100755 → 100644
No changes.
View
0 favicon.ico 100755 → 100644
No changes.
View
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+chown -R toneill *
+chgrp -R root *
+
+find . -type f | sed /\ /d | xargs chmod 644
+find . -type d | sed /\ /d | xargs chmod 755
View
File renamed without changes
View
File renamed without changes
View
File renamed without changes
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
4 index.html 100755 → 100644
@@ -32,9 +32,9 @@
</style>
</head>
<body>
- <img class="logo" src="logo-white.png">
+ <img class="logo" src="img/logo-white.png">
<div id="container"></div>
- <div id="crosshair"><img src="crosshair.png"/></div>
+ <div id="crosshair"><img src="img/crosshair.png"/></div>
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
View
32 index.js 100755 → 100644
@@ -34,7 +34,10 @@ function createNonRepeater(keyControl, fn) {
var game = createGame( {
generate: function (x, y, z) {
- return y % 16 ? 0 : Math.ceil(Math.random() * 2)
+ (y % 16 === 0) ? Math.ceil(Math.random() * 2) // repeating levels of grass and obsidian
+ : (x == 0 && y == 1 && z == 0) ? 3 // brick
+ : (x == 1 && y == 1 && z == 0) ? 4 // plank
+ : 0 // empty
},
keybindings: {
'W': 'forward'
@@ -46,8 +49,9 @@ var game = createGame( {
, 'I': 'select'
, 'X': 'copy'
, 'E': 'paste'
- , 'O': 'randomize'
- , 'P': 'pause'
+ , 'O': 'randomizeLife'
+ , 'U': 'updateLife'
+ , 'P': 'pauseLife'
, '<mouse 1>': 'fire'
, '<mouse 2>': 'firealt'
, '<space>' : 'jump'
@@ -65,13 +69,13 @@ var game = createGame( {
texturePath: texturePath,
worldOrigin: [0, 0, 0],
controls: { discreteFire: true }
-});
+})
-window.game = game // for debugging
+window.game = game // add to global browser scope for easy debugging
game.appendTo(document.body)
// add the player
-var player = createPlayer(game)('player.png')
+var player = createPlayer(game)('img/player.png')
player.possess()
player.yaw.position.set(2, 14, 4)
var triggerView = createNonRepeater('view', player.toggle.bind(player));
@@ -83,15 +87,15 @@ var highlighter = highlight(game, {
, distance: 100
, adjacentActive: createToggler('adjacent')
, selectActive: createToggler('select')
- , animate: false
+ , animate: true
});
highlighter.on('highlight', function (voxelPos) { blockPosErase = voxelPos })
highlighter.on('remove', function (voxelPos) { blockPosErase = null })
highlighter.on('highlight-adjacent', function (voxelPos) { blockPosPlace = voxelPos })
highlighter.on('remove-adjacent', function (voxelPos) { blockPosPlace = null })
// block interaction stuff, uses highlight data
-var currentMaterial = 1
+var currentMaterial = 2 // default obsidian
game.on('fire', function (target, state) {
var position = blockPosPlace
@@ -111,12 +115,11 @@ var triggerCopy = createNonRepeater('copy')
var triggerPaste = createNonRepeater('paste')
// GoL support, life engine wrapper
-var life = require('./life')(game, { tickTime: 300 } )
+var life = require('./life-engine')(game, { tickTime: 300 } )
life.randomize()
-life.apply()
-var triggerRandomize = createNonRepeater('randomize', life.randomize.bind(life))
-var triggerPause = createNonRepeater('pause', life.pause.bind(life))
+var triggerRandomize = createNonRepeater('randomizeLife', life.randomize)
+var triggerPause = createNonRepeater('pauseLife', life.togglePause)
// main update function, called at about 60 hz
game.on('tick', function onUpdate(dt) {
@@ -137,6 +140,11 @@ game.on('tick', function onUpdate(dt) {
life.tick(dt) // update life engine
})
+game.on('updateLife', function () {
+ console.log("Applying voxel state to life engine")
+ life.readVoxels()
+})
+
highlighter.on('highlight-select', function (s) {
selection = s
console.log(">>> [" + s.start + "][" + s.end + "] highlighted selection")
View
@@ -0,0 +1,77 @@
+var Life = require('alive')
+
+module.exports = function createInstance(game, opts) {
+ opts = opts || {}
+ var boardPos = opts.boardPosition || [-32, 0, -32]
+ var boardSize = opts.boardSize || 64
+ var tickTime = opts.tickTime || 500
+ var timeSinceTick = 0
+ var paused = false
+ var instance = new Life(boardSize)
+
+ function pause() {
+ paused = true
+ }
+
+ function togglePause() {
+ paused = !paused
+ }
+
+ function resume() {
+ paused = false
+ }
+
+ function randomize() {
+ pause()
+ for (var i = 0; i < boardSize; i++) {
+ for (var j = 0; j < boardSize; j++) {
+ // random cell state 1 (grass) or 2 (obsidian)
+ instance.setCell(i, j, Math.random() > 0.5 ? 1 : 0)
+ }
+ }
+ paint()
+ }
+
+ function readVoxels() {
+ pause()
+ for (var i = 0; i < boardSize; i++) {
+ for (var j = 0; j < boardSize; j++) {
+ pos = [boardPos[0] + i, boardPos[1], boardPos[2] + j]
+ instance.setCell(i, j, game.getBlock(pos))
+ }
+ }
+ paint()
+ }
+
+ function tick(dt) {
+ if (paused) return;
+ timeSinceTick += dt
+ if (timeSinceTick > tickTime) {
+ timeSinceTick = 0
+ instance.tick() // slow? need partial tick without paint
+ paint()
+ }
+ }
+
+ function paint() {
+ var pos
+ for (var i = 0; i < boardSize; i++) {
+ for (var j = 0; j < boardSize; j++) {
+ pos = [boardPos[0] + i, boardPos[1], boardPos[2] + j]
+ game.setBlock(pos, instance.getCell(i, j) ? 2 : 1)
+ }
+ }
+ }
+
+ // public api:
+ return {
+ pause: pause,
+ togglePause: togglePause,
+ resume: resume,
+ randomize: randomize,
+ readVoxels: readVoxels,
+ tick: tick,
+ paint: paint
+ }
+}
+
View
@@ -1,47 +0,0 @@
-var Life = require('alive')
-
-module.exports = function (game, opts) {
- opts = opts || {}
- var boardPos = opts.boardPosition || [-32, 0, -32]
- var boardSize = opts.boardSize || 64
- var tickTime = opts.tickTime || 900
- var timeSinceTick = 0
- var paused = false
- return {
- instance: new Life(boardSize),
-
- pause: function () { paused = !paused },
-
- tick: function (dt) {
- timeSinceTick += dt
- if (timeSinceTick > tickTime) {
- timeSinceTick = 0
- this.instance.tick()
- this.apply()
- console.log("life tick " + Date.now())
- }
- },
-
- randomize: function () {
- console.log("life::randomize")
- for (var i = 0; i < boardSize; i++) {
- for (var j = 0; j < boardSize; j++) {
- // random cell state 1 (grass) or 2 (obsidian)
- this.instance.setCell(i, j, Math.random() > 0.5 ? 1 : 0)
- }
- }
- },
-
- apply: function () {
- if (this.paused) return;
- var pos
- for (var i = 0; i < boardSize; i++) {
- for (var j = 0; j < boardSize; j++) {
- pos = [boardPos[0] + i, boardPos[1], boardPos[2] + j]
- game.setBlock(pos, this.instance.getCell(i, j) ? 2 : 1)
- }
- }
- }
- }
-}
-
View
5 package.json 100755 → 100644
@@ -14,8 +14,9 @@
"voxel-player": "0.1.0"
},
"devDependencies": {
- "browserify": "2.7.3",
- "browservefy": "0.0.10"
+ "browserify": "2.7.3",
+ "browservefy": "0.0.10",
+ "node-static": "0.6.7"
},
"scripts": {
"start": "browservefy index.js:bundle.js 8080 -- -dv"
View
@@ -0,0 +1,17 @@
+var static = require('node-static');
+
+//
+// Create a node-static server instance to serve the './public' folder
+//
+var file = new(static.Server)('./');
+
+require('http').createServer(function (request, response) {
+ request.addListener('end', function () {
+ //
+ // Serve files!
+ //
+ console.log(request.url)
+ file.serve(request, response);
+ });
+}).listen(8080);
+
View
11 voxel-clipboard.js 100755 → 100644
@@ -1,4 +1,3 @@
-var systemClipboard = require('node-clipboard')
var Voxel = require('./Voxel')
module.exports = Clipboard
@@ -40,19 +39,9 @@ Clipboard.prototype.copy = function (start, end) {
}
}
this.data = newSelectionData
-
- systemClipboard.write(JSON.stringify(newSelectionData))
}
Clipboard.prototype.paste = function (pos, selection) {
- if (!this.data) {
- try {
- var newData = JSON.parse(systemClipboard.read())
- } catch (e) {
- console.log("Error reading system clipboard: " + e)
- }
- if (newData) this.data = newData
- }
if (!this.data || (!pos && !selection)) {
console.log("paste failed: missing required data")
return;

0 comments on commit c7a8143

Please sign in to comment.