diff options
| -rw-r--r-- | game.html.coffee | 203 |
1 files changed, 96 insertions, 107 deletions
diff --git a/game.html.coffee b/game.html.coffee index 3fd3505..617216a 100644 --- a/game.html.coffee +++ b/game.html.coffee @@ -92,14 +92,7 @@ genPage = -> screen_x1 = 120 screen_y1 = 180 - sqrt <. Math - sin <. Math - cos <. Math - tan <. Math - pow <. Math - atan <. Math - atan2 <. Math - round <. Math + { sqrt, sin, cos, tan, pow, atan, atan2, round, log, exp, random } = Math pi = atan2 0, -1 pi_h = atan2 1, 0 pi_h_i = 1.0 / pi_h @@ -122,6 +115,7 @@ genPage = -> evolve.call c::, -> @ .< evolve Basic.evolution.call @ + @ .< Basic @ @brew = (name, base, evolution)@> @[name] = base::namedEvolve(name, evolution) @@ -134,6 +128,8 @@ genPage = -> @className = name @[name] = @constructor @[name] = c = @evolve -> evolution.call(@,p) + @process = (x)@> x.call(@) + @spawn = (x)@> new @constructor().process(x) @ Basic::Basic = Basic Demo = Basic::evolve -> @@ -155,7 +151,6 @@ genPage = -> @y = (@@screen_y1 + @py - @tb) * 2 @drawJSprite() @cr = 4 - @sqrt = Math.sqrt @collide = (o)@> { px, py, cr } = o opx = o.px; opy = o.py; ocr = o.cr @@ -276,21 +271,17 @@ genPage = -> @patience = 490 super() # Math: Math - sqrt: Math.sqrt - pow: Math.pow - sin: Math.sin draw: (collisions, game)-> { px, py, lr } = @ (spin = @spin) then - { pow, sin } = @ - d = pow(px * px + py * py, 0.42) + d = @@pow(px * px + py * py, 0.42) r = 5 / (d + 1) if r < 1 ir = 1 else - d = sin(d / 8000) + d = @@sin(d / 8000) d = d * d * 8000 - ir = sqrt(1 - r * r) * pow(d, 0.01) + ir = @@sqrt(1 - r * r) * @@pow(d, 0.01) @px = px * ir + py * (r * spin) @py = py * ir - px * (r * spin) (d = px * px + py * py) > 40000 then @@ -351,32 +342,35 @@ genPage = -> goodnight: (game)@> game.quitSlowBubbles() bumpedInto: (o)@> o.dead = true - @oxygenation = oxygenation = - create: @> oxygen: 0.7; __proto__: @ - addFrom: (o)@> - (o = o.oxygen)? then - oxygen <. @ - oxygen += o * 0.001 - if oxygen > 1.0 - oxygen = 1.0 - @oxygen = oxygen - consume: @> - @oxygen *= 0.99999 - @Vaquita = class extends @Sprite - twist: [ pixyvaquita_twist_l, pixyvaquita_twist_r ] - constructor: -> + @brew 'Vaquita', @Sprite, -> + @Oxygenation = @Basic::evolve -> + @oxygen = 0.7 + @addFrom = (o)@> + (o = o.oxygen)? then + oxygen <. @ + oxygen += o * 0.001 + if oxygen > 1.0 + oxygen = 1.0 + @oxygen = oxygen + @consume = @> + @oxygen *= 0.99999 + @ + @twist = [ pixyvaquita_twist_l, pixyvaquita_twist_r ] + @setConstructor -> @lr = 16 @tb = 16 - @oxygen = oxygenation.create() - super() - draw: -> + @oxygen = new @Oxygenation + @Sprite() + return + @draw = -> @oxygen.consume() if @vx < 0 @lr = - 18 else if @vx > 0 @lr = 18 - super() - bumpedInto: (x)@> @oxygen.addFrom(x) + @drawSprite() + @bumpedInto = (x)@> @oxygen.addFrom(x) + @ @AiVaquita = class extends @Vaquita constructor: -> @image = pixyvaquita @@ -384,8 +378,8 @@ genPage = -> super() beat_lr: 0 draw: -> - vx = @vx + Math.floor(Math.random()*5) - 2 - vy = @vy + Math.floor(Math.random()*3) - 1 + vx = @vx + Math.floor(@@random()*5) - 2 + vy = @vy + Math.floor(@@random()*3) - 1 x = @px y = @py rx = 0.5 * x / screen_x1 @@ -436,8 +430,8 @@ genPage = -> if @auto_to > 0 then @auto_to-- else @fvx ?= @vx @fvy ?= @vy - fvx = @fvx * 1.2 + Math.random() - 0.5 - fvy = @fvy * 1.2 + Math.random() - 0.5 + fvx = @fvx * 1.2 + @@random() - 0.5 + fvy = @fvy * 1.2 + @@random() - 0.5 x = @px y = @py if (s = fvx * fvx + fvy * fvy * 2) > 6 @@ -448,8 +442,8 @@ genPage = -> @vx = fvx|0 @vy = fvy|0 return - ax = vx/10 + (Math.random() - 0.5) * 2 - ay = vy/10 + (Math.random() - 0.5) * 2 + ax = vx/10 + (@@random() - 0.5) * 2 + ay = vy/10 + (@@random() - 0.5) * 2 return else unless @auto_to > 0 @@ -458,7 +452,7 @@ genPage = -> @auto_to = 600 if (aq = ax * ax + ay * ay) > 1 - aq = sqrt(aq) + aq = @@sqrt(aq) ax /= aq ay /= aq # aq = 1 @@ -484,9 +478,9 @@ genPage = -> if true # new way to calculate drag - anglecomponent = 0.2 + (sharpen(sqr(sin(Math.atan2(vy, vx))))) + anglecomponent = 0.2 + (@@sharpen(@@sqr(@@sin(@@atan2(vy, vx))))) # anglecomponent = 1200 / anglecomponent - antidrag = 1 - sqr( atan( (vx * vx + vy * vy * 3) *anglecomponent / 9 ) * pi_h_i ) + antidrag = 1 - @@sqr( @@atan( (vx * vx + vy * vy * 3) *anglecomponent / 9 ) * pi_h_i ) vx *= antidrag vy *= antidrag else @@ -516,12 +510,12 @@ genPage = -> # n.href.baseVal = "#_v105" if Math.random(0) > 0.5 # n.setAttribute "transform", "" # sea.appendChild n - angle = Math.random() * 6.28 + angle = @@random() * 6.28 v = new @AiVaquita v.vx = 0 v.vy = 0 - v.px = Math.floor(Math.sin(angle) * 300) - v.py = Math.floor(Math.cos(angle) * 300) + v.px = Math.floor(@@sin(angle) * 300) + v.py = Math.floor(@@cos(angle) * 300) # v.draw() # vaquita.update() @vaquitas.push v @@ -543,22 +537,17 @@ genPage = -> b.push v # v.draw() @setConstructor (@vaquitas = [], @cameos = [], @stilla = null)-> - @encounters = - __proto__: - encounter: encounter = - add: (game, x, y)@> game.addInto('cameos', new @creature(), x, y) - vy: 0 - random: Math.random - log: Math.log - exp: Math.exp - pow: Math.pow - poissonSample: (m)@> - { exp, random } = @ + @brew 'Encounters', @Basic, -> + @Encounter = @Basic::evolve -> + @add = (game, x, y)@> game.addInto('cameos', new @creature(), x, y) + @vy = 0 + @ + @poissonSample = (m)@> pgen = (m)-> x = 0 - p = exp(-m) + p = @@exp(-m) s = p - u = random() + u = @@random() while u > s x++ p = p * m / x @@ -569,20 +558,20 @@ genPage = -> s += pgen 50 m -= 50 s + pgen m - generate: (game,left,top,width,height,vx,vvy)@> - { probability, random } = @ + @generate = (game,left,top,width,height,vx,vvy)@> + { probability } = @ depth = game.getDepth() genRect = (m,left,top,width,height)=> c = m.p(depth) * width * height # c = 0 c = @poissonSample(c) if c is 1 - m.add?( game, left + ((random() * width)|0), top + ((random() * height)|0) ) + m.add?( game, left + ((@@random() * width)|0), top + ((@@random() * height)|0) ) else # c = 0 # if c > 1000 # c-- if random() > 0.15 while c-- > 0 - m.add?( game, left + ((random() * width)|0), top + ((random() * height)|0) ) + m.add?( game, left + ((@@random() * width)|0), top + ((@@random() * height)|0) ) 1 if vx * vx >= width * width for k,v of @catalogue @@ -613,35 +602,31 @@ genPage = -> genRect(v, left, top + height - vy, width, vy) else if vy < 0 genRect(v, left, top, width, -vy) - catalogue: - happybubble: - __proto__: encounter - p: (depth)@> 0.0001 * (1.5 - depth) - creature: @HappyBubble - vy: -1 - grumpybubble: - __proto__: encounter - p: (depth)@> depth < 0.08 then 0 else (depth - 0.08) * 0.00015 - creature: @GrumpyBubble - vy: -3 - evilbubble: - __proto__: encounter - p: (depth)@> depth < 0.35 then 0 else (depth - 0.35) * 0.00005 - creature: @EvilBubble - vy: -8 - stilla: - __proto__: encounter - p: (depth)@> depth < 0.01 then 1 else (1-depth)/100000 - add: (game, x, y)@> game.addStilla(x, y) - @touchInput = - ax: 0 - ay: 0 - tx: 0 - ty: 0 - ongoing: { } - __proto__: - eval: eval - start: (ev,el)@> + @ + @encounters = do (game = @)=> @Encounters::spawn -> + @catalogue = + happybubble: @Encounter::spawn -> + @p = (depth)@> 0.0001 * (1.5 - depth) + @creature = game.HappyBubble + @vy = -1 + @ + grumpybubble: @Encounter::spawn -> + @p = (depth)@> depth < 0.08 then 0 else (depth - 0.08) * 0.00015 + @creature = game.GrumpyBubble + @vy = -3 + @ + evilbubble: @Encounter::spawn -> + @p = (depth)@> depth < 0.35 then 0 else (depth - 0.35) * 0.00005 + @creature = game.EvilBubble + @vy = -8 + @ + stilla: @Encounter::spawn -> + @p = (depth)@> depth < 0.01 then 1 else (1-depth)/100000 + @add = (game, x, y)@> game.addStilla(x, y) + @ + @ + @brew 'TouchInput', @Basic, -> + @start = (ev,el)@> ongoing <. @ for t in ev.changedTouches { identifier, pageX, pageY } = t @@ -650,7 +635,7 @@ genPage = -> py: pageY ox: pageX oy: pageY - move: (ev,el)@> + @move = (ev,el)@> ongoing <. @ @ .> tx @ .> ty @@ -677,20 +662,28 @@ genPage = -> @ .< ty @ .< ax @ .< ay - end: (ev,el)@> + @end = (ev,el)@> ongoing <. @ for t in ev.changedTouches identifier <. t delete ongoing[identifier] @ax = 0 @ay = 0 - handle: (name)-> + @handle = (name)-> touchInput = @ (event)-> event.preventDefault() event.stopPropagation() touchInput[name](event,this) catch err alert err.toString() + @ + @touchInput = @TouchInput::spawn -> + @ax = 0 + @ay = 0 + @tx = 0 + @ty = 0 + @ongoing = { } + @ false then @ColorPlane = do=> document: document init: @> @@ -890,13 +883,11 @@ genPage = -> @WaterPlane = do=> waterscapeSuper: waterscapeSuper = @SeamlessPlane __proto__: waterscapeSuper - random: Math.random - sqrt: Math.sqrt colors: [ "cyan", "blue" ] randomStuff: @> - { random, sqrt, ctx } = @ - s = sqrt(15000 / (random() * 50 + 1)) | 0 - @withRect (random() * @w | 0), (random() * @h | 0), s, s >> 2, (x,y,w,h)-> + { ctx } = @ + s = @@sqrt(15000 / (@@random() * 50 + 1)) | 0 + @withRect (@@random() * @w | 0), (@@random() * @h | 0), s, s >> 2, (x,y,w,h)-> ctx.fillRect x,y,w,h @ init: (options)@> @@ -917,9 +908,9 @@ genPage = -> @randomStuff() waterscapeSuperFrame: waterscapeSuper.frame frame: (t)@> - { ctx, moltf, random } = @ + { ctx, moltf } = @ - ctx.fillStyle = @colors[ random() * 1.2 | 0 ] + ctx.fillStyle = @colors[ @@random() * 1.2 | 0 ] @randomStuff() while moltf-- > 0 t.save() @@ -974,7 +965,6 @@ genPage = -> first ?= last @last = last @first ?= first - Math: Math draw: @> { first, game } = @ unless first? @@ -987,7 +977,6 @@ genPage = -> ex = radx dy = rady - rady / 3 last = null - { random } = @Math while first? # x = first.x unless (x = first.x)? @@ -1004,7 +993,7 @@ genPage = -> first.x = x ly = last?.y y = first.y ?= ly ? dy - rr = random() + rr = @@random() y += ((rr*(3/0.33))|0) - 1 if rr < 0.33 ly? then dd = 1 + x - px @@ -1126,7 +1115,7 @@ genPage = -> lower: @waterscape logzoom: 0 frame: (t,sx,sy)@> - { ctx, random, w, h } = @ + { ctx, w, h } = @ x = @x + sx x = (x + w) % w |
