Fixed: unable to undock with an aetheric drive (game requires locomotion)
Entities have a chance of dropping any item on board when killed
Mine behavior generalized to grid objects, used in dropped loot
Added tractor beam (not itemized yet) which pulls in nearby loot (and mines!)
Items are added to player inventory when they collide with the ship (and mines explode!)
Added console command to spawn turret
This commit is contained in:
Reinier Vens 2021-04-15 04:25:22 +01:00
commit 11a55e236b
56 changed files with 6962 additions and 161 deletions

View file

@ -515,6 +515,27 @@ public class ActionGameManager : MonoBehaviour
}
});
ConsoleController.AddCommand("spawnturret",
_ =>
{
var nearestFaction = CurrentSector.Factions.MinBy(f => CurrentSector.HomeZones[f].Distance[Zone.SectorZone]);
var loadoutGenerator = new LoadoutGenerator(
ref ItemManager.Random,
ItemManager,
CurrentSector,
Zone.SectorZone,
nearestFaction,
.5f);
var turret = EntitySerializer.Unpack(ItemManager, Zone, loadoutGenerator.GenerateTurretLoadout(), true);
turret.Position.xz = _currentEntity.Position.xz +
ItemManager.Random.NextFloat2Direction() * ItemManager.Random.NextFloat(50, 500);
turret.Zone = Zone;
Zone.Entities.Add(turret);
turret.Activate();
});
ConsoleController.AddCommand("pingscene",
_ =>
{
@ -770,7 +791,7 @@ public class ActionGameManager : MonoBehaviour
Dialog.MoveToCursor();
AkSoundEngine.PostEvent("UI_Fail", gameObject);
}
else if (CurrentEntity.GetBehavior<Thruster>() == null)
else if (CurrentEntity.GetBehavior<Thruster>() == null && CurrentEntity.GetBehavior<AetherDrive>() == null)
{
Dialog.Clear();
Dialog.Title.text = "Can't undock. Missing thruster component!";
@ -1011,6 +1032,10 @@ public class ActionGameManager : MonoBehaviour
{
ship.MovementDirection = Input.Player.Move.ReadValue<Vector2>();
}
var tractorPower = Input.Player.TractorBeam.ReadValue<float>();
CurrentEntity.TractorPower =
saturate(CurrentEntity.TractorPower + sign(tractorPower - CurrentEntity.TractorPower) * Time.deltaTime * 2);
}
Zone.Update(Time.deltaTime);
}

View file

@ -7,6 +7,7 @@ using UnityEngine;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using float2 = Unity.Mathematics.float2;
using Random = UnityEngine.Random;
public class EntityInstance : MonoBehaviour
{
@ -60,6 +61,7 @@ public class EntityInstance : MonoBehaviour
public Transform LookAtPoint { get; private set; }
public Entity Entity { get; private set; }
public ZoneRenderer ZoneRenderer { get; private set; }
public Transform LocalSpace { get; private set; }
public bool Visible
{
get => _fade > 0.01f;
@ -67,6 +69,7 @@ public class EntityInstance : MonoBehaviour
private void Awake()
{
LocalSpace = new GameObject().transform;
var meshes = gameObject.GetComponentsInChildren<MeshRenderer>();
var materials = new List<(Material material, List<(MeshRenderer renderer, int index)> submeshes)>();
foreach (var mesh in meshes)
@ -150,6 +153,8 @@ public class EntityInstance : MonoBehaviour
public virtual void SetEntity(ZoneRenderer zoneRenderer, Entity entity)
{
gameObject.name = entity.Name;
LocalSpace.gameObject.name = $"{entity.Name} Sim Space";
LocalSpace.SetParent(transform.parent);
Entity = entity;
ZoneRenderer = zoneRenderer;
var hullData = entity.ItemManager.GetData(entity.Hull) as HullData;
@ -419,6 +424,27 @@ public class EntityInstance : MonoBehaviour
{
if (!this) return;
_destroyed = true;
foreach (var gear in Entity.Equipment)
{
if (Random.value < ZoneRenderer.Settings.LootDropProbability)
{
ZoneRenderer.DropItem(
Entity.Position,
Random.onUnitSphere * ZoneRenderer.Settings.LootDropVelocity,
gear.EquippableItem);
}
}
foreach (var cargo in Entity.CargoBays)
{
foreach (var item in cargo.Cargo.Keys)
{
ZoneRenderer.DropItem(
Entity.Position,
Random.onUnitSphere * ZoneRenderer.Settings.LootDropVelocity,
item);
}
}
if (DestroyEffect != null)
{
var t = Instantiate(DestroyEffect).transform;
@ -453,6 +479,7 @@ public class EntityInstance : MonoBehaviour
public virtual void Update()
{
LocalSpace.localPosition = transform.localPosition;
if (_currentPing.transform)
{
_currentPing.transform.localScale = _sensor.PingRadius * Vector3.one;
@ -531,15 +558,16 @@ public class EntityInstance : MonoBehaviour
public virtual void OnDestroy()
{
Destroy(LocalSpace.gameObject);
foreach (var r in _radiatorSfx)
{
AkSoundEngine.PostEvent(r.Key.Item.Data.SoundEffectTrigger + "_stop", r.Value);
AkSoundEngine.PostEvent($"{r.Key.Item.Data.SoundEffectTrigger}_stop", r.Value);
AkSoundEngine.UnregisterGameObj(r.Value);
}
if (_reactor.reactor != null)
{
AkSoundEngine.PostEvent(_reactor.reactor.Item.Data.SoundEffectTrigger + "_stop", _reactor.sfxSource);
AkSoundEngine.PostEvent($"{_reactor.reactor.Item.Data.SoundEffectTrigger}_stop", _reactor.sfxSource);
AkSoundEngine.UnregisterGameObj(_reactor.sfxSource);
}
foreach(var x in _subscriptions)

View file

@ -11,6 +11,9 @@ using UnityEngine;
public class GameSettings : ScriptableObject
{
public string StartingHullName = "Longinus";
public float PickupLifetime = 30;
public float LootDropProbability = .25f;
public float LootDropVelocity = 25;
public float HeatstrokePhasingFloor = 0;
public float HeatstrokePhasingFrequency = 5;
public float WormholeDistanceRatio;

View file

@ -0,0 +1,49 @@
using System;
using Unity.Mathematics;
using UnityEngine;
using static Unity.Mathematics.math;
using Random = UnityEngine.Random;
public class GridObject : MonoBehaviour
{
public float RotationSpeed;
public float GridOffset;
public float GridAttraction;
public float Gravity;
public float Drag = .1f;
public float LaunchDrag;
private float2 _selfVelocity;
private float _timeOffset;
public Zone Zone { get; set; }
public Vector3 Velocity { get; set; }
private void Start()
{
_timeOffset = Random.value * 100;
}
void Update()
{
if (Zone == null) return;
var t = transform;
var position = t.position;
var gridHeight = Zone.GetHeight(position.Flatland()) + GridOffset;
Velocity += Vector3.up * (sign(gridHeight - position.y) * GridAttraction * Time.deltaTime);
Velocity *= max(0, 1 - LaunchDrag * Time.deltaTime);
var normal = Zone.GetNormal(position.Flatland());
var force = new float2(normal.x, normal.z);
var forceMagnitude = lengthsq(force);
if (forceMagnitude > .001f)
{
var fa = 1 / (1 - forceMagnitude) - 1;
_selfVelocity += normalize(force) * Zone.Settings.GravityStrength * fa * Gravity;
}
_selfVelocity *= max(0, 1 - Drag * Time.deltaTime);
t.position = position + (Velocity + new Vector3(_selfVelocity.x, 0, _selfVelocity.y)) * Time.deltaTime;
t.localRotation = Quaternion.Euler(sin(Time.time - _timeOffset * RotationSpeed) * 90, 0, cos(Time.time - _timeOffset * RotationSpeed) * 90);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 737f47c158214f0995ba2835f040ed96
timeCreated: 1618441439

View file

@ -0,0 +1,6 @@
using UnityEngine;
public class ItemPickup : MonoBehaviour
{
public ItemInstance Item { get; set; }
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0c18f67d85af44c2b5afc8ecd15ce960
timeCreated: 1618445037

View file

@ -0,0 +1,41 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class ShieldAnimation : MonoBehaviour
{
public AnimationCurve Alpha;
public AnimationCurve Radius;
public AnimationCurve AlbedoGamma;
public AnimationCurve TextureGamma;
public AnimationCurve GradientMultiplier;
public float Duration;
private float _lerp;
private Material _material;
public Vector3 Direction
{
set { _material.SetVector("_Impact", value); }
}
void OnEnable()
{
_material = GetComponent<MeshRenderer>().material;
_material.SetFloat("_DitherSampleOffset", Random.value);
_lerp = 0;
}
void Update()
{
_lerp += Time.deltaTime / Duration;
if(_lerp > 1) GetComponent<Prototype>().ReturnToPool();
_material.SetFloat("_Alpha", Alpha.Evaluate(_lerp));
_material.SetFloat("_Radius", Radius.Evaluate(_lerp));
_material.SetFloat("_AlbedoGamma", AlbedoGamma.Evaluate(_lerp));
_material.SetFloat("_TextureGamma", TextureGamma.Evaluate(_lerp));
_material.SetFloat("_GradientMul", GradientMultiplier.Evaluate(_lerp));
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c959273c4a61f124abd3728581b4aa38
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,78 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using UnityEngine;
public class ShieldManager : MonoBehaviour
{
public Prototype ShieldPrototype;
public float CollisionHitDuration = 3;
public Entity Entity { get; set; }
private void OnCollisionEnter(Collision other)
{
var otherShield = other.collider.GetComponent<ShieldManager>();
if (!otherShield)
{
var gridObject = other.collider.GetComponent<GridObject>();
if (gridObject)
{
var itemPickup = other.collider.GetComponent<ItemPickup>();
var mine = other.collider.GetComponent<Mine>();
if (itemPickup)
{
if (Entity.CargoBays.Any(c => c.TryStore(itemPickup.Item)))
{
// TODO: Pickup notification!
Destroy(itemPickup.gameObject);
}
else
{
// TODO: Pickup failed notification!
var cp = other.GetContact(0);
gridObject.Velocity += cp.normal * 25;
Debug.Log("Attempted item pickup, but no space in cargo bay!");
}
}
else if (mine)
{
mine.Explode();
}
else
{
Debug.Log("Shield collision occurred with grid object, but other collider isn't a mine or an item pickup!");
}
}
else
Debug.Log("Shield collision occurred, but other collider isn't a shield or a grid object!");
return;
}
var contact = other.GetContact(0);
var normal = normalize(float2(contact.normal.x, contact.normal.z));
var tangent = normal.Rotate(ItemRotation.CounterClockwise);
var v1n = dot(normal, Entity.Velocity);
var v1t = dot(tangent, Entity.Velocity);
var v2n = dot(normal, otherShield.Entity.Velocity);
//var v2t = dot(tangent, otherShield.Entity.Velocity);
var v1np = PostCollisionVelocity(v1n, Entity.Mass, v2n, otherShield.Entity.Mass);
Entity.Velocity = tangent * v1t + normal * v1np;
if(Entity.Shield != null && Entity.Shield.Item.Active.Value) ShowHit(contact.point, CollisionHitDuration);
}
private float PostCollisionVelocity(float v1, float m1, float v2, float m2)
{
return (v1 * (m1 - m2) + m2 * v2) / (m1 + m2);
}
public void ShowHit(Vector3 point, float duration)
{
var shield = ShieldPrototype.Instantiate<ShieldAnimation>();
shield.Direction = normalize(shield.transform.InverseTransformPoint(point));
shield.Duration = duration;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a42e4b31178f74d43b17322b0ae8f178
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Mathematics;
using static Unity.Mathematics.math;
public class ShieldTest : MonoBehaviour
{
public float Acceleration;
private float _velocity;
// Update is called once per frame
void Update()
{
_velocity += -sign(transform.position.x) * Acceleration * Time.deltaTime;
transform.position += Vector3.right * (_velocity * Time.deltaTime);
}
private void OnCollisionEnter(Collision other)
{
_velocity = -_velocity;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 28d5bea988fad35428986b994b4cf42e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,9 +1,12 @@
using System;
using System.Collections;
using System.Linq;
using UnityEngine;
public class ShipInstance : EntityInstance
{
private static int _shipIndex;
public TractorBeam TractorBeam;
private class ThrusterInstance
{
public Thruster Thruster;
@ -17,6 +20,12 @@ public class ShipInstance : EntityInstance
public Ship Ship { get; private set; }
private void Start()
{
var tractorBeamMain = TractorBeam.ParticleSystem.main;
tractorBeamMain.customSimulationSpace = LocalSpace;
}
public override void SetEntity(ZoneRenderer zoneRenderer, Entity entity)
{
base.SetEntity(zoneRenderer, entity);
@ -79,6 +88,9 @@ public class ShipInstance : EntityInstance
public override void Update()
{
base.Update();
TractorBeam.Power = Entity.TractorPower;
TractorBeam.Direction = Entity.LookDirection;
foreach (var thrusterInstance in _thrusters)
{

View file

@ -0,0 +1,19 @@
using System;
using UnityEngine;
public class TimedDestroy : MonoBehaviour
{
public float Duration;
private float _startTime;
private void Start()
{
_startTime = Time.time;
}
public void Update()
{
if(Time.time - _startTime > Duration) Destroy(gameObject);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5d5539a23b814254b038a16f32f1d8a9
timeCreated: 1618453298

View file

@ -0,0 +1,33 @@
using System;
using UnityEngine;
public class TractorBeam : MonoBehaviour
{
public ParticleSystem ParticleSystem;
public float Radius;
public float Traction;
public float Distance;
public float Power { get; set; }
public Vector3 Direction { get; set; }
public void Update()
{
transform.rotation = Quaternion.LookRotation(Direction);
var emission = ParticleSystem.emission;
if (Power > .01f)
{
var objectFound = false;
foreach (var o in Physics.SphereCastAll(transform.position, Radius, Direction, Distance, 1 << 21))
{
var gridObject = o.collider.GetComponent<GridObject>();
if (gridObject == null) continue;
objectFound = true;
gridObject.Velocity += Vector3.Normalize(transform.position - gridObject.transform.position) * (Traction * Time.deltaTime);
}
emission.rateOverTimeMultiplier = objectFound ? Power : 0;
}
else emission.rateOverTimeMultiplier = 0;
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3f02211fcfb247b88dc54e58ad3b72d8
timeCreated: 1618450377

View file

@ -17,7 +17,6 @@ public class ConstantParticleWeapon : MonoBehaviour
private float _emission;
private List<ParticleSystem.Particle> _collisionParticles = new List<ParticleSystem.Particle>();
private HullCollider _hull;
private Transform _simulationSpace;
private void Start()
{
@ -33,12 +32,11 @@ public class ConstantParticleWeapon : MonoBehaviour
public void Initialize()
{
_simulationSpace = new GameObject($"{gameObject.name} Sim Space").transform;
foreach(var p in Particles)
{
var main = p.main;
main.simulationSpace = ParticleSystemSimulationSpace.Custom;
main.customSimulationSpace = _simulationSpace;
main.customSimulationSpace = Source.LocalSpace;
}
var trigger = Particles[0].trigger;
@ -57,11 +55,9 @@ public class ConstantParticleWeapon : MonoBehaviour
private void Update()
{
if (Source == null) return;
_simulationSpace.position = Source.transform.position;
if (_stopping && Particles.All(p=>p.particleCount == 0))
{
GetComponent<Prototype>().ReturnToPool();
Destroy(_simulationSpace.gameObject);
return;
}
}

View file

@ -8,6 +8,7 @@ using Random = UnityEngine.Random;
public class Mine : MonoBehaviour
{
public GridObject GridObject;
public MeshRenderer MeshRenderer;
public int EmissionSubmesh;
public AnimationCurve EmissionCurve;
@ -15,12 +16,6 @@ public class Mine : MonoBehaviour
public float ArmedCycleDuration;
public float ActiveEmission;
public float ArmedEmission;
public float RotationSpeed;
public float GridOffset;
public float GridAttraction;
public float Gravity;
public float Drag = .1f;
public float LaunchDrag;
public Prototype HitEffect;
public float Lifetime;
@ -28,7 +23,6 @@ public class Mine : MonoBehaviour
public float BlastRange;
public float BlastDelay;
private float2 _selfVelocity;
private float _startTime;
private float _blastTime;
private bool _blastCountdown;
@ -36,9 +30,6 @@ public class Mine : MonoBehaviour
private Material _material;
private float _pulseLerp;
private float _emission;
public Zone Zone { get; set; }
public Vector3 Velocity { get; set; }
public float Damage { get; set; }
public DamageType DamageType { get; set; }
public EntityInstance Source { get; set; }
@ -60,25 +51,8 @@ public class Mine : MonoBehaviour
// Update is called once per frame
void Update()
{
if (Zone == null) return;
var t = transform;
var position = t.position;
var gridHeight = Zone.GetHeight(position.Flatland()) + GridOffset;
Velocity += Vector3.up * (sign(gridHeight - position.y) * GridAttraction * Time.deltaTime);
Velocity *= max(0, 1 - LaunchDrag * Time.deltaTime);
var normal = Zone.GetNormal(position.Flatland());
var force = new float2(normal.x, normal.z);
var forceMagnitude = lengthsq(force);
if (forceMagnitude > .001f)
{
var fa = 1 / (1 - forceMagnitude) - 1;
_selfVelocity += normalize(force) * Zone.Settings.GravityStrength * fa * Gravity;
}
_selfVelocity *= max(0, 1 - Drag * Time.deltaTime);
t.position = position + (Velocity + new Vector3(_selfVelocity.x,0,_selfVelocity.y)) * Time.deltaTime;
t.localRotation = Quaternion.Euler(sin(Time.time-_startTime * RotationSpeed) * 90, 0, cos(Time.time-_startTime * RotationSpeed) * 90);
var position = transform.position;
if (Time.time - _startTime > ActivationDelay && !_active)
{
_active = true;
@ -91,7 +65,7 @@ public class Mine : MonoBehaviour
_pulseLerp %= 1;
if(!_blastCountdown)
{
foreach (var collider in Physics.OverlapSphere(t.position, BlastRange, 1))
foreach (var collider in Physics.OverlapSphere(position, BlastRange, 1))
{
var hull = collider.GetComponent<HullCollider>();
if (hull)
@ -106,26 +80,32 @@ public class Mine : MonoBehaviour
Time.time - _startTime > Lifetime ||
_blastCountdown && Time.time > _blastTime)
{
foreach (var collider in Physics.OverlapSphere(t.position, BlastRange, 1 | (1 << 17)))
{
var shield = collider.GetComponent<ShieldManager>();
if (shield && (shield.Entity.Shield != null && shield.Entity.Shield.Item.Active.Value && shield.Entity.Shield.CanTakeHit(DamageType, Damage)))
{
shield.Entity.Shield.TakeHit(DamageType, Damage);
shield.ShowHit(t.position, sqrt(Damage));
}
var hull = collider.GetComponent<HullCollider>();
if (hull && !(hull.Entity.Shield != null && hull.Entity.Shield.Item.Active.Value && hull.Entity.Shield.CanTakeHit(DamageType, Damage)))
{
hull.SendSplash(Damage, DamageType, Source.Entity, (collider.transform.position - t.position).normalized);
}
}
var ht = HitEffect.Instantiate<Transform>();
ht.position = t.position;
GetComponent<Prototype>().ReturnToPool();
Explode();
}
}
_material.SetFloat("_Emission", _emission * EmissionCurve.Evaluate(_pulseLerp));
}
public void Explode()
{
var position = transform.position;
foreach (var collider in Physics.OverlapSphere(position, BlastRange, 1 | (1 << 17)))
{
var shield = collider.GetComponent<ShieldManager>();
if (shield && (shield.Entity.Shield != null && shield.Entity.Shield.Item.Active.Value && shield.Entity.Shield.CanTakeHit(DamageType, Damage)))
{
shield.Entity.Shield.TakeHit(DamageType, Damage);
shield.ShowHit(position, sqrt(Damage));
}
var hull = collider.GetComponent<HullCollider>();
if (hull && !(hull.Entity.Shield != null && hull.Entity.Shield.Item.Active.Value && hull.Entity.Shield.CanTakeHit(DamageType, Damage)))
{
hull.SendSplash(Damage, DamageType, Source.Entity, (collider.transform.position - position).normalized);
}
}
var ht = HitEffect.Instantiate<Transform>();
ht.position = position;
GetComponent<Prototype>().ReturnToPool();
}
}

View file

@ -18,17 +18,17 @@ public class MineManager : InstantWeaponEffectManager
var angle = weapon.Spread / 2;
p.Source = source;
p.transform.position = barrel.position;
p.Velocity = Quaternion.Euler(
Random.Range(-angle, angle),
Random.Range(-angle, angle),
Random.Range(-angle, angle)) *
barrel.forward *
weapon.Velocity;
p.GridObject.Velocity = Quaternion.Euler(
Random.Range(-angle, angle),
Random.Range(-angle, angle),
Random.Range(-angle, angle)) *
barrel.forward *
weapon.Velocity;
if(InheritVelocity)
p.Velocity += new Vector3(source.Entity.Velocity.x, 0, source.Entity.Velocity.y);
p.GridObject.Velocity += new Vector3(source.Entity.Velocity.x, 0, source.Entity.Velocity.y);
p.Damage = weapon.Damage;
p.Range = weapon.Range;
p.DamageType = weapon.WeaponData.DamageType;
p.Zone = source.Entity.Zone;
p.GridObject.Zone = source.Entity.Zone;
}
}