Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///<reference path="phaser.d.ts"/>
- module ExtremeCrash {
- export class Game extends Phaser.Game {
- constructor() {
- super(window.innerWidth, window.innerHeight, Phaser.CANVAS, '', null);
- this.state.add("Boot", ExtremeCrash.Scenes.Boot, false);
- this.state.add("Main", ExtremeCrash.Scenes.Main, false);
- this.state.start("Boot");
- }
- }
- export module Entities {
- export class Player extends Phaser.Sprite {
- constructor(game: Phaser.Game, x: number, y: number, layer: Phaser.TilemapLayer) {
- super(game, x, y, 'player');
- game.add.existing(this);
- this.layer = layer;
- game.input.keyboard.addKeyCapture([
- Phaser.Keyboard.LEFT,
- Phaser.Keyboard.RIGHT,
- Phaser.Keyboard.UP,
- Phaser.Keyboard.DOWN,
- Phaser.Keyboard.SPACE
- ]);
- // Cursors
- this.cursors = this.game.input.keyboard.createCursorKeys();
- // Physics
- this.body.collideWorldBounds = true;
- this.body.bounce.setTo(0.5, 0.2);
- this.body.gravity.setTo(0, 90);
- this.body.drag.x = 8000;
- this.body.allowRotation = false;
- this.body.maxVelocity.x = 700;
- this.body.maxVelocity.y = 1650;
- // GUI
- this.accel = {x: 9000, y: -110000};
- var p = ExtremeCrash.Debug.GUI.addFolder("Player");
- var f1 = p.addFolder("Physics")
- var drag = f1.addFolder("drag");
- drag.add(this.body.drag, "x", 0, 15000);
- drag.add(this.body.drag, "y", 0, 15000);
- var maxVel = f1.addFolder("maxVelocity");
- maxVel.add(this.body.maxVelocity, "x", 0, 1500);
- maxVel.add(this.body.maxVelocity, "y", 0, 5000);
- var gravity = f1.addFolder("gravity");
- gravity.add(this.body.gravity, "x", -1000, 1000);
- gravity.add(this.body.gravity, "y", -1000, 1000);
- var bounce = f1.addFolder("bounce");
- bounce.add(this.body.bounce, "x", 0, 1);
- bounce.add(this.body.bounce, "y", 0, 1);
- var accel = f1.addFolder("accel");
- accel.add(this.accel, "x", 0, 25000);
- accel.add(this.accel, "y", -150000, -15000);
- // Camera
- this.game.camera.follow(this);
- // Animation
- this.animations.add("idle", [0, 1], 2, true);
- this.animations.add("walk", [2, 3, 4, 5], 10, true);
- var f2 = p.addFolder("Animation")
- var idle = f2.addFolder("idle");
- idle.add(this.animations.getAnimation('idle'), "delay", 1, 2000);
- var walk = f2.addFolder("walk");
- walk.add(this.animations.getAnimation('walk'), "delay", 1, 2000);
- // Setting center
- this.anchor.setTo(0.5, 0);
- }
- update() {
- this.game.physics.collide(this, this.layer);
- var play = "idle";
- this.body.acceleration.x = this.body.acceleration.y = 0;
- this.scale.x = 1;
- if (this.cursors.left.isDown) {
- this.body.acceleration.x = -this.accel.x;
- this.scale.x = -1;
- play = "walk";
- }
- else if (this.cursors.right.isDown) {
- this.body.acceleration.x = this.accel.x;
- play = "walk";
- }
- if(this.cursors.up.isDown && this.body.touching.down) {
- this.body.acceleration.y = this.accel.y;
- }
- this.animations.play(play);
- }
- render() {
- }
- }
- }
- export module Scenes {
- export class Boot extends Phaser.State {
- preload() {
- this.load.spritesheet("player", "/img/playerWalk.png", 60, 126);
- this.game.load.tilemap('map', '/maps/industry.json', null, Phaser.Tilemap.TILED_JSON);
- this.game.load.tileset('tileset', '/img/industry_level_tiles.png', 40, 40);
- this.game.stage.backgroundColor = "#FFF";
- }
- create() {
- this.game.state.start("Main", true, false)
- }
- }
- export class Main extends Phaser.State {
- map: Phaser.Tilemap;
- tileset: Phaser.Tileset;
- layers = Array<Phaser.TilemapLayer>();
- player: Phaser.Sprite;
- constructor() {
- super(arguments);
- }
- create() {
- this.map = this.game.add.tilemap('map');
- this.tileset = this.game.add.tileset('tileset');
- this.tileset.setCollisionRange(0, 70, true, true, true, true);
- var collisionLayer;
- for(var i = 0; i < this.map.layers.length; i++) {
- 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);
- layer.fixedToCamera = false;
- layer.resizeWorld();
- if(this.map.layers[i].name == "Collision" && !this.player) {
- layer.render = function() { return true; };
- this.player = new Entities.Player(this.game, this.game.world.centerX, this.game.world.centerY, layer);
- }
- this.layers.push(layer);
- }
- }
- render() {
- }
- update() {
- }
- }
- }
- }
- window.onload = () => {
- game = new ExtremeCrash.Game();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement