Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var game = new Phaser.Game(192, 144, Phaser.CANVAS, 'phaser', { preload: preload, create: create, update: update, render: render });
- function preload() {
- game.load.image('bg', 'assets/background.png');
- game.load.image('ground', 'assets/ground.png');
- game.load.image('mrPixel', 'assets/mrPixel.png');
- game.load.image('mrMan', 'assets/mrMan.png');
- game.load.image('spike', 'assets/spike.png');
- game.load.image('spike_flipped', 'assets/spike_flipped.png');
- game.load.image('dialogBox', 'assets/dialogBox_1.png');
- game.load.image('dialogBox2', 'assets/dialogBox_2.png');
- game.load.image('dialogBox3', 'assets/dialogBox_3.png');
- game.load.tilemap('level_1', 'assets/tilemaps/level_1.json', null, Phaser.Tilemap.TILED_JSON);
- }
- var pixelwidth = 0;
- var pixelheight = 0;
- var runOnce = 0;
- var dialogStarted = true;
- var cursors;
- var gravityKey;
- var mrPixel;
- var mrManGroup;
- var mrMan;
- var cameraPos = new Phaser.Point(0, 0);
- function create() {
- game.stage.backgroundColor = '#DDDDDD';
- game.world.setBounds(0, 0, 384, 288);
- fixScaling();
- tilemaps();
- character();
- createObject();
- enablePhysics();
- game.camera.roundPx = true;
- game.renderer.renderSession.roundPixels = true;
- //cameraPos.setTo(mrPixel.x, mrPixel.y);
- game.camera.follow(mrPixel);
- }
- function update() {
- updateDialog();
- //updateCamera();
- moveCharacter();
- game.physics.arcade.collide(mrPixel, blockedLayer);
- game.physics.arcade.overlap(mrPixel, mrManGroup, startDialog, null, this);
- game.physics.arcade.overlap(mrPixel, killerGroup, respawn, null, this);
- gravityFunction();
- }
- function render() {
- pixelcontext.drawImage(game.canvas, 0, 0, 192, 144, 0, 0, pixelwidth, pixelheight);
- }
- function fixScaling() {
- var pixelCanvas = document.getElementById("pixel");
- pixelcontext = pixelCanvas.getContext("2d");
- pixelwidth = pixelCanvas.width;
- pixelheight = pixelCanvas.height;
- Phaser.Canvas.setSmoothingEnabled(pixelcontext, false);
- }
- function tilemaps() {
- map = game.add.tilemap('level_1');
- map.addTilesetImage('background', 'bg');
- map.addTilesetImage('ground', 'ground');
- backgroundLayer = map.createLayer('backgroundLayer');
- blockedLayer = map.createLayer('blockedLayer');
- map.setCollisionBetween(1, 100000, true, 'blockedLayer');
- backgroundLayer.scrollFactorX = 0.50;
- backgroundLayer.scrollFactorY = 0.50;
- }
- function character() {
- cursors = game.input.keyboard.createCursorKeys();
- gravityKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
- mrPixel = game.add.sprite(25, 25, 'mrPixel');
- mrPixel.anchor.setTo(0.5, 0.5);
- }
- function updateCamera() {
- var lerp = 0.05;
- cameraPos.x += (mrPixel.x - cameraPos.x) * lerp;
- cameraPos.y += (mrPixel.y - cameraPos.y) * lerp;
- game.camera.focusOnXY(cameraPos.x, cameraPos.y);
- }
- function enablePhysics() {
- game.physics.startSystem(Phaser.Physics.ARCADE);
- game.physics.arcade.enable([mrPixel]);
- game.physics.arcade.gravity.y = 800;
- mrPixel.body.enable = true;
- }
- function moveCharacter() {
- if (runOnce == 0) {
- if (cursors.left.isDown) {
- mrPixel.body.x -= 1;
- } else if (cursors.right.isDown) {
- mrPixel.body.x += 1;
- }
- game.camera.focusOnXY(mrPixel.x, mrPixel.y);
- }
- }
- function gravityFunction() {
- if (runOnce == 0) {
- if (gravityKey.isDown && mrPixel.body.blocked.down) {
- game.physics.arcade.gravity.y = -800;
- }
- if (gravityKey.isDown && mrPixel.body.blocked.up) {
- game.physics.arcade.gravity.y = 800;
- }
- }
- }
- function createObject() {
- mrManGroup = game.add.group();
- mrManGroup.enableBody = true;
- map.createFromObjects('objectLayer', 1730, 'mrMan', 0, true, false, mrManGroup);
- mrManGroup.forEach(function(mrMan){
- mrMan.body.allowGravity = false;
- mrMan.anchor.setTo(0.5, 0);
- mrMan.body.setSize(32, 8);
- mrMan.body.immovable = true;
- });
- killerGroup = game.add.group();
- killerGroup.enableBody = true;
- map.createFromObjects('objectLayer', 1731, 'spike', 0, true, false, killerGroup);
- map.createFromObjects('objectLayer', 1732, 'spike_flipped', 1, true, false, killerGroup);
- killerGroup.forEach(function(spike){
- spike.body.allowGravity = false;
- spike.body.immovable = true;
- });
- }
- function updateDialog() {
- if (runOnce == 1) {
- dialogBox = game.add.sprite(96, 165, 'dialogBox');
- dialogBox.anchor.setTo(0.5, 0.5);
- dialogBox.fixedToCamera = true;
- game.add.tween(dialogBox.cameraOffset).to( { y: 125 }, 0, Phaser.Easing.Back.In, true);
- runOnce = 2;
- } else if (runOnce == 2) {
- if (gravityKey.isDown && runOnce == 2) {
- dialogBox.destroy();
- dialogBox = game.add.sprite(96, 125, 'dialogBox2');
- dialogBox.anchor.setTo(0.5, 0.5);
- dialogBox.fixedToCamera = true;
- game.time.events.add(Phaser.Timer.SECOND * 4, removeDialog, this);
- runOnce = 3;
- }
- }
- }
- function removeDialog() {
- runOnce = 0;
- dialogBox.destroy();
- dialogBox = game.add.sprite(96, 125, 'dialogBox3');
- dialogBox.anchor.setTo(0.5, 0.5);
- dialogBox.fixedToCamera = true;
- game.add.tween(dialogBox).to( { alpha: 0 }, 3500, Phaser.Easing.Linear.None, true);
- }
- function startDialog() {
- if (dialogStarted) {
- runOnce = 1;
- dialogStarted = false;
- }
- }
- function respawn() {
- mrPixel.x = 25;
- mrPixel.y = 25;
- }
- /*function findObjects (type, map, layer) {
- var result = new Array();
- map.objects[layer].forEach(function(element){
- if(element.properties.type === type) {
- element.y -= map.tileHeight;
- result.push(element);
- }
- });
- return result;
- }
- function createSprite (element, group) {
- var sprite = group.create(element.x, element.y, element.properties.sprite);
- Object.keys(element.properties).forEach(function(key){
- sprite[key] = element.properties[key];
- });
- }
- function createObjects() {
- //create items
- mrManGroup = game.add.group();
- mrManGroup.enableBody = true;
- result = findObjects('dialog_1', map, 'objectLayer');
- result.forEach(function(element){
- createSprite(element, mrManGroup);
- });
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement