package com.ady.timerunner;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

/**
 * Created by ADY on 27.1.2018.
 */

public class FakeCharacter {
    Sprite sprite;
    Vector2 center;
    Vector2 position;
    int[] replay = new int[2400];
    float worldradius;
    float height = 0;
    float force = 0;
    float acc = 0.6f;
    float angle = 90f;
    int time = -1;
    World world;
    float alpha = 0.7f;
    public float elapsedTime;

    public FakeCharacter(Texture text, World world, float angle, int[] replay,float height,float turn) {
        for (int i = 0; i < 2400; i++) {
            this.replay[i] = replay[i];
        }
        time-=turn;
        this.world = world;
        this.angle = angle+0.5f;
        sprite = new Sprite(text);
        sprite.setOrigin(text.getWidth() / 2f, 5f);
        sprite.setCenter(text.getWidth() / 2f, 5f);
        sprite.setAlpha(0.7f);
        center = world.center;
        worldradius = world.size / 2f;
        position = center.cpy().add(MathUtils.cosDeg(angle) * (worldradius + height), MathUtils.sinDeg(angle) * (height + worldradius));
        sprite.setRotation(angle - 90);
        sprite.setOriginBasedPosition(position.x, position.y);
    }


    public void act(Array<Bullet> bullets) {
        elapsedTime += Gdx.graphics.getDeltaTime();
        sprite.setRotation(angle - 90f);
        position = center.cpy().add(MathUtils.cosDeg(angle) * (worldradius + height), MathUtils.sinDeg(angle) * (worldradius + height));
        sprite.setOriginBasedPosition(position.x, position.y);
        time++;
        if (time == 1200) {
            time = 0;
            alpha *= 0.7f;
            sprite.setAlpha(alpha);
        }
        if (time >= 0) {
            if (replay[time] >= 10) {
                int com = replay[time] / 10;
                if (com == 1) {
                    angle -= 0.2f;
                } else if (com == 2) {
                    angle += 0.2f;
                }else if(com==4)
                {
                        force = 200;
                }
                replay[time] -= com * 10;
            }


            if (replay[time] == 1) {
                force = 20;
            } else if (replay[time] == 2) {
                bullets.add(new Bullet(world, angle, height+30));
            }
        }
        else
        {
            angle+=0.3f;
        }
        height += force;

        force -= world.acc;
        if (height < 0) {
            height = 0;
            force = 0;
        }

    }
}
