micromissiles-unity/Assets/Scripts/Interceptors/Hydra70.cs

60 lines
2.1 KiB
C#
Raw Normal View History

2024-09-12 15:44:55 -07:00
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
2024-09-13 22:45:25 -07:00
public class Hydra70 : Missile {
private Vector3 _acceleration;
private bool _submunitionsLaunched = false;
protected override void Update() {
base.Update();
// Check if it's time to launch submunitions
if (!_submunitionsLaunched &&
(GetFlightPhase() == FlightPhase.MIDCOURSE || GetFlightPhase() == FlightPhase.BOOST) &&
SimManager.Instance.GetElapsedSimulationTime() >=
_agentConfig.submunitions_config.launch_config.launch_time) {
SpawnSubmunitions();
_submunitionsLaunched = true;
2024-09-12 15:44:55 -07:00
}
2024-09-13 22:45:25 -07:00
}
protected override void UpdateMidCourse(double deltaTime) {
Vector3 accelerationInput = Vector3.zero;
// Calculate and set the total acceleration
Vector3 acceleration = CalculateAcceleration(accelerationInput);
GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration);
_acceleration = acceleration;
}
protected override void DrawDebugVectors() {
base.DrawDebugVectors();
if (_acceleration != null) {
Debug.DrawRay(transform.position, _acceleration * 1f, Color.green);
2024-09-12 15:44:55 -07:00
}
2024-09-13 22:45:25 -07:00
}
public void SpawnSubmunitions() {
Debug.Log("Spawning submunitions");
// print the callstack
Debug.Log(new System.Diagnostics.StackTrace().ToString());
List<Missile> submunitions = new List<Missile>();
switch (_agentConfig.submunitions_config.agent_config.missile_type) {
case MissileType.MICROMISSILE:
for (int i = 0; i < _agentConfig.submunitions_config.num_submunitions; i++) {
AgentConfig convertedConfig =
AgentConfig.FromSubmunitionAgentConfig(_agentConfig.submunitions_config.agent_config);
convertedConfig.initial_state.position = transform.position;
convertedConfig.initial_state.velocity = GetComponent<Rigidbody>().velocity;
Missile submunition = SimManager.Instance.CreateMissile(convertedConfig);
submunitions.Add(submunition);
Debug.Log("Created submunition");
2024-09-12 15:44:55 -07:00
}
2024-09-13 22:45:25 -07:00
break;
2024-09-12 15:44:55 -07:00
}
2024-09-13 22:45:25 -07:00
SimManager.Instance.AssignMissilesToTargets(submunitions);
}
2024-09-12 15:44:55 -07:00
}