objects/items/itempickup.js



import Phaser from "phaser";
const HealthPotion = require("./healthPotion");

const dataManager = require('../data');
const FlameThrower = require('../weapons/flamethrower');

/**
 * @static
 * @final
 * @memberof ClubCrawler.Objects.Items
 * @description Map of which values set in the map create which items
 */
const GETTABLES = {
    flamethrower: {
        makes: FlameThrower,
        type:"weapon",
        sprite:"flamethrower"
    },
    error: { // not implemented
        makes: null,
        type: null,
        sprite: "errorSprite"
    },
    healthPotion: {
        makes: HealthPotion,
        type: "stackable",
        sprite:"blue-potion"
    }
}

/** 
 * @classdesc 
 * A class for picking up a gettable item
 * 
 * @memberof ClubCrawler.Objects.Items
 * @extends Phaser.GameObjects.Image
*/
class ItemPickup extends Phaser.GameObjects.Image {

    /**
     * Description
     * @param {ClubCrawler.Types.GameItemConfig} gameItemConfig - The game item config as generated by Tiled and the Parser
     */
    constructor(gameItemConfig) {
        let gettable;
        if(GETTABLES[gameItemConfig.gettable]) {
            gettable = GETTABLES[gameItemConfig.gettable];
        }
        else {
            gettable = GETTABLES.error
        }
        super(
            gameItemConfig.scene, 
            gameItemConfig.x,
            gameItemConfig.y,
            gettable.sprite
        );
        this.gettable = gettable;
        this.scene.add.existing(this);
        this.scene.physics.add.existing(this);
        this.setOrigin(0,0);
        this.setDepth(0);
        this.scene.physics.add.overlap(this.scene.player, this, this.onPlayerTouch, null, this);
    }

    /**
     * If succesfully added to inventory, the pickup will be deleted.
     */
    onPlayerTouch() {
        let player = this.scene.player;
    
        if(dataManager.debug.items.overlap) {
            dataManager.log(`player touched ${this.gettable} gettable!`);
        }

        let pickupResult = player.inventory.addItem(new this.gettable.makes({}));

        if(dataManager.debug.items.pickup) {
            dataManager.log(`pickup result: ${pickupResult}`);
        }
        if(pickupResult) {
            this.destroy(true);
        }
    }

}

module.exports = ItemPickup;