package de.miaurizius.jgame2d.entity.particle;

import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.Entity;

import java.awt.*;

public class Particle extends Entity {

    GamePanel panel;
    Entity generator;
    Color color;
    int size;
    int xd, yd;

    public Particle(GamePanel panel, Entity generator, Color color, int size, int speed, int maxLife, int xd, int yd) {
        super(panel);
        this.panel = panel;
        this.generator = generator;
        this.color = color;
        this.size = size;
        this.speed = speed;
        this.maxLife = maxLife;
        this.xd = xd;
        this.yd = yd;

        life = maxLife;
        int offset = (panel.tileSize/2) - (size/2);
        worldX = generator.worldX + offset;
        worldY = generator.worldY + offset;
    }

    @Override
    public void update() {
        life--;
        if(life < maxLife/3) yd++;
        worldX += xd*speed;
        worldY += yd*speed;
        alive = (life != 0);
    }

    @Override
    public void draw(Graphics2D graphics2d) {
        int screenX = worldX - panel.player.worldX + panel.player.screenX;
        int screenY = worldY - panel.player.worldY + panel.player.screenY;

        graphics2d.setColor(color);
        graphics2d.fillRect(screenX, screenY, size, size);
    }
}
