using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spaceship : MonoBehaviour, IPlayer
{
    public Rect Bounds;
    public float Speed;
    public int StartHealth = 100;

    private new Rigidbody rigidbody;
    private CenterBar bar;

    public int Health { get; set; }

    public void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
        Health = StartHealth;
        bar = GetComponentInChildren<CenterBar>();
    }

    public void Update()
    {
        if (!EndCondition.IsGameOver)
        {
            bar.SetValue((float)Health / StartHealth);
        }
        else
        {
            bar.SetValue(0);
        }
    }

    public void FixedUpdate()
    {
        if (EndCondition.IsGameOver)
        {
            return;
        }

        var vertical = Input.GetAxis("Vertical");
        var horizontal = Input.GetAxis("Horizontal");

        var pos = rigidbody.position;
        pos.x = Mathf.Clamp(pos.x + horizontal * Speed, Bounds.xMin, Bounds.xMax);
        pos.z = Mathf.Clamp(pos.z + vertical * Speed, Bounds.yMin, Bounds.yMax);
        rigidbody.position = pos;
    }
}
