Permalink
Browse files

start on game of life

  • Loading branch information...
1 parent fc2062d commit eafa18db71b098da15e7ce314bfb3a38446fee75 @tinco committed Apr 22, 2012
Showing with 42 additions and 274 deletions.
  1. +0 −148 assets/PieceGeometry.js
  2. +0 −19 brick.coffee
  3. +5 −42 game.coffee
  4. +0 −25 graphics.coffee
  5. +1 −2 index.html
  6. +12 −27 keyboard.coffee
  7. +0 −11 piece.coffee
  8. +24 −0 world.coffee
View
@@ -1,148 +0,0 @@
-/**
- * @author mr.doob / http://mrdoob.com/
- * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Piece.as
- */
-
-THREE.PieceGeometry = function ( width, height, segmentsWidth, segmentsHeight, segmentsDepth, radius, materials, sides ) {
- THREE.Geometry.call( this );
- // Width is how many faces a circumference has.
- // Height is how far the piece extrudes.
- var scope = this;
-
- var mpx, mpy, mpz, mnx, mny, mnz;
-
- if ( materials !== undefined ) {
-
- if ( materials instanceof Array ) {
-
- this.materials = materials;
-
- } else {
-
- this.materials = [];
-
- for ( var i = 0; i < 6; i ++ ) {
-
- this.materials.push( materials );
-
- }
-
- }
-
- mpx = 0; mnx = 1; mpy = 2; mny = 3; mpz = 4; mnz = 5;
-
- } else {
-
- this.materials = [];
-
- }
-
- this.sides = { px: true, nx: true, py: true, ny: true, pz: true, nz: true };
-
- if ( sides != undefined ) {
-
- for ( var s in sides ) {
-
- if ( this.sides[ s ] !== undefined ) {
-
- this.sides[ s ] = sides[ s ];
-
- }
-
- }
-
- }
-
- this.sides.px && buildPlane( 'z', 'y', - 1, - 1, 1, width, height, mpx ); // px
- this.sides.nx && buildPlane( 'z', 'y', 1, - 1, -1, width, height, mnx ); // nx
- this.sides.py && buildPlane( 'x', 'z', 1, 1, 1, width, depth, mpy ); // py
- this.sides.ny && buildPlane( 'x', 'z', 1, - 1, -1, width, depth, mny ); // ny
- this.sides.pz && buildPlane( 'x', 'y', 1, - 1, 1, width, height, mpz ); // pz
- this.sides.nz && buildPlane( 'x', 'y', - 1, - 1, -1, width, height, mnz ); // nz
-
- function buildPlane( u, v, udir, vdir, wdir, height, depth, material ) {
- var w, ix, iy,
- gridX = segmentsWidth || 1,
- gridY = segmentsHeight || 1,
- offset = scope.vertices.length;
-
- if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) {
- w = 'z';
- } else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) {
- w = 'y';
- gridY = segmentsDepth || 1;
- } else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) {
- w = 'x';
- gridX = segmentsDepth || 1;
- }
-
- var gridX1 = gridX + 1,
- gridY1 = gridY + 1,
- segment_width = width / gridX,
- segment_height = height / gridY,
- normal = new THREE.Vector3();
-
- normal[ w ] = 0 > 0 ? 1 : - 1; // TODO fix normals
- var angle = Math.TAU / width;
- var startAngle = 1/2 * angle;
- if (w != 'y') {
- for ( iy = 0; iy < gridY1; iy ++ ) {
- for ( ix = 0; ix < gridX1; ix ++ ) {
- var vector = new THREE.Vector3();
- var yradius = radius + (iy * segment_height);
- vector[ u ] = Math.sin(startAngle - ix * (angle / segmentsWidth)) * -yradius * udir;
- vector[ v ] = (Math.cos(startAngle - iy * (angle / segmentsHeight)) * -yradius +radius + height /2) * vdir;
- vector[ w ] = (Math.tan(startAngle - angle) * yradius) * wdir;
-
- scope.vertices.push( new THREE.Vertex( vector ) );
- }
- }
- } else {
- // top or bottom
- for ( iy = 0; iy < gridY1; iy ++ ) {
- for ( ix = 0; ix < gridX1; ix ++ ) {
- var vector = new THREE.Vector3();
- yradius = -radius + depth * 2
- vector[ u ] = Math.sin(startAngle - ix * (angle / segmentsWidth)) * yradius * udir;
- vector[ v ] = Math.sin(startAngle - iy * (angle / segmentsHeight)) * yradius * vdir;
- vector[ w ] = -depth;
- scope.vertices.push( new THREE.Vertex( vector ) );
- }
- }
- }
-
- for ( iy = 0; iy < gridY; iy++ ) {
-
- for ( ix = 0; ix < gridX; ix++ ) {
-
- var a = ix + gridX1 * iy;
- var b = ix + gridX1 * ( iy + 1 );
- var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
- var d = ( ix + 1 ) + gridX1 * iy;
-
- var face = new THREE.Face4( a + offset, b + offset, c + offset, d + offset );
- face.normal.copy( normal );
- face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone(), normal.clone() );
- face.materialIndex = material;
-
- scope.faces.push( face );
- scope.faceVertexUvs[ 0 ].push( [
- new THREE.UV( ix / gridX, iy / gridY ),
- new THREE.UV( ix / gridX, ( iy + 1 ) / gridY ),
- new THREE.UV( ( ix + 1 ) / gridX, ( iy + 1 ) / gridY ),
- new THREE.UV( ( ix + 1 ) / gridX, iy / gridY )
- ] );
-
- }
-
- }
-
- }
-
- this.computeCentroids();
- this.mergeVertices();
-
-};
-
-THREE.PieceGeometry.prototype = new THREE.Geometry();
-THREE.PieceGeometry.prototype.constructor = THREE.PieceGeometry;
View
@@ -1,19 +0,0 @@
-class Brick
- constructor: () ->
- @pieces = (new Piece() for n in [1..4])
-
-class IBrick extends Brick
-class TBrick extends Brick
-class OBrick extends Brick
-class LBrick extends Brick
-class JBrick extends Brick
-class SBrick extends Brick
-class ZBrick extends Brick
-
-window.BrickKinds = [IBrick,
- TBrick,
- OBrick,
- LBrick,
- JBrick,
- SBrick,
- ZBrick]
View
@@ -1,10 +1,3 @@
-Math.TAU = 2 * Math.PI
-
-window.world_radius = 50
-pieces_per_row = 20
-
-piece_size = Math.TAU * (window.world_radius) / pieces_per_row
-
class Game
constructor: () ->
@frame = 0
@@ -14,21 +7,20 @@ class Game
@leftover = 0.0
@fps = 30
@step = 1000 / @fps # ms
- @pieces = []
@pause = false
loop: () ->
- t = this
- step = -> t.gameStep()
+ step = => @gameStep()
setInterval(step, @step / 10)
start: () ->
@graphics.setup()
@graphics.loadScene()
+ @world = new World()
+ @graphics.scene.add(@world.mesh)
@graphics.start()
@keyboard.start()
@started = true
- @newPiece(0,0)
@loop()
gameStep: () ->
@@ -37,7 +29,7 @@ class Game
catchUpFrameCount = Math.floor(timeSinceLastDoLogic / @step);
i = 0
while i < catchUpFrameCount
- @updateLogic()
+ @updateLogic()
@frame += 1
i++
@@ -46,33 +38,4 @@ class Game
updateLogic: () ->
if !@pause
- @piece.position.subSelf(@piece.position.clone().normalize().multiplyScalar(0.3))
- x = @piece.x
- y = @piece.y
- if @piece.position.distanceTo(new THREE.Vector3(0,0,0)) < window.world_radius + piece_size / 2 or onPiece(@piece)
- @pieces.push(@piece)
- @newPiece(x,y)
-
- newBrick: () ->
- r = Math.floor((Math.random()*7));
- @brick = new BrickKinds[r]()
- @graphics.addToScene(p.mesh) for p in @brick.pieces
-
- newPiece: (x,y) ->
- @piece = new THREE.Mesh(
- new THREE.PieceGeometry(
- piece_size, piece_size, piece_size),
- new THREE.MeshLambertMaterial(
- color: 0xCC0000))
-
- @piece.rotation = @graphics.camera.rotation.clone()
- @piece.position = @graphics.camera.position.clone().normalize().multiplyScalar(150)
- @piece.x = x
- @piece.y = y
- @graphics.addToScene(@piece)
-
- onPiece = (piece) ->
- for p in @pieces
- if piece.position.distanceTo(p.position) < piece_size
- return true
- return false
+ @world.step()
View
@@ -51,30 +51,6 @@ class Graphics
# so pull it back
@camera.position.z = 250
- # set up the sphere vars
- radius = world_radius
- segments = 16
- rings = 16
-
- # create the sphere's material
- sphereMaterial = new THREE.MeshLambertMaterial(color: 0xCC0000)
-
- # create a new mesh with
- # sphere geometry - we will cover
- # the sphereMaterial next!
- sphere = new THREE.Mesh(
- new THREE.SphereGeometry(
- radius,
- segments,
- rings),
- sphereMaterial);
-
- # add the sphere to the scene
- scene.add(sphere)
-
- #create the sphere's material
- sphereMaterial = new THREE.MeshLambertMaterial(color: 0xCC00AA)
-
#create a point light
pointLight = new THREE.PointLight(0xFFFFFF);
@@ -86,4 +62,3 @@ class Graphics
# add to the scene
scene.add(pointLight)
@scene = scene
- @sphere = sphere
View
@@ -12,8 +12,7 @@
<script type="text/javascript" src="lib/Three.js"></script>
<script type="text/javascript" src="assets/graphics.js"></script>
<script type="text/javascript" src="assets/keyboard.js"></script>
- <script type="text/javascript" src="assets/piece.js"></script>
- <script type="text/javascript" src="assets/brick.js"></script>
+ <script type="text/javascript" src="assets/world.js"></script>
</head>
<body>
<h1>Ludum Dare 23 Game</h1>
View
@@ -1,9 +1,7 @@
-
-
class Keyboard
constructor: (game,graphics) ->
@graphics = graphics
- @g = game
+ @game = game
@center = new THREE.Vector3(0,0,0)
@angle = Math.TAU / 20
@target
@@ -12,42 +10,29 @@ class Keyboard
start: () ->
@c = @graphics.camera
- @p = @c.position
key 'w', => @rotate "X-"
key 'a', => @rotate "Y-"
key 's', => @rotate "X+"
key 'd', => @rotate "Y+"
- key 'p', => @g.pause = !@g.pause
-
+ key 'p', => @game.pause = !@game.pause
+
rotate: (dir) ->
@speed = 1
@dir = dir
- if @dir is "X-"
- @g.piece.y-=1
- @rotX(@g.piece.position,-@angle)
- @rotX(@p,-@angle)
- else if @dir is "X+"
- @g.piece.y+=1
- @rotX(@g.piece.position,@angle)
- @rotX(@p,@angle)
- else if @dir is "Y-"
- @g.piece.x-=1
- @rotY(@g.piece.position,-@angle)
- @rotY(@p,-@angle)
- else if @dir is "Y+"
- @g.piece.x+=1
- @rotY(@g.piece.position,@angle)
- @rotY(@p,@angle)
- @c.lookAt(@center)
- @g.piece.rotation = @c.rotation.clone()
-
-
+ # if @dir is "X-"
+ #
+ # else if @dir is "X+"
+ #
+ # else if @dir is "Y-"
+ #
+ # else if @dir is "Y+"
+
rotX: (op,val) ->
y = op.y*Math.cos(val) - op.z*Math.sin(val)
z = op.y*Math.sin(val) + op.z*Math.cos(val)
op.y = y
op.z = z
-
+
rotY: (op,val) ->
x = op.x*Math.cos(val) + op.z*Math.sin(val)
z = op.z*Math.cos(val) - op.x*Math.sin(val)
View
@@ -1,11 +0,0 @@
-class Piece
- constructor: () ->
- @mesh = new THREE.Mesh(
- new THREE.CubeGeometry(
- piece_size, piece_size, piece_size),
- new THREE.MeshLambertMaterial(
- color: 0xCC0000))
-
- onPiece: (piece) ->
- # TODO it's more than this
- @piece.position.z < piece.position.z+piece_size
View
@@ -0,0 +1,24 @@
+class World
+ WORLD_RADIUS = 50
+ WORLD_CIRCUMFERENCE = 20
+ WORLD_HEIGHT = 10
+
+ constructor: () ->
+ @createMesh()
+
+ step: () ->
+
+ createMesh: () ->
+ # create the sphere's material
+ sphereMaterial = new THREE.MeshLambertMaterial(color: 0xCC0000)
+
+ # create a new mesh with
+ # sphere geometry - we will cover
+ # the sphereMaterial next!
+ sphere = new THREE.Mesh(
+ new THREE.SphereGeometry(
+ WORLD_RADIUS,
+ 100,
+ 100),
+ sphereMaterial);
+ @mesh = sphere

0 comments on commit eafa18d

Please sign in to comment.