From ea9203ca8adb559d70ffd97aeb70adefcac33437 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Tue, 17 Sep 2019 18:52:55 +0200 Subject: [PATCH] Implemented movement AbstractCharacter mainly fleshed out InputManager is done Player movement is done, not ennemy Energy management is done (AbstractCharacter) --- Assets/Prefabs/Player.prefab | 155 +++++++++++++++++++++++ Assets/Prefabs/Player.prefab.meta | 7 + Assets/Scripts/AbstractCharacter.cs | 91 ++++++++++++- Assets/Scripts/AbstractCharacter.cs.meta | 2 +- Assets/Scripts/AbstractShooter.cs | 2 +- Assets/Scripts/BasicShooter.cs | 2 +- Assets/Scripts/EnnemyCharacter.cs.meta | 2 +- Assets/Scripts/InputManager.cs | 23 +++- Assets/Scripts/InputManager.cs.meta | 2 +- Assets/Scripts/Mover.cs | 26 +++- Assets/Scripts/PlayerCharacter.cs | 16 ++- Assets/Scripts/PlayerCharacter.cs.meta | 2 +- 12 files changed, 304 insertions(+), 26 deletions(-) create mode 100644 Assets/Prefabs/Player.prefab create mode 100644 Assets/Prefabs/Player.prefab.meta diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab new file mode 100644 index 0000000..d86428d --- /dev/null +++ b/Assets/Prefabs/Player.prefab @@ -0,0 +1,155 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &2690590977808455807 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2690590977808455801} + - component: {fileID: 2690590977808455806} + - component: {fileID: 2690590977808455800} + - component: {fileID: 2690590977808455797} + - component: {fileID: 2690590977808455802} + - component: {fileID: 2690590977808455803} + - component: {fileID: 2690590977808455796} + m_Layer: 0 + m_Name: Player + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &2690590977808455801 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2690590977808455807} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0.051522817, y: -0.05536398, z: -0.18232574} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &2690590977808455806 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2690590977808455807} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 21300000, guid: 1fe77f169e8b4289c95cae0cff058bee, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 0.1, y: 0.1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!58 &2690590977808455800 +CircleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2690590977808455807} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + serializedVersion: 2 + m_Radius: 0.25 +--- !u!114 &2690590977808455797 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2690590977808455807} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 35183fd85dde156de82c2af47fea6156, type: 3} + m_Name: + m_EditorClassIdentifier: + _maxSpeed: 10 + _maxHealth: 3 + _maxEnergy: 100 + _energyRegen: 20 +--- !u!114 &2690590977808455802 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2690590977808455807} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8d0d4226322cf15c8b3f89217d9b1c66, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &2690590977808455803 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2690590977808455807} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 2a3ed7cf697d2bd43836d1b20c50008e, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &2690590977808455796 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2690590977808455807} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 61bf45c54c3d922598ce965db2dc869c, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Prefabs/Player.prefab.meta b/Assets/Prefabs/Player.prefab.meta new file mode 100644 index 0000000..b4784e1 --- /dev/null +++ b/Assets/Prefabs/Player.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 3ae3c84bd570eb26d86ed9ec4e5fcba0 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/AbstractCharacter.cs b/Assets/Scripts/AbstractCharacter.cs index 1bd1bb6..d001464 100644 --- a/Assets/Scripts/AbstractCharacter.cs +++ b/Assets/Scripts/AbstractCharacter.cs @@ -2,17 +2,100 @@ using System.Collections.Generic; using UnityEngine; -public class AbstractCharacter : MonoBehaviour +public abstract class AbstractCharacter : MonoBehaviour { + // + // Core components of a character + // + protected Mover _mover; + protected AbstractShooter _shooter; + + // + // Characteristics and status of a character + // + // Speed in m/s + [SerializeField] protected float _maxSpeed; + public float MaxSpeed + { + get { return _maxSpeed; } + set { _maxSpeed = value; } + } + + [SerializeField] protected int _maxHealth; + public int MaxHealth => _maxHealth; + public int CurrentHealth { get; set; } + + [SerializeField] protected float _maxEnergy; + public float MaxEnergy => _maxEnergy; + public float CurrentEnergy { get; set; } + // Energy gained back per second + [SerializeField] private float _energyRegen; + public float EnergyRegen + { + get { return _energyRegen; } + set { _energyRegen = value; } + } + + protected bool _isShooting; + // True if the energy went down to zero + protected bool _isDepleted; + // Start is called before the first frame update void Start() { - + MaxSpeed = 10; + _maxHealth = 1; + CurrentHealth = MaxHealth; + _maxEnergy = 100; + CurrentEnergy = MaxEnergy; + EnergyRegen = 20; + + _mover = GetComponentInParent(); + _shooter = GetComponentInParent(); + + if (_mover == null) + { + throw new MissingComponentException("No Mover !"); + } + + if (_shooter == null) + { + throw new MissingComponentException("No Shooter !"); + } } - // Update is called once per frame + // Manages the energy of the character void Update() { - + if (!_isShooting && CurrentEnergy < MaxEnergy) + { + var regen = _isDepleted ? EnergyRegen * 0.75f : EnergyRegen; + CurrentEnergy += regen * Time.deltaTime; + } + + if (CurrentEnergy > MaxEnergy) + { + CurrentEnergy = MaxEnergy; + if (_isDepleted) + { + _isDepleted = false; + } + } + if (CurrentEnergy < 0) + { + CurrentEnergy = 0; + } + + // The InputManager is executed before and updates it to true if needed + _isShooting = false; + } + + public abstract void Move(Vector2 movementDirection); + public abstract void Shoot(); + + // TODO : Manage hurt + OnCollision + Death'n stuff + public void Hurt() + { + throw new System.NotImplementedException(); } } diff --git a/Assets/Scripts/AbstractCharacter.cs.meta b/Assets/Scripts/AbstractCharacter.cs.meta index 3cc7afb..6d273b5 100644 --- a/Assets/Scripts/AbstractCharacter.cs.meta +++ b/Assets/Scripts/AbstractCharacter.cs.meta @@ -4,7 +4,7 @@ MonoImporter: externalObjects: {} serializedVersion: 2 defaultReferences: [] - executionOrder: 0 + executionOrder: -2 icon: {instanceID: 0} userData: assetBundleName: diff --git a/Assets/Scripts/AbstractShooter.cs b/Assets/Scripts/AbstractShooter.cs index af97aa9..a44c212 100644 --- a/Assets/Scripts/AbstractShooter.cs +++ b/Assets/Scripts/AbstractShooter.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using UnityEngine; -public class Shooter : MonoBehaviour +public class AbstractShooter : MonoBehaviour { // Start is called before the first frame update void Start() diff --git a/Assets/Scripts/BasicShooter.cs b/Assets/Scripts/BasicShooter.cs index 17f8074..a4df3d7 100644 --- a/Assets/Scripts/BasicShooter.cs +++ b/Assets/Scripts/BasicShooter.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using UnityEngine; -public class PlayerShooter : MonoBehaviour +public class BasicShooter : AbstractShooter { // Start is called before the first frame update void Start() diff --git a/Assets/Scripts/EnnemyCharacter.cs.meta b/Assets/Scripts/EnnemyCharacter.cs.meta index 3b87522..949d5ae 100644 --- a/Assets/Scripts/EnnemyCharacter.cs.meta +++ b/Assets/Scripts/EnnemyCharacter.cs.meta @@ -4,7 +4,7 @@ MonoImporter: externalObjects: {} serializedVersion: 2 defaultReferences: [] - executionOrder: 0 + executionOrder: -3 icon: {instanceID: 0} userData: assetBundleName: diff --git a/Assets/Scripts/InputManager.cs b/Assets/Scripts/InputManager.cs index 00a893a..f50b53d 100644 --- a/Assets/Scripts/InputManager.cs +++ b/Assets/Scripts/InputManager.cs @@ -4,15 +4,32 @@ using UnityEngine; public class InputManager : MonoBehaviour { - // Start is called before the first frame update + + private AbstractCharacter _character; void Start() { - + _character = GetComponentInParent(); } - // Update is called once per frame void Update() { + // + // Manage movement inputs + // + var movementDirection = Vector2.zero; + + movementDirection += Input.GetAxis("Horizontal") * Vector2.right; + movementDirection += Input.GetAxis("Vertical") * Vector2.up; + _character.Move(movementDirection); + + // + // Manage action inputs + // + + if (Input.GetButton("Fire1")) + { + _character.Shoot(); + } } } diff --git a/Assets/Scripts/InputManager.cs.meta b/Assets/Scripts/InputManager.cs.meta index 8d722a1..eb10588 100644 --- a/Assets/Scripts/InputManager.cs.meta +++ b/Assets/Scripts/InputManager.cs.meta @@ -4,7 +4,7 @@ MonoImporter: externalObjects: {} serializedVersion: 2 defaultReferences: [] - executionOrder: 0 + executionOrder: -5 icon: {instanceID: 0} userData: assetBundleName: diff --git a/Assets/Scripts/Mover.cs b/Assets/Scripts/Mover.cs index eae5821..e403f02 100644 --- a/Assets/Scripts/Mover.cs +++ b/Assets/Scripts/Mover.cs @@ -2,17 +2,31 @@ using System.Collections.Generic; using UnityEngine; -public class MovementManager : MonoBehaviour +public class Mover : MonoBehaviour { - // Start is called before the first frame update - void Start() + private AbstractCharacter _character; + private Transform _transform; + private Vector2 _direction; + private float _maxSpeed; + + public Vector2 Direction { - + get { return _direction; } + set { _direction = value; } } - // Update is called once per frame + void Start() + { + _transform = GetComponentInParent(); + _character = GetComponentInParent(); + _maxSpeed = _character.MaxSpeed; + _direction = Vector2.zero; + } + void Update() { - + _maxSpeed = _character.MaxSpeed; + Vector2 currentPosition = _transform.position; + _transform.position = currentPosition + _maxSpeed * Time.deltaTime * _direction; } } diff --git a/Assets/Scripts/PlayerCharacter.cs b/Assets/Scripts/PlayerCharacter.cs index cac3628..1908ae3 100644 --- a/Assets/Scripts/PlayerCharacter.cs +++ b/Assets/Scripts/PlayerCharacter.cs @@ -2,17 +2,19 @@ using System.Collections.Generic; using UnityEngine; -public class PlayerCharacter : MonoBehaviour +public class PlayerCharacter : AbstractCharacter { - // Start is called before the first frame update - void Start() + public override void Move(Vector2 movementDirection) { - + // TODO : Implement limits + Debug.Log("Direction:"); + Debug.Log(movementDirection); + _mover.Direction = movementDirection; } - // Update is called once per frame - void Update() + public override void Shoot() { - + // TODO : Implement + throw new System.NotImplementedException(); } } diff --git a/Assets/Scripts/PlayerCharacter.cs.meta b/Assets/Scripts/PlayerCharacter.cs.meta index fe3f9bc..b6f0087 100644 --- a/Assets/Scripts/PlayerCharacter.cs.meta +++ b/Assets/Scripts/PlayerCharacter.cs.meta @@ -4,7 +4,7 @@ MonoImporter: externalObjects: {} serializedVersion: 2 defaultReferences: [] - executionOrder: 0 + executionOrder: -1 icon: {instanceID: 0} userData: assetBundleName: