Created
March 1, 2013 08:22
-
-
Save namuol/5063230 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define [ | |
'fragl' | |
'util' | |
'modules/web' | |
'modules/webgl' | |
], (fr, util, web, webgl)-> | |
SCR_W = 150 | |
SCR_H = 300 | |
LEFT_WALL = 0 | |
RIGHT_WALL = SCR_W | |
MIN_TICK_LENGTH = 100 | |
NEW_STUFF_INTERVAL = 500 | |
BALL_PROB = 1 | |
MIN_BALLS = 1 | |
MAX_BALLS = 6 | |
TTL = 1*1000 | |
class Ball extends fr.Actor | |
constructor: -> | |
super arguments... | |
@behaviors.push fr.HasPhysics | |
width: 8 | |
height: 8 | |
init: -> | |
super arguments... | |
@sheet = new fr.TextureGrid @app.gfx.gfx, @width, @height | |
@anims.idle = @sheet.anim 20, 50, false | |
@anim = @anims.idle | |
@timeTilLaunch = TTL | |
@time = 0 | |
@bounces = 0 | |
@spawned = false | |
update: (dt) -> | |
@time += dt | |
@timeTilLaunch -= dt | |
if @timeTilLaunch > 0 | |
@alpha = 1 + 0.5*Math.sin (@time/1000)*60 | |
return | |
super arguments... | |
@alpha = 1 | |
left = @pos.x | |
right = left + @width | |
if left < LEFT_WALL | |
@pos.x = LEFT_WALL | |
@v.x = -@v.x | |
if !@app.muted | |
@app.sfx.bounce.play 0.5 | |
else if right > RIGHT_WALL | |
@pos.x = RIGHT_WALL - @width | |
@v.x = -@v.x | |
if !@app.muted | |
@app.sfx.bounce.play 0.5 | |
if (@pos.y + @height > SCR_H - @app.paddle.height) | |
pleft = @app.paddle.pos.x | |
pright = pleft + @app.paddle.width | |
if (right >= pleft and left <= pright) | |
# BOUNCE | |
@v.y = -@v.y*1.05 | |
xdiff = (@app.paddle.pos.x + @app.paddle.width/2) - (@pos.x + @width/2) | |
@v.x = xdiff * -4.8 | |
@pos.y = SCR_H - @app.paddle.height - @height | |
@app.score += @app.balls.length | |
if !@app.muted | |
@app.sfx.bounce.play() | |
++@bounces | |
@app.bounce() | |
else if (@pos.y > SCR_H) | |
@app.score += @bounces*@bounces | |
@app.removeBall(@) | |
class Paddle extends fr.Actor | |
width: 24 | |
height: 8 | |
init: -> | |
super arguments... | |
@sheet = new fr.TextureGrid @app.gfx.gfx, @width, @height | |
@anims.idle = @sheet.anim 0, 50, false | |
@anim = @anims.idle | |
update: -> | |
super arguments... | |
@pos.x = @app.input.worldMouse.x - @width / 2 | |
@pos.y = SCR_H - @height | |
#@pos.y = @app.input.worldMouse.y | |
class MyApp extends web.WebApp | |
gfx: | |
gfx: 'assets/gfx.png' | |
font: 'assets/font.png' | |
sfx: | |
# Shorthand multi-file load: | |
bounce: ['assets/mediumBounce.ogg', 'assets/mediumBounce.mp3', 'assets/mediumBounce.m4a'] | |
died: ['assets/died.ogg', 'assets/died.mp3', 'assets/died.m4a'] | |
allowSfxFailures: true | |
init: -> | |
super arguments... | |
@font = new fr.Font @, @gfx.font, 8,8 | |
@reset() | |
@gameIsOver = true | |
@gameOverText.string = " Click to Start" | |
@gravity.y = 120 | |
reset: -> | |
fr.warn 'RESET' | |
@score = 0 | |
@going = false | |
@actors = [] | |
@drawTarget.drawables = [] | |
@badBalls = [] | |
@scoreText = new fr.TextString | |
font: @font | |
static: true | |
pos: | |
x: 1 | |
y: 0 | |
string: "0" | |
update: => | |
@scoreText.string = "#{@score}\n#{@balls.length}/#{@minBalls}\n#{@bounces}" | |
@addActor @scoreText | |
@gameOverText = new fr.TextString | |
font: @font | |
static: true | |
pos: | |
x: SCR_W/2-72 | |
y: SCR_H/2 | |
string: "" | |
@addActor @gameOverText | |
@balls = [] | |
@tick = 0 | |
@paddle = new Paddle | |
pos: | |
x:SCR_W/2 | |
y:SCR_H - 8 | |
@addActor @paddle | |
@input.mapKey fr.K_R, 'reset' | |
@input.mapKey fr.K_M, 'mute' | |
@input.mapKey fr.K_UP, 'up' | |
@input.mapKey fr.K_DOWN, 'down' | |
@input.mapKey fr.K_LEFT, 'left' | |
@input.mapKey fr.K_RIGHT, 'right' | |
@gameIsOver = false | |
@bounces = 3 | |
@minBalls = 1 | |
newBall: -> | |
if @balls.length < MAX_BALLS | |
ball = new Ball { | |
pos: | |
x: Math.floor (SCR_W-8)*Math.random() | |
y: SCR_H / 2 | |
} | |
@balls.push ball | |
@addActor ball | |
removeBall: (a) -> | |
@badBalls.push a | |
a.dead = true | |
if @balls.length - 1 < @minBalls | |
@gameOver() | |
bounce: -> | |
--@bounces | |
if (@bounces <= 0) | |
@newBall() | |
@bounces = @balls.length * 3 | |
gameOver: -> | |
@gameIsOver = true | |
@gameOverText.string = ' GAME OVER\n(Click to restart)' | |
if !@muted | |
@sfx.died.play() | |
update: -> | |
if @gameIsOver | |
if @actions.LMB.hit() | |
@reset() | |
@newBall() | |
return | |
for a in @badBalls | |
@balls.splice @balls.indexOf(a), 1 | |
@badBalls = [] | |
if @actions.mute.hit() | |
@muted = !@muted | |
if @score > 20 | |
@minBalls = 2 | |
if @score > 60 | |
@minBalls = 3 | |
if @score > 200 | |
@minBalls = 4 | |
if @score > 450 | |
@minBalls = 5 | |
if @score > 800 | |
@minBalls = 6 | |
super arguments... | |
app = new MyApp SCR_W, SCR_H, 60, 2, [webgl.WebGLDrawTarget, web.ScaledCanvasDrawTarget, web.CanvasDrawTarget] | |
app.run 'container' | |
window.app = app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment