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.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

/**
 * Created by ADY on 26.1.2018.
 */

public class Character {
    public float bombsize=0f;
    Sprite sprite;
    Vector2 center;
    Vector2 position;
    int[] replay = new int[2400];
    int turn = 0;
    float worldradius;
    float height = 0;
    float force = 0;
    float horizonforce = 0;
    int jump = 0;
    World world;
    float angle = 90f;
    float fakeangle = 90f;
    int time = -1;
    Rectangle body = new Rectangle();
    private int created = 0;
    private float jumpforce=25f;
    public float bombchange=0.001f;

    public Character(Texture text, World world) {
        sprite = new Sprite(text);
        sprite.setOrigin(text.getWidth() / 2f, 5f);
        sprite.setCenter(text.getWidth() / 2f, 5f);
        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);
        this.world = world;
        body.set(position.x, position.y, 64, 128);
    }

    public void act(Array<FakeCharacter> fakes) {
        sprite.setRotation(angle - 90);
        position = center.cpy().add(MathUtils.cosDeg(angle) * (worldradius + height), MathUtils.sinDeg(angle) * (worldradius + height));
        sprite.setOriginBasedPosition(position.x, position.y);
        body.set(position.x - 32, position.y - 5, 64, 128);
        time++;

        bombsize+=bombchange;
        //System.out.println(bombsize+"bombo");
        if (time == 1200) {
            time = 0;
            //fakeangle+=27f;
            turn += 90;
            if (created++ < 3) {
                fakes.add(new FakeCharacter(sprite.getTexture(), world, fakeangle, replay, height, turn));
            }
        }
        height += force;

        force -= world.acc;
        if (height <= 0) {
            height = 0;
            force = 0;
            jump = 0;
        }
        replay[time] = 0;
        jumpforce=20f;
    }

    public void jump() {
        if (jump < 2) {
            jump++;
            force = jumpforce;
            if(time>0)
            replay[time] = 1;
        }
    }

    public void shoot() {
        replay[time] = 2;
    }

    public void left() {
        angle += 0.2f;
        replay[time] += 10;
    }

    public void right() {
        angle -= 0.2f;
        replay[time] += 20;
    }

    public void jumprocket() {
        if (jump < 2) {
            jump++;
            force = 200;
            if(time>0)
                replay[time] +=40 ;
        }
    }
    public void die() {
        System.out.println("dead now");
        //Gdx.app.exit();
    }

}
