package org.academiadecodigo.bootcamp.car;

import org.academiadecodigo.bootcamp.Bullet;
import org.academiadecodigo.bootcamp.grid.position.GridPosition;

import java.util.ArrayList;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class Tank extends Car {

    public static final int SPEED = 1;
    private Bullet bullet;

    public Tank(GridPosition pos) {
        super(pos, CarType.TANK);
    }

    @Override
    public void move() {
        accelerate(chooseDirection(), Tank.SPEED);
        shoot();
        if (bullet != null) {
            bullet.move();
        }
    }

    @Override
    public void crash() {
        ArrayList<Car> crashed = collisionDetector.getCarsInSamePosition(getPos());

        for (Car car : crashed) {
            car.setDirectionChangeLevel(0);
            car.forceMove(car.chooseDirection(), 1);
        }
    }

    public void shoot() {
        if (bullet == null && Math.random() > 0.5) {
            bullet = new Bullet(currentDirection, getPos());
        }
    }
}
