Skip to content

Instantly share code, notes, and snippets.

@hornos
Created June 13, 2014 13:47
Show Gist options
  • Select an option

  • Save hornos/d8f9fcb1df3bce9ee8d6 to your computer and use it in GitHub Desktop.

Select an option

Save hornos/d8f9fcb1df3bce9ee8d6 to your computer and use it in GitHub Desktop.
Quintus Runner
define (require, exports, module) ->
  'use strict'

  Q = window.Q = Quintus()
    .include("Sprites, Scenes, Input, 2D, Anim, Touch, UI")
    .setup( maximize: true )
    .controls().touch()

  SPRITE_BOX = 1
  Q.gravityY = 2000

  console.log "init"

  class Player extends Q.Sprite
    constructor: (p) ->
      options=
        sheet: "player"
        sprite: "player"
        collisionMask: SPRITE_BOX
        x: 40
        y: 555
        standingPoints: [ [ -16, 44], [ -23, 35 ], [-23,-48], [23,-48], [23, 35 ], [ 16, 44 ]]
        duckingPoints : [ [ -16, 44], [ -23, 35 ], [-23,-10], [23,-10], [23, 35 ], [ 16, 44 ]]
        speed: 500
        jump: -700        
      super p, options
      @p.points = @p.standingPoints
      @add("2d, animation")
    #

    step: (dt) ->
      @p.vx += (@p.speed - @p.vx) / 4

      if @p.y > 555
        @p.y = 555
        @p.landed = 1
        @p.vy = 0
      else
        @p.landed = 0

      if Q.inputs['up'] and @p.landed > 0
        @p.vy = @p.jump

      @p.points = @p.standingPoints
      if @p.landed
        if Q.inputs['down'] 
          @play("duck_right")
          @p.points = @p.duckingPoints
        else
          @play("walk_right")
      else
        @play("jump_right")

      @stage.viewport.centerOn(@p.x + 300, 400 )
    # step
  # class Player


  class Box extends Q.Sprite
    constructor: (player) ->
      levels = [ 565, 540, 500, 450 ]
      # player = Q("Player").first()

      options=
        x: player.p.x + Q.width + 50
        y: levels[Math.floor(Math.random() * 3)]
        frame: Math.random() < 0.5 ? 1 : 0
        scale: 2
        type: SPRITE_BOX
        sheet: "crates"
        vx: -600 + 200 * Math.random()
        vy: 0
        ay: 0
        theta: (300 * Math.random() + 200) * (Math.random() < 0.5 ? 1 : -1)
      super options
      @on("hit")
    # constructor

    step: (dt) ->
      @p.x  += @p.vx * dt
      @p.vy += @p.ay * dt
      @p.y  += @p.vy * dt
      if @p.y != 565
        @p.angle += @p.theta * dt
      @destroy() if @p.y > 800
    # step

    hit: () ->
      @p.type = 0
      @p.collisionMask = Q.SPRITE_NONE
      @p.vx = 200
      @p.ay = 400
      @p.vy = -300
      @p.opacity = 0.5
    # hit
  # class


  class BoxThrower extends Q.GameObject
    constructor: (@player) ->
      super()
      @p=
        launchDelay: 0.75
        launchRandom: 1
        launch: 2
    #

    update: (dt) ->
      @p.launch -= dt
      if @p.launch < 0
        @stage.insert new Box(@player)
        @p.launch = @p.launchDelay + @p.launchRandom * Math.random()
      #
    #
  # class GameObject


  Q.scene "level1", (stage) ->
    stage.insert new Q.Repeater
      asset: "background-wall.png"
      speedX: 0.5

    stage.insert new Q.Repeater
      asset: "background-floor.png"
      repeatY: false
      speedX: 1.0
      y: 300

    player = new Player()
    stage.insert new BoxThrower player
    stage.insert player
    stage.add "viewport"
  # level1


  Q.load "player.json, player.png, background-wall.png, background-floor.png, crates.png, crates.json", () ->
    Q.compileSheets "player.png","player.json"
    Q.compileSheets "crates.png","crates.json"
    options=
      walk_right:
        frames: [0,1,2,3,4,5,6,7,8,9,10]
        rate: 1/15
        flip: false
        loop: true
      jump_right:
        frames: [13]
        rate: 1/10
        flip: false
      stand_right:
        frames:[14]
        rate: 1/10
        flip: false
      duck_right:
        frames: [15]
        rate: 1/10
        flip: false
    Q.animations "player", options
    Q.stageScene "level1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment