Skip to content

Commit

Permalink
Enable humans
Browse files Browse the repository at this point in the history
  • Loading branch information
brainopia committed Oct 17, 2010
1 parent d02e3a4 commit f677e27
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
21 changes: 15 additions & 6 deletions lib/human.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ def initialize()
end

def move
next_cell = @cell.next(direction)
next_cell = @cell.next(rand(4) + 1) while next_cell
next_cell.content = self
@cell.content = nil
true
@direction = rand(4) + 1 if rand(3) == 0
next_cell = @cell.next(@direction)

if !next_cell or next_cell.content
@direction = rand(4) + 1
next_cell = @cell.next(@direction)
end

if next_cell && !next_cell.content
@cell.content = nil
@cell = next_cell
@cell.content = self
end
end

def data
['h', cell.location]
["h#{self.object_id}", [@cell.location]]
end

def self.destroy(human)
@@humans.delete(human)
new
end

def self.all
Expand Down
4 changes: 3 additions & 1 deletion lib/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def get_wall(lenght, content)
end

def data
Hash[*Worm.all.map { |it| it.data }.flatten(1)]
worms = Worm.all.map {|it| it.data }.flatten(1)
humans = Human.all.map {|it| it.data }.flatten(1)
Hash[*(worms+humans)]
end

def view
Expand Down
1 change: 0 additions & 1 deletion lib/worm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def move
if check_content next_cell
next_cell.content = self
@cells.unshift next_cell
true
end
end

Expand Down
6 changes: 5 additions & 1 deletion public/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ canvas.draw = function(objects){
prevY = prevLocation[i][1];
}

canvas.fillRect(x*4, y*4, 4, 4)
if (typeof(id) == 'string' && id.indexOf('h') != -1)
canvas.fillRect(x*4+1, y*4+1, 2, 2)
else
canvas.fillRect(x*4, y*4, 4, 4)

/*var j = 4;
while(j>0){
prevX += (prevX-x);
Expand Down
2 changes: 2 additions & 0 deletions script/websocket
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def tick
# puts
# end
Worm.all.each &:move
Human.all.each &:move
json = JSON.generate ['render', Map.data]
Worm.sockets.each do |socket|
socket.send json
Expand All @@ -33,6 +34,7 @@ def tick
end

Host = (ENV['RAILS_ENV'] == 'production') ? 'bioreactor.r10.railsrumble.com' : '0.0.0.0'
5.times { Human.new }

EM.run do

Expand Down
5 changes: 3 additions & 2 deletions spec/lib/worm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
it 'should use correct format' do
socket = Object.new
Worm.new socket
p Worm.all.map { |it| it.data }.flatten(0)
p Hash[*Worm.all.map { |it| it.data }.flatten(1)]
Human.new
Human.new
p Map.data
end

end

0 comments on commit f677e27

Please sign in to comment.