<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInput : MonoBehaviour
{
    public string inputPostfix;
    public float AxisMoveX { get; private set; }
    public float AxisMoveY { get; private set; }
    public bool Punch { get; private set; }
    
    // Update is called once per frame
    void Update()
    {
        AxisMoveX = Input.GetAxisRaw("Horizontal_"+ inputPostfix);
        AxisMoveY = Input.GetAxisRaw("Vertical_" + inputPostfix);
        Punch = Input.GetButtonDown("Fire_" + inputPostfix);
    }

    
}
<file_sep>﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public struct PlayerSettingContainer 
{
    private int id;
    public PlayerController playerController;
    public HeadquarterController headquarter;
    public UIHealthManager uiHealthHeadquarterController;
    public UIHealthManager uiHealthPlayerController;

    internal void SetPlayerId(int id)
    {
        this.id = id;
        playerController.SetPlayerId(id);
        headquarter.SetPlayerId(id);

        uiHealthPlayerController.SetHealthManager(playerController);
        uiHealthHeadquarterController.SetHealthManager(headquarter);
    }

    public int GetId()
    {
        return id;
    }

}
<file_sep>﻿using System;

public interface IHealthManager 
{
    event Action<int,int> OnHealthChange;
    event Action<int> OnHealtZero;
    int GetMaxHealth();
    int GetHealth();
    void ModifyHealt(int modifier);
}
<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour
{
    public static GameController INSTANCE { get; private set; }
    

    private void Awake()
    {
        if (INSTANCE == null)
        {
            INSTANCE = this;
        }
        else
        {
            Destroy(this);
        }
    }

    private void Start()
    {
        foreach(PlayerSettingContainer container in PlayerSettingsManager.INSTANCE.playersContainers)
        {
            container.headquarter.OnHealtZero += OnPlayerLoose;
        }
    }

    private void OnPlayerLoose(int playerId) 
    {

    }

}
<file_sep>﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeadquarterController : MonoBehaviour, IHealthManager
{
    public event Action<int,int> OnHealthChange;
    public event Action<int> OnHealtZero;
    
    public int maxHealth;

    private int health;
    private int playerId;

    private void Awake()
    {
        health = maxHealth;
    }

    

    public int GetPlayerId()
    {
        return playerId;
    }

    public int GetHealth()
    {
        return health;
    }

    public int GetMaxHealth()
    {
        return maxHealth;
    }

    public void ModifyHealt(int modifier)
    {
        health += modifier;
        if (OnHealthChange != null)
            OnHealthChange(playerId,modifier);
        if (health <= 0)
        {
            if (OnHealtZero != null)
                OnHealtZero(playerId);
        }
        else
        {
            if (health > maxHealth)
                health = maxHealth;
        }
    }

    internal void SetPlayerId(int playerId)
    {
        this.playerId = playerId;
    }

}
<file_sep>﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMain : MonoBehaviour
{
    private int id;
    public PlayerController PlayerController { get; private set; }

    private void Awake()
    {
        PlayerController = GetComponent<PlayerController>();
    }

    internal void SetPlayerId(int id)
    {
        this.id = id;
    }

    public int GetId()
    {
        return id;
    }
}
<file_sep>﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour, IHealthManager
{
    public event Action<int, int> OnHealthChange;
    public event Action<int> OnHealtZero;

    public float movementSpeed;
    public int maxEnergy;
    public float stunTime;

    public Vector2 FacingVector { get; private set; }

    private int playerId;
    private PlayerInput playerInput;
    private Rigidbody2D mRigidbody2D;
    private Vector2 currentVelocity;
    private int energy;
    private bool canPunch;

    private void Awake()
    {
        playerInput = GetComponent<PlayerInput>();
        mRigidbody2D = GetComponent<Rigidbody2D>();
        currentVelocity = mRigidbody2D.velocity;
        FacingVector = transform.right;
        energy = maxEnergy;
        canPunch = true;
    }


    void Update()
    {
        if (energy > 0)
        {
            currentVelocity.x = playerInput.AxisMoveX;
            currentVelocity.y = playerInput.AxisMoveY;
            Flip();
            currentVelocity.Normalize();
            mRigidbody2D.velocity = movementSpeed * currentVelocity;
            if (canPunch && playerInput.Punch)
            {
                Debug.Log("punch " + playerInput.inputPostfix);
            }
        }
    }

    void FixedUpdate()
    {
        Facing();
    }

    public int GetHealth()
    {
        return energy;
    }

    public int GetMaxHealth()
    {
        return maxEnergy;
    }

    public void ModifyHealt(int modifier)
    {
        energy += modifier;
        if (OnHealthChange != null)
            OnHealthChange(playerId, modifier);
        if (energy <= 0)
        {
            if (OnHealtZero != null)
                OnHealtZero(playerId);
        }
        else
        {
            if (energy > maxEnergy)
                energy = maxEnergy;
        }
    }

    private void Flip()
    {
        if ((currentVelocity.x * transform.right.x) < 0)
        {
            transform.Rotate(0, 180, 0);
        }

        if ((currentVelocity.y * transform.up.y) < 0)
        {
            transform.Rotate(180, 0, 0);
        }
    }

    private void Facing()
    {
        if (currentVelocity.x != 0 || currentVelocity.y != 0)
        {
            FacingVector = (currentVelocity.x * Vector2.right) + (currentVelocity.y * Vector2.up);
        }
        FacingVector.Normalize();
        Debug.DrawRay(transform.position, FacingVector * 3, Color.red);
    }

    internal void SetPlayerId(int id)
    {
        this.playerId = id;
    }

    public int GetId()
    {
        return this.playerId;
    }
}
<file_sep>﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIHealthManager : MonoBehaviour
{
    
    private IHealthManager healthManager;

    public Slider sliderHealth;
    

    public void SetHealthManager(IHealthManager healthManager)
    {
        if (this.healthManager!=null)
        {
            this.healthManager.OnHealthChange -= OnHealthChange;
            this.healthManager.OnHealtZero -= OnHealthZero;
        }
        this.healthManager = healthManager;

        this.healthManager.OnHealthChange += OnHealthChange;
        this.healthManager.OnHealtZero += OnHealthZero;

        sliderHealth.minValue = 0;
        sliderHealth.maxValue = this.healthManager.GetMaxHealth();
        sliderHealth.value = this.healthManager.GetHealth();
    }

    private void OnHealthChange(int playerId, int modify) 
    {
        sliderHealth.value = sliderHealth.value + modify;
    }
    
    private void OnHealthZero(int playerId) 
    { 

    }
   
}
