Skip to content
Browse files

Refactoring, generation button

  • Loading branch information...
1 parent a6f4bba commit fbc3e119194f1a1a8a174cdcc1feca681349820e Steve Jackson committed Sep 11, 2011
View
30 Gemfile.lock
@@ -4,8 +4,6 @@ GEM
activesupport (3.1.0)
multi_json (~> 1.0)
addressable (2.2.6)
- childprocess (0.2.2)
- ffi (~> 1.0.6)
chunky_png (1.2.1)
coffee-script (2.2.0)
coffee-script-source
@@ -16,16 +14,12 @@ GEM
fssm (>= 0.2.7)
sass (~> 3.1)
daemons (1.1.4)
- diff-lcs (1.1.3)
em-websocket (0.3.1)
addressable (>= 2.1.1)
eventmachine (>= 0.12.9)
eventmachine (0.12.10)
execjs (1.2.4)
multi_json (~> 1.0)
- fancy-buttons (1.1.2)
- compass (>= 0.11)
- ffi (1.0.9)
fssm (0.2.7)
guard (0.6.3)
thor (~> 0.14.6)
@@ -39,13 +33,6 @@ GEM
rack (>= 1.0.0)
url_mount (~> 0.2.1)
i18n (0.6.0)
- jasmine (1.0.2.1)
- json_pure (>= 1.4.3)
- rack (>= 1.1)
- rspec (>= 1.3.1)
- selenium-webdriver (>= 0.1.3)
- json_pure (1.5.4)
- spruz (~> 0.2.8)
libv8 (3.3.10.2)
maruku (0.6.0)
syntax (>= 1.0.0)
@@ -85,21 +72,7 @@ GEM
rack-test (0.6.1)
rack (>= 1.0)
rb-fsevent (0.4.3.1)
- rspec (2.6.0)
- rspec-core (~> 2.6.0)
- rspec-expectations (~> 2.6.0)
- rspec-mocks (~> 2.6.0)
- rspec-core (2.6.4)
- rspec-expectations (2.6.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.6.0)
- rubyzip (0.9.4)
sass (3.1.7)
- selenium-webdriver (2.5.0)
- childprocess (>= 0.2.1)
- ffi (>= 1.0.7)
- json_pure
- rubyzip
sinatra (1.2.6)
rack (~> 1.1)
tilt (>= 1.2.2, < 2.0)
@@ -110,7 +83,6 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
- spruz (0.2.13)
syntax (1.0.0)
temple (0.3.4)
therubyracer (0.9.4)
@@ -131,7 +103,5 @@ PLATFORMS
ruby
DEPENDENCIES
- fancy-buttons
- jasmine
middleman
therubyracer
View
BIN source/images/twitter-48x48.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
8 source/index.html.haml
@@ -20,15 +20,17 @@
%header
.primary_layout
%span.bigger
- mazery
+ %a{ :href => '/', :alt => "mazery" } mazery
%span.smaller
- a js/coffeescript/html5 experiment by Steve Jackson
+ %a{ :href => 'http://sjackson.net' } a js/coffeescript/html5 canvas thing by Steve Jackson
%hr.space
%content
.primary_layout
- %a.button Generate
+ %a.button#generate Generate
+ %a.right#twitter{ :href => 'http://twitter.com/_sjackson', :alt => "Follow me on twitter" }
+ Follow this guy on twitter.
%hr.space
%canvas#mazecanvas{ :width => 950, :height => 950 }
Maze Canvas.
View
57 source/javascripts/mazery.js.coffee
@@ -1,9 +1,17 @@
+# steve jackson, 2011
+# use at your own peril.
+
$(document).ready ->
mazery()
+ # 'Generate' button handler
+ $('#generate').click ->
+ mazery()
+
mazery = ->
# grab the context
canvas = $('#mazecanvas').get(0)
+ canvas.width = canvas.width
canvasWidth = canvas.width
canvasHeight = canvas.height
cellSize = 30
@@ -29,12 +37,14 @@ mazery = ->
context.clearRect(0, 0, canvas.width, canvas.height)
# rendering / logic
@maze.update()
- #@maze.drawGrid()
@maze.drawAllCells()
+ $('#generate').click ->
+ clearInterval(mazeInterval)
+
# start our loop!
framesPerSecond = 60
- setInterval(logicLoop, 1000 / framesPerSecond)
+ mazeInterval = setInterval(logicLoop, 1000 / framesPerSecond)
class Maze
constructor: (@context, @width, @height, @cellSize) ->
@@ -52,33 +62,6 @@ class Maze
@hunting = false
@complete = false
- drawGrid: ->
- @context.strokeStyle = "#eaeaea"
- for x in [-0.5..@width * @cellSize + 0.5]
- @context.beginPath()
- @context.moveTo(x, 0)
- @context.lineTo(x, @height * @cellSize)
- @context.fill()
- @context.closePath()
- @context.stroke()
- x += @cellSize
-
- for y in [0.5..@height * @cellSize + 0.5]
- @context.beginPath()
- @context.moveTo(0, y)
- @context.lineTo(@width * @cellSize, y)
- @context.fill()
- @context.closePath()
- @context.stroke()
- y += @cellSize
-
- getRandomColor: (x, y) ->
- red = 255#Math.floor(Math.random() * 256)
- green = Math.floor(Math.random() * 0)
- blue = Math.floor(Math.random() * 0)
- opacity = 1 #Math.random() * 1.5
- "rgba(" + red + "," + green + "," + blue + "," + opacity + ")"
-
getRandomCell: ->
x = Math.floor(Math.random() * @width)
y = Math.floor(Math.random() * @height)
@@ -97,12 +80,12 @@ class Maze
# * EITHER "empty" cell color, or "traversed" cell color
# * "recently visited" cell color, fades away
# * "hunted" cell color, fades away
-
+
@cells[x][y].update()
@context.globalAlpha = @cells[x][y].alpha
locX = x * @cellSize
locY = y * @cellSize
-
+
# draw either "empty" cell color, or "traversed" cell color
@context.fillStyle = @getCellColor(x, y)
@context.fillRect(locX, locY, @cellSize, @cellSize)
@@ -121,14 +104,13 @@ class Maze
@context.globalAlpha = @cells[x][y].huntedAlpha
@context.fillRect(locX, locY, @cellSize, @cellSize)
@context.fill()
-
+
@drawCellBorders(x, y)
drawCellBorders: (x, y) ->
@context.strokeStyle = "#eaeaea"
@context.globalAlpha = 1
- # top border:
if @cells[x][y].north
@context.beginPath()
@context.moveTo(x * @cellSize, y * @cellSize)
@@ -165,8 +147,7 @@ class Maze
update: ->
# maze generation logic
- if @complete
- return true
+ return true if @complete
if @hunting
# when we're hunting, we're sweeping down row by row to find a cell that is
@@ -186,6 +167,7 @@ class Maze
@location[1] += 1
else
@complete = true
+ # normal traversal mode
else
neighbor = @getNeighbor(@location[0], @location[1], false)
unless not neighbor
@@ -202,6 +184,7 @@ class Maze
for i in [0..@width]
@cells[i][row].hunted = true
@cells[i][row].huntedAlpha = 1
+ # now let's actually hunt this row.
for i in [0..@width]
if not @cells[i][row].visited
# if this cell is unvisited, is it next to a cell that IS visited?
@@ -243,14 +226,14 @@ class Maze
checkedSides.push(sideToCheck)
+ # we couldn't find a valid neighbor
false
validCellLocation: (x, y) ->
return x >= 0 and x <= @width and y >= 0 and y <= @height
class Cell
constructor: (@x, @y) ->
- @states = []
@alpha = 1.0
@visited = false
@north = true
@@ -259,7 +242,7 @@ class Cell
@west = true
@visitedAlpha = 0
@huntedAlpha = 0
-
+
@recentlyVisited = false
@hunted = false
View
17 source/stylesheets/layout.css.sass
@@ -15,13 +15,14 @@ body
font-family: proxima-nova, "Helvetica Neue", Helvetica, Arial, sans-serif
background: $background-color
-form
- +blueprint-form
-
p, span, h1, h2, h3
color: $text-color
-header p, span, h1, h2, h3
+body a, body a:visited
+ color: $text-color
+ text-decoration: none
+
+header p, span, h1, h2, h3, body a, body a:visited
+transition-property(all)
+transition-duration(0.2s)
+transition-timing-function(ease-in-out)
@@ -31,6 +32,7 @@ header p, span, h1, h2, h3
.primary_layout
+container
+ width: 900px
header
+column(24)
content
@@ -72,3 +74,10 @@ hr.space
background: darken($theme2, 10%)
&:active
top: 1px
+
+.right
+ float: right
+
+#twitter
+ position: relative
+ top: 20px

0 comments on commit fbc3e11

Please sign in to comment.
Something went wrong with that request. Please try again.