import Phaser from 'phaser';

export default class LevelScene extends Phaser.Scene {
    constructor() {
        super({ key: 'LevelScene' });
        this.player = null;
        this.cursors = null;
        this.camera = null;
    }

    create() {
        // Load the tilemap
        const map = this.add.tilemap('map');
        const tileset = map.addTilesetImage('tileset', 'tileset');

        // Create the ground layer
        const groundLayer = map.createStaticLayer('Ground', tileset, 0, 0);

        // Add player
        this.player = this.physics.add.sprite(100, 100, 'player');
        this.player.setBounce(0.2);
        this.player.setCollideWorldBounds(true);

        // Input
        this.cursors = this.input.keyboard.createCursorKeys();

        // Physics collision
        this.physics.add.collider(this.player, groundLayer);

        // Camera follow player
        this.cameras.main.startFollow(this.player);
    }

    update() {
        if (this.cursors.left.isDown) {
            this.player.setVelocityX(-200);
        } else if (this.cursors.right.isDown) {
            this.player.setVelocityX(200);
        } else {
            this.player.setVelocityX(0);
        }

        if (this.cursors.up.isDown) {
            this.player.setVelocityY(-200);
        } else if (this.cursors.down.isDown) {
            this.player.setVelocityY(200);
        } else {
            this.player.setVelocityY(0);
        }
    }
}