Skip to content

Commit

Permalink
Merge branch 'refs/heads/wip'
Browse files Browse the repository at this point in the history
  • Loading branch information
tobobo committed Apr 4, 2013
2 parents 9b96cec + e1a86e2 commit 1db9f22
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
33 changes: 20 additions & 13 deletions agent.coffee
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
agent = (x, y, heading, speed) ->
agent = (x, y, heading, vel) ->
this.x = x
this.y = y
this.heading = heading
this.speed = speed;

this.maxSpeed = 2;
this.minSpeed = 0;
this.vel = vel;

this.maxVel = 3;
this.minVel = 0.5;

this.evalHeading = (heading) ->
heading - Math.floor (heading / 2*Math.PI) * heading

this.heading = this.evalHeading heading

this.update = (agents, width, height) ->
this.x += Math.cos(this.heading)*this.speed
this.y += Math.sin(this.heading)*this.speed
this.x += Math.cos(this.heading)*this.vel
this.y += Math.sin(this.heading)*this.vel

randomVariance = (variance) -> (Math.random() - 0.5)*(variance)
this.heading = this.heading += randomVariance(0.5)
this.speed = this.speed += randomVariance(0.25)
randomVariance = (variance) -> (Math.random() - 0.5)*(2*variance)
this.heading = this.heading += randomVariance 0.25
this.vel = this.vel += randomVariance 0.05

if this.speed > this.maxSpeed then this.speed = this.maxSpeed
else if this.speed < this.minSpeed then this.speed = this.minSpeed
if this.vel > this.maxVel then this.vel = this.maxVel
else if this.vel < this.minVel then this.vel = this.minVel

this.changeHeading = (delta) ->
this.heading = this.evalHeading (this.heading + delta)

this.draw = (ctx) ->
ctx.lineWidth = 2
ctx.beginPath();
ctx.beginPath()
ctx.arc(this.x, this.y, 2, 0, 2 * Math.PI, false)
ctx.fill()

Expand Down
7 changes: 1 addition & 6 deletions display.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
display = (width, height) ->
disp = (width, height) ->

this.width = parseInt width
this.height = parseInt height
Expand Down Expand Up @@ -26,8 +26,3 @@ display = (width, height) ->
this.ctx.strokeRect 0, 0, width, height

this

$ ->
console.log('display.coffee');


15 changes: 1 addition & 14 deletions script.coffee
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
$ ->
console.log 'script.coffee'

window.requestAnimationFrame =
window.requestAnimationFrame or
window.mozRequestAnimationFrame or
window.webkitRequestAnimationFrame or
window.msRequestAnimationFrame;

theGame = new tag()

window.step = () ->
theGame.draw()
window.requestAnimationFrame(window.step)

window.requestAnimationFrame(window.step)
theGame.anim()
40 changes: 26 additions & 14 deletions tag.coffee
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
tag = () ->

window.requestAnimationFrame =
window.requestAnimationFrame or
window.mozRequestAnimationFrame or
window.webkitRequestAnimationFrame or
window.msRequestAnimationFrame


this.display = new display 1024, 768
tag = () ->
this.display = new disp 1024, 768
this.ctx = this.display.ctx

thisTag = this

this.agents = []

for i in [0..1000]
this.agents.push new agent(
Math.random()*this.display.width,
Math.random()*this.display.height,
Math.random()*2*Math.PI,
Math.random()*2
)
agentX = Math.random()*this.display.width
agentY = Math.random()*this.display.height
agentHeading = Math.random()*2*Math.PI
agentSpeed = Math.random()*5
this.agents.push new
agent agentX, agentY, agentHeading, agentSpeed

this.anim = () ->
window.requestAnimationFrame thisTag.step

this.updateAgents = (agents, width, height) ->
for a in this.agents
a.update(agents, width, height)
a.update agents, width, height

this.drawAgents = () ->
for p in this.agents
p.draw(this.ctx)
for a in this.agents
a.draw this.ctx

this.draw = () ->
this.display.clear()
this.updateAgents(this.agents, this.display.width, this.display.height)
this.updateAgents this.agents, this.display.width, this.display.height
this.drawAgents()
this.display.draw()

this.step = () ->
thisTag.draw()
window.requestAnimationFrame thisTag.step

this

0 comments on commit 1db9f22

Please sign in to comment.