Skip to content

Commit

Permalink
Settup State for turrets
Browse files Browse the repository at this point in the history
  • Loading branch information
Amatsugu committed Jun 29, 2020
1 parent 3bd2add commit 6594ed6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 81.90951, g: 183.89426, b: 589.8264, a: 1}
m_IndirectSpecularColor: {r: 84.56308, g: 188.70863, b: 609.30585, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -12866,7 +12866,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: -443}
m_AnchoredPosition: {x: 0, y: -443.00003}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!114 &1427279150
Expand Down Expand Up @@ -17247,7 +17247,7 @@ MonoBehaviour:
rTransform: {fileID: 0}
closeButton: {fileID: 0}
titleText: {fileID: 0}
hideOnStart: 0
hideOnStart: 1
hideOnBlur: 1
hideInEditor: 0
--- !u!1 &2037892364
Expand Down
75 changes: 68 additions & 7 deletions Assets/Scripts/Game Logic/Systems/Combat/TurretSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Transforms;

using UnityEngine;
using UnityEngine.AddressableAssets;

namespace Amatsugu.Phos.ECS
Expand All @@ -19,6 +24,11 @@ public class TurretSystem : ComponentSystem

private bool _isReady = false;
private ProjectileMeshEntity _bullet;
private BuildPhysicsWorld _physicsWorld;
private StepPhysicsWorld _simWorld;
private NativeList<int> _castHits;
private CollisionFilter _playerTargetingFilter;
private CollisionFilter _phosTargetingFilter;

protected override void OnCreate()
{
Expand All @@ -32,29 +42,80 @@ protected override void OnCreate()
_isReady = true;
}
};
_physicsWorld = World.GetExistingSystem<BuildPhysicsWorld>();
_simWorld = World.GetExistingSystem<StepPhysicsWorld>();
_castHits = new NativeList<int>(Allocator.Persistent);
_playerTargetingFilter = new CollisionFilter
{
BelongsTo = (1u << (int)Faction.Player),
CollidesWith = (1u << (int)Faction.Phos)
};
_phosTargetingFilter = new CollisionFilter
{
BelongsTo = (1u << (int)Faction.Phos),
CollidesWith = (1u << (int)Faction.Player)
};
}

protected override void OnUpdate()
{
if (!_isReady)
return;

Entities.ForEach((Entity e, ref Turret t, ref Translation pos, ref AttackSpeed speed, ref AttackRange range) =>
var world = _physicsWorld.PhysicsWorld;
_castHits.Clear();

//Verify Target
Entities.WithAll<Turret>().ForEach((Entity e, ref AttackTarget target) =>
{
});

//Aim
Entities.ForEach((Entity e, ref Turret t, ref Translation pos, ref AttackSpeed speed, ref AttackRange range, ref AttackTarget attackTarget) =>
{
var r = EntityManager.GetComponentData<Rotation>(t.Head).Value;
r = math.mul(math.normalizesafe(r), quaternion.AxisAngle(math.up(), 10 * Time.DeltaTime));
var tgtPos = EntityManager.GetComponentData<CenterOfMass>(attackTarget.Value);
var desR = quaternion.LookRotation(pos.Value - tgtPos.Value, math.up());
desR = Quaternion.RotateTowards(r, desR, 10 * Time.DeltaTime);
EntityManager.SetComponentData(t.Head, new Rotation
{
Value = r
Value = desR
});
});

var fwd = -math.rotate(r, new float3(0, 0, 1));
//Idle
Entities.WithAll<Turret>().ForEach((Entity e, ref Translation pos, ref AttackRange range, ref FactionId faction) =>
{
_physicsWorld.AABBCast(pos.Value, range.Value, faction.Value == Faction.Player ? _playerTargetingFilter : _phosTargetingFilter, ref _castHits);
for (int i = 0; i < _castHits.Length; i++)
{
var tgtE = _physicsWorld.PhysicsWorld.Bodies[_castHits[i]].Entity;
var tgtPos = EntityManager.GetComponentData<CenterOfMass>(tgtE).Value;
var distSq = math.lengthsq(pos.Value - tgtPos);
if(distSq < range.ValueSq)
{
PostUpdateCommands.AddComponent(e, new AttackTarget { Value = tgtE });
break;
}
}
});

var b = _bullet.BufferedInstantiate(PostUpdateCommands, pos.Value + fwd * 2f, 0.1f, fwd * 10);
//Shoot
Entities.ForEach((Entity e, ref Turret t, ref Translation pos, ref AttackSpeed speed, ref AttackRange range, ref AttackTarget attackTarget) =>
{
var r = EntityManager.GetComponentData<Rotation>(t.Head).Value;
var tgtPos = EntityManager.GetComponentData<CenterOfMass>(attackTarget.Value);
var dir = math.normalizesafe(pos.Value - tgtPos.Value);
var desR = quaternion.LookRotation(dir, math.up());
if (!r.value.Equals(desR))
return;
if (Time.ElapsedTime < speed.NextAttackTime)
return;
speed.NextAttackTime += speed.Value;
var b = _bullet.Instantiate(pos.Value + math.up() * 2, 0.5f, dir * 5);
PostUpdateCommands.AddComponent(b, new DeathTime { Value = Time.ElapsedTime + 5 });
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2020.1.0b12
m_EditorVersionWithRevision: 2020.1.0b12 (9e6726e6ce12)
m_EditorVersion: 2020.1.0b13
m_EditorVersionWithRevision: 2020.1.0b13 (3682d55b6e87)

0 comments on commit 6594ed6

Please sign in to comment.