Advertisement
Guest User

Untitled

a guest
Jan 10th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///<reference path="phaser.d.ts"/>
  2.  
  3. module ExtremeCrash {
  4.  
  5.     export class Game extends Phaser.Game {
  6.  
  7.         constructor() {
  8.             super(window.innerWidth, window.innerHeight, Phaser.CANVAS, '', null);
  9.  
  10.             this.state.add("Boot", ExtremeCrash.Scenes.Boot, false);
  11.             this.state.add("Main", ExtremeCrash.Scenes.Main, false);
  12.  
  13.             this.state.start("Boot");
  14.         }
  15.  
  16.     }
  17.  
  18.     export module Entities {
  19.  
  20.         export class Player extends Phaser.Sprite {
  21.  
  22.             constructor(game: Phaser.Game, x: number, y: number, layer: Phaser.TilemapLayer) {
  23.                 super(game, x, y, 'player');
  24.                 game.add.existing(this);
  25.  
  26.                 this.layer = layer;
  27.  
  28.                 game.input.keyboard.addKeyCapture([
  29.                     Phaser.Keyboard.LEFT,
  30.                     Phaser.Keyboard.RIGHT,
  31.                     Phaser.Keyboard.UP,
  32.                     Phaser.Keyboard.DOWN,
  33.                     Phaser.Keyboard.SPACE
  34.                 ]);
  35.  
  36.                 // Cursors
  37.  
  38.                 this.cursors = this.game.input.keyboard.createCursorKeys();
  39.  
  40.                 // Physics
  41.  
  42.                 this.body.collideWorldBounds = true;
  43.                 this.body.bounce.setTo(0.5, 0.2);
  44.                 this.body.gravity.setTo(0, 90);
  45.                 this.body.drag.x = 8000;
  46.                 this.body.allowRotation = false;
  47.                 this.body.maxVelocity.x = 700;
  48.                 this.body.maxVelocity.y = 1650;
  49.  
  50.                 // GUI
  51.  
  52.                 this.accel = {x: 9000, y: -110000};
  53.  
  54.                 var p = ExtremeCrash.Debug.GUI.addFolder("Player");
  55.  
  56.                 var f1 = p.addFolder("Physics")
  57.                     var drag = f1.addFolder("drag");
  58.                         drag.add(this.body.drag, "x", 0, 15000);
  59.                         drag.add(this.body.drag, "y", 0, 15000);
  60.                     var maxVel = f1.addFolder("maxVelocity");
  61.                         maxVel.add(this.body.maxVelocity, "x", 0, 1500);
  62.                         maxVel.add(this.body.maxVelocity, "y", 0, 5000);
  63.                     var gravity = f1.addFolder("gravity");
  64.                         gravity.add(this.body.gravity, "x", -1000, 1000);
  65.                         gravity.add(this.body.gravity, "y", -1000, 1000);
  66.                     var bounce = f1.addFolder("bounce");
  67.                         bounce.add(this.body.bounce, "x", 0, 1);
  68.                         bounce.add(this.body.bounce, "y", 0, 1);
  69.                     var accel = f1.addFolder("accel");
  70.                         accel.add(this.accel, "x", 0, 25000);
  71.                         accel.add(this.accel, "y", -150000, -15000);
  72.  
  73.                 // Camera  
  74.  
  75.                 this.game.camera.follow(this);
  76.  
  77.                 // Animation
  78.  
  79.                 this.animations.add("idle", [0, 1], 2, true);
  80.                 this.animations.add("walk", [2, 3, 4, 5], 10, true);
  81.  
  82.                 var f2 = p.addFolder("Animation")
  83.                     var idle = f2.addFolder("idle");
  84.                         idle.add(this.animations.getAnimation('idle'), "delay", 1, 2000);
  85.                     var walk = f2.addFolder("walk");
  86.                         walk.add(this.animations.getAnimation('walk'), "delay", 1, 2000);
  87.  
  88.                 // Setting center
  89.  
  90.                 this.anchor.setTo(0.5, 0);
  91.             }
  92.  
  93.             update() {
  94.                 this.game.physics.collide(this, this.layer);
  95.  
  96.                 var play = "idle";
  97.  
  98.                 this.body.acceleration.x = this.body.acceleration.y = 0;
  99.  
  100.                 this.scale.x = 1;
  101.  
  102.                 if (this.cursors.left.isDown) {
  103.                     this.body.acceleration.x = -this.accel.x;
  104.                     this.scale.x = -1;
  105.                     play = "walk";
  106.                 }
  107.                 else if (this.cursors.right.isDown) {
  108.                     this.body.acceleration.x = this.accel.x;
  109.                     play = "walk";
  110.                 }
  111.                 if(this.cursors.up.isDown && this.body.touching.down) {
  112.                     this.body.acceleration.y = this.accel.y;
  113.                 }
  114.  
  115.                 this.animations.play(play);
  116.             }
  117.  
  118.             render() {
  119.             }
  120.  
  121.         }
  122.  
  123.     }
  124.  
  125.     export module Scenes {
  126.  
  127.         export class Boot extends Phaser.State {
  128.  
  129.             preload() {
  130.                 this.load.spritesheet("player", "/img/playerWalk.png", 60, 126);
  131.                 this.game.load.tilemap('map', '/maps/industry.json', null, Phaser.Tilemap.TILED_JSON);
  132.                 this.game.load.tileset('tileset', '/img/industry_level_tiles.png', 40, 40);
  133.  
  134.                 this.game.stage.backgroundColor = "#FFF";
  135.             }
  136.  
  137.             create() {
  138.                 this.game.state.start("Main", true, false)
  139.             }
  140.  
  141.         }
  142.  
  143.  
  144.         export class Main extends Phaser.State {
  145.  
  146.             map: Phaser.Tilemap;
  147.             tileset: Phaser.Tileset;
  148.             layers = Array<Phaser.TilemapLayer>();
  149.             player: Phaser.Sprite;
  150.  
  151.             constructor() {
  152.                 super(arguments);
  153.             }
  154.  
  155.             create() {
  156.                 this.map = this.game.add.tilemap('map');
  157.                 this.tileset = this.game.add.tileset('tileset');
  158.  
  159.                 this.tileset.setCollisionRange(0, 70, true, true, true, true);
  160.  
  161.                 var collisionLayer;
  162.  
  163.                 for(var i = 0; i < this.map.layers.length; i++) {
  164.                     var layer = this.game.add.tilemapLayer(0, 0, this.map.layers[i].width*this.tileset.tileWidth, this.map.layers[i].height*this.tileset.tileHeight, this.tileset, this.map, i);
  165.  
  166.                     layer.fixedToCamera = false;
  167.                     layer.resizeWorld();
  168.  
  169.                     if(this.map.layers[i].name == "Collision" && !this.player) {
  170.                         layer.render = function() { return true; };
  171.                         this.player = new Entities.Player(this.game, this.game.world.centerX, this.game.world.centerY, layer);
  172.                     }
  173.  
  174.                     this.layers.push(layer);
  175.                 }
  176.                
  177.             }
  178.  
  179.             render() {
  180.             }
  181.  
  182.             update() {
  183.             }
  184.  
  185.         }
  186.  
  187.     }
  188.  
  189. }
  190.  
  191. window.onload = () => {
  192.  
  193.     game = new ExtremeCrash.Game();
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement