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 8, 2013
2 parents 1db9f22 + 8136155 commit a7c6069
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 81 deletions.
47 changes: 25 additions & 22 deletions agent.coffee
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
agent = (x, y, heading, vel) ->
this.x = x
this.y = y
class Agent

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

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

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

this.heading = this.evalHeading heading
constructor: (@x, @y, @heading, @vel) ->
@x = x
@y = y

@vel = vel;

@maxVel = 3;
@minVel = 0.5;

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

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

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

if this.vel > this.maxVel then this.vel = this.maxVel
else if this.vel < this.minVel then this.vel = this.minVel
if @vel > @maxVel then @vel = Agent.maxVel
else if @vel < @minVel then @vel = Agent.minVel

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

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

this
35 changes: 16 additions & 19 deletions display.coffee
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
disp = (width, height) ->
class Disp

this.width = parseInt width
this.height = parseInt height
constructor: (@width, @height) ->

this.$body = $ 'body'
@$body = $ 'body'

@$body.append (
@$canvas =
$('<canvas>').attr('width', width).attr('height', height)
)

this.$body.append (
this.$canvas =
$('<canvas>').attr('width', width).attr('height', height)
)
@canvas = @$canvas.get 0

this.canvas = this.$canvas.get 0
@ctx = @canvas.getContext '2d'
@ctx.lineWidth = 4

this.ctx = this.canvas.getContext '2d'
this.ctx.lineWidth = 4
clear:() ->
@ctx.clearRect 0, 0, @width, @height

this.clear =() ->
this.ctx.clearRect 0, 0, this.width, this.height
draw: () ->
@drawBorder @ctx

this.draw = () ->
this.drawBorder(this.ctx)
drawBorder: () ->
@ctx.strokeRect 0, 0, @width, @height

this.drawBorder = () ->
this.ctx.strokeRect 0, 0, width, height

this
2 changes: 1 addition & 1 deletion script.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
$ ->
theGame = new tag()
theGame = new Tag
theGame.anim()
78 changes: 39 additions & 39 deletions tag.coffee
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
window.requestAnimationFrame =
window.requestAnimationFrame or
window.mozRequestAnimationFrame or
window.webkitRequestAnimationFrame or
window.msRequestAnimationFrame

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

thisTag = this

this.agents = []

for i in [0..1000]
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
class Tag
constructor: ->
@display = new Disp 1024, 768
@ctx = this.display.ctx

@agents = []
thisTag = @

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

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

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

anim: ->
@step()

updateAgents: (agents, width, height) ->
for a in @agents
a.update agents, width, height

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

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

this.step = () ->
thisTag.draw()
window.requestAnimationFrame thisTag.step
draw: ->
@display.clear()
@updateAgents @agents, @display.width, @display.height
@drawAgents()
@display.draw()

this

0 comments on commit a7c6069

Please sign in to comment.