Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                html, body {
  height: 100%;
}

body {
  overflow: hidden;
  margin: 0;
  padding: 0;
  background: black center url(https://labs.phaser.io/assets/skies/space.jpg);
  color: #eee;
  font: caption;
}

#version {
  position: absolute;
  left: 5px;
  top: 5px;
}
              
            
!

JS

              
                DENSITY = 0.001
FOOD = 10
DEBUG = no
MAX_ENTRIES = 16
WHITE = 0xffffff
BLACK = 0


{ abs, floor, max, pow, sqrt } = Math
{ Clamp, DegToRad, Linear, RadToDeg, Wrap } = Phaser.Math
{ BetweenPoints, ShortestBetween } = Phaser.Math.Angle
{ SetBlendMode } = Phaser.Actions
{ ADD, SCREEN } = Phaser.BlendModes
{ velocityFromRotation } = Phaser.Physics.Arcade.ArcadePhysics.prototype


DistanceBetween = Phaser.Math.Distance.Between
DistanceBetweenPoints = Phaser.Math.Distance.BetweenPoints
DistanceSquared = Phaser.Math.Distance.Squared
RandomBetween = Phaser.Math.Between


random = ( min, max ) ->
  unless max?
    max = min
    min = 0
  RandomBetween( min, max )


class Ship extends Phaser.Physics.Arcade.Image

  constructor: ->
    super( arguments... )
    @alpha = 0.4
    @thrust = 90
    @hasTarget = false
    @lowestDist = Infinity
    @eating = false
    this

  eat: ( food ) ->
    food.life -= 0.002
    @eating = true
    return

  spotFood: ( food ) ->
    dist = DistanceBetweenPoints( this, food )
    if food.threshold < dist
      return false
    if @lowestDist < dist
      return false
    @hasTarget = true
    @lowestDist = dist
    @steerTo( food )
    if dist < food.radius
      @eat( food )
    true

  steerTo: ( point ) ->
    delta = ShortestBetween(
      @body.rotation,
      RadToDeg( BetweenPoints( this, point ) )
    )
    delta *= 60
    delta = Clamp( delta, -180, 180 )
    # @body.angularVelocity = delta
    @body.angularVelocity = Linear( @body.angularVelocity, delta, 0.05 )
    return

  update: ->
    @setDebugBodyColor(
      if @eating then 0x00ffff
      else if @hasTarget then 0xffff00
      else 0xff00ff
    )
    @body.maxAngular = 180 # TODO
    if @eating
      @thrust *= 0.99
    else if @hasTarget
      @thrust *= 1.01
    else
      @body.angularVelocity *= 0.98
    @thrust += random( -1, 1 )
    @thrust += 1 if @thrust < 60
    @thrust -= 1 if @thrust > 180
    @thrust = max( 1, @thrust )
    @body.angularVelocity += random( -1, 1 )
    @hasTarget = false
    @lowestDist = Infinity
    @eating = false
    velocityFromRotation( @rotation, @thrust, @body.velocity )
    @wrap()
    return

  wrap: ->
    unless @hasTarget
      @body.world.wrap( this )
    return


class Food extends Phaser.Physics.Arcade.Image

  constructor: ->
    super( arguments... )
    @growthRadius = 0
    @radius = 0
    @life = 0
    @threshold = 0
    @growing = false
    this

  reset: ->
    @active = true
    @visible = true
    @scale = 1
    @growthRadius = 0
    @life = random( 30, 120 )
    @radius = @life
    @threshold = 2 * @radius
    @growing = true
    width = 2 * @threshold
    height = 2 * @threshold
    @body.reset()
    @body.x += 0.5 * ( @width - width )
    @body.y += 0.5 * ( @height - height )
    @body.setSize( width, height, 0, 0 )
    @body.world.remove( @body ) # growing!
    this

  update: ->
    if @growing
      @growthRadius += 1
      if @growthRadius >= @radius
        @growing = false
        @body.world.add( @body )
    if @life > 0
      strength = ( if @growing then @growthRadius else @life )
      @scale = pow( strength / 90, 0.5 )
    else
      @body.world.remove( @body )
      @active = false
      @visible = false
    return


class Scene extends Phaser.Scene

  init: ->
    this.add.graphics()
      .fillStyle( WHITE )
      .fillTriangle( 0, 0, 12, 6, 0, 12 )
      .generateTexture( "ship", 12, 12 )
      .destroy()
    return
  
  preload: ->
    @load
      .image( "food", "assets/particles/red.png" )
    return

  create: ->
    console.time( "create" )
    @bounds = Phaser.Geom.Rectangle.Clone( @scale )
    @width = @bounds.width
    @height = @bounds.height
    @ships = @physics.add.group(
      classType: Ship
      defaultKey: "ship"
      runChildUpdate: true
    )
    quantity = floor( DENSITY * @width * @height )
    for [0...quantity]
      @ships.create( 0, 0 )
    @food = @physics.add.staticGroup(
      classType: Food
      defaultKey: "food"
      maxSize: FOOD
      runChildUpdate: true
    )
    # @physics.world.staticTree.clear()
    @queueRandomFood()
    SetBlendMode( @ships.getChildren(), SCREEN )
    SetBlendMode( @food.getChildren(), SCREEN )
    @input.on( "pointerdown", @mousedown, this )
    console.timeEnd( "create" )
    return

  update: ->
    { treeMinMax } = @physics.world
    for food in @food.getChildren() when food.active and not food.growing
      { x, y, width, height } = food.body
      bodies = @physics.overlapRect( x, y, width, height )
      for body in bodies
        @overlap( body.gameObject, food )
    return

  overlap: ( ship, food ) ->
    ship.spotFood( food )
    return    

  createFood: ( x, y ) ->
    @food.get( x, y )?.reset().update()

  createRandomFood: ->
    @createFood( random( @width ), random( @height ) )

  queueRandomFood: ->
    @createRandomFood()
    @time.addEvent(
      delay: random( 0, 5000 )
      callback: @queueRandomFood
      callbackScope: this
    )
    return

  mousedown: ( pointer ) ->
    @createFood( pointer.worldX, pointer.worldY )
    return


document.addEventListener( "DOMContentLoaded", ->
  new Phaser.Game(
    width: "100%"
    height: "100%"
    transparent: yes
    scene: Scene
    loader:
      baseURL: "https://labs.phaser.io"
      crossOrigin: "anonymous"
    audio:
      noAudio: yes
    physics:
      default: "arcade"
      arcade:
        debug: DEBUG
        debugShowBody: yes
        debugShowVelocity: no
        maxEntries: MAX_ENTRIES
        useTree: no
  )
)

              
            
!
999px

Console