Commit 264da874 by alsunj

destroy the player instead of repositioning

parent f111cbb4
......@@ -4,7 +4,7 @@ using UnityEngine;
public class HitPointsAuthoring : MonoBehaviour
{
public int MaxHitPoints;
// public Vector3 HealthBarOffset;
public Vector3 HealthBarOffset;
public class HitPointsBaker : Baker<HitPointsAuthoring>
{
......@@ -15,7 +15,7 @@ public class HitPointsAuthoring : MonoBehaviour
AddComponent(entity, new MaxHitPoints { Value = authoring.MaxHitPoints });
AddBuffer<DamageBufferElement>(entity);
AddBuffer<DamageThisTick>(entity);
//AddComponent(entity, new HealthBarOffset { Value = authoring.HealthBarOffset });0
AddComponent(entity, new HealthBarOffset { Value = authoring.HealthBarOffset });
}
}
}
\ No newline at end of file
......@@ -15,7 +15,7 @@ public class EntitiesReferencesAuthoring : MonoBehaviour
{
public override void Bake(EntitiesReferencesAuthoring authoring)
{
Entity entity = GetEntity(TransformUsageFlags.Dynamic);
Entity entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new EntititesReferences
{
PlayerPrefabEntity = GetEntity(authoring.playerPrefabGameObject, TransformUsageFlags.Dynamic),
......@@ -23,15 +23,10 @@ public class EntitiesReferencesAuthoring : MonoBehaviour
SlimeEnemyEntity = GetEntity(authoring.SlimeEnemyGameObject, TransformUsageFlags.Dynamic),
RespawnEntity = GetEntity(authoring.RespawnEntity, TransformUsageFlags.None)
});
AddComponentObject(entity, new UIPrefabs
{
PlayerHealthUIEntity = authoring.HealthBarPrefab
});
}
}
}
public struct EntititesReferences : IComponentData
{
public Entity PlayerPrefabEntity;
public Entity RougeEnemyEntity;
public Entity SlimeEnemyEntity;
public Entity RespawnEntity;
}
\ No newline at end of file
fileFormatVersion: 2
guid: 54055e963725d3946b0f8ecd5aff2954
guid: 2bc8e0b896da57345a538ac0f5f3b6a2
folderAsset: yes
DefaultImporter:
externalObjects: {}
......
using Unity.Entities;
using UnityEngine;
public struct EntititesReferences : IComponentData
{
public Entity PlayerPrefabEntity;
public Entity RougeEnemyEntity;
public Entity SlimeEnemyEntity;
public Entity RespawnEntity;
}
public class UIPrefabs : IComponentData
{
public GameObject PlayerHealthUIEntity;
}
\ No newline at end of file
fileFormatVersion: 2
guid: c212b1d1bc3a33f46b6d0a1916775ff0
\ No newline at end of file
......@@ -27,4 +27,14 @@ public struct PlayerTag : IComponentData
public struct TeamTypes : IComponentData
{
[GhostField] public TeamType Value;
}
public class HealthBarUIReference : ICleanupComponentData
{
public GameObject Value;
}
public struct HealthBarOffset : IComponentData
{
public float3 Value;
}
\ No newline at end of file
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;
using UnityEngine.UI;
[UpdateAfter(typeof(TransformSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct HealthBarSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<EndSimulationEntityCommandBufferSystem.Singleton>();
state.RequireForUpdate<UIPrefabs>();
}
public void OnUpdate(ref SystemState state)
{
var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);
foreach (var (transform, healthBarOffset, maxHitPoints, entity) in SystemAPI
.Query<LocalTransform, HealthBarOffset, MaxHitPoints>().WithNone<HealthBarUIReference>()
.WithEntityAccess())
{
var healthBarPrefab = SystemAPI.ManagedAPI.GetSingleton<UIPrefabs>().PlayerHealthUIEntity;
var spawnPosition = transform.Position + healthBarOffset.Value;
var newHealthBar = Object.Instantiate(healthBarPrefab, spawnPosition, Quaternion.identity);
SetHealthBar(newHealthBar, maxHitPoints.Value, maxHitPoints.Value);
ecb.AddComponent(entity, new HealthBarUIReference { Value = newHealthBar });
}
// Update position and values of health bar
foreach (var (transform, healthBarOffset, currentHitPoints, maxHitPoints, healthBarUI) in SystemAPI
.Query<LocalTransform, HealthBarOffset, CurrentHitPoints, MaxHitPoints, HealthBarUIReference>())
{
var healthBarPosition = transform.Position + healthBarOffset.Value;
healthBarUI.Value.transform.position = healthBarPosition;
SetHealthBar(healthBarUI.Value, currentHitPoints.Value, maxHitPoints.Value);
}
// Cleanup health bar once associated entity is destroyed
foreach (var (healthBarUI, entity) in SystemAPI.Query<HealthBarUIReference>().WithNone<LocalTransform>()
.WithEntityAccess())
{
Object.Destroy(healthBarUI.Value);
ecb.RemoveComponent<HealthBarUIReference>(entity);
}
}
private void SetHealthBar(GameObject healthBarCanvasObject, int curHitPoints, int maxHitPoints)
{
var healthBarSlider = healthBarCanvasObject.GetComponentInChildren<Slider>();
healthBarSlider.minValue = 0;
healthBarSlider.maxValue = maxHitPoints;
healthBarSlider.value = curHitPoints;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 23d3a8759edba9248b7506c0f3009f02
\ No newline at end of file
......@@ -102,7 +102,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2de495c310a322143be08aa7819a1a33, type: 3}
m_Name:
m_EditorClassIdentifier:
DamageOnTrigger: 100
DamageOnTrigger: 50
--- !u!114 &-8527907258641966621
MonoBehaviour:
m_ObjectHideFlags: 0
......
......@@ -24,7 +24,7 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1001200712104652852}
m_LocalRotation: {x: 0.5, y: 0, z: 0, w: 0.8660254}
m_LocalRotation: {x: 0.17364816, y: 0, z: 0, w: 0.9848078}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
......@@ -34,11 +34,11 @@ RectTransform:
- {fileID: 1049164032731023949}
- {fileID: 6194065087393849582}
m_Father: {fileID: 8603131648779679062}
m_LocalEulerAnglesHint: {x: 60, y: 0, z: 0}
m_LocalEulerAnglesHint: {x: 20, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 200, y: 75}
m_SizeDelta: {x: 120, y: 40}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &8986874467296708868
MonoBehaviour:
......@@ -239,7 +239,7 @@ RectTransform:
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 3}
m_SizeDelta: {x: 1000, y: 1000}
m_SizeDelta: {x: 4, y: 1000}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!223 &7587403821176963594
Canvas:
......
......@@ -320,7 +320,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 8e73c985659b81d408f4e99cb152349d, type: 3}
m_Name:
m_EditorClassIdentifier:
MaxHitPoints: 500
MaxHitPoints: 1000
HealthBarOffset: {x: 0, y: 2.5, z: 0}
--- !u!1001 &6975352639711469968
PrefabInstance:
m_ObjectHideFlags: 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment