package de.miaurizius.jgame2d.entity.projectile;

import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.entity.Entity;

public class Projectile extends Entity {

    Entity user;
    //TODO: parry projectiles: https://youtu.be/8KitXuxcuqI?list=PL_QPQmz5C6WUF-pOQDsbsKbaBZqXj4qSq&t=75

    public Projectile(GamePanel panel) {
        super(panel);
    }

    public void set(int worldX, int worldY, Direction direction, boolean alive, Entity user) {
        this.worldX = worldX;
        this.worldY = worldY;
        this.direction = direction;
        this.alive = alive;
        this.user = user;
        this.life = maxLife;
    }

    public void update() {

        if(user.type == EntityType.PLAYER) {
            int monsterIndex = panel.collisionH.checkEntity(this, panel.monster[panel.currentMap.getIndex()]);
            if(monsterIndex != 999) {
                panel.player.damageMonster(monsterIndex, this, attack, knockbackVal);
                generateParticle(user.projectile, panel.monster[panel.currentMap.getIndex()][monsterIndex]);
                alive = false;
            }
        }

        if(user.type == EntityType.MONSTER) {
            if(!panel.player.invincible && panel.collisionH.checkPlayer(this)) {
                damagePlayer(attack);
                generateParticle(user.projectile, panel.player);
                alive = false;
            }
        }

        switch(direction) {
            case UP -> worldY -= speed;
            case DOWN -> worldY += speed;
            case LEFT -> worldX -= speed;
            case RIGHT -> worldX += speed;
        }

        life--;
        if(life <= 0) {
            alive = false;
            return;
        }

        spriteCount++;
        if(spriteCount > 12) {
            if(spriteNum == 1) spriteNum = 2;
            else if(spriteNum == 2) spriteNum = 1;
            else spriteNum = 0;
            spriteCount = 0;
        }
    }

}
