//public Animator doorAnimator;
    //public float doorPosition;

    void Awake()
    {
        aircraft = GetComponent <AIAircraftSpawn>();
        pilot    = GetComponent <AIPilot>();
        rb       = GetComponent <Rigidbody>();
        //doorAnimator = GetComponentInChildren<Animator>();

        gearAnimator = GetComponentInChildren <GearAnimator>();
        tailHook     = GetComponentInChildren <Tailhook>();
        catHook      = GetComponentInChildren <CatapultHook>();
        refuelPort   = GetComponentInChildren <RefuelPort>();
        wingRotator  = pilot.wingRotator;

        kPlane   = GetComponent <KinematicPlane>();
        fuelTank = GetComponent <FuelTank>();

        waypoint = new Waypoint();
        GameObject waypointObject = new GameObject();

        waypointObject.AddComponent <FloatingOriginTransform>();
        waypoint.SetTransform(waypointObject.transform);

        tilter = GetComponent <TiltController>();

        normalSpeed = pilot.navSpeed;

        health = GetComponent <Health>();
        health.OnDeath.AddListener(OnDeath);
    }
Пример #2
0
    public static GameObject SpawnRepresentation(ulong networkID, Vector3D position, Quaternion rotation, bool isLeft)
    {
        if (networkID == localUID)
        {
            return(null);
        }

        int playerID = FindPlayerIDFromNetworkUID(networkID);

        if (playerID == -1)
        {
            Debug.LogError("Spawn Representation couldn't find a player id.");
        }
        Player player = players[playerID];

        if (player.vehicle != null)
        {
            GameObject.Destroy(player.vehicle);
        }

        GameObject newVehicle = null;

        switch (player.vehicleType)
        {
        case VTOLVehicles.None:
            Debug.LogError("Vehcile Enum seems to be none, couldn't spawn player vehicle");
            return(null);

        case VTOLVehicles.AV42C:
            if (null == av42cPrefab)
            {
                SetPrefabs();
            }
            newVehicle = GameObject.Instantiate(av42cPrefab, VTMapManager.GlobalToWorldPoint(position), rotation);
            break;

        case VTOLVehicles.FA26B:
            if (null == fa26bPrefab)
            {
                SetPrefabs();
            }
            newVehicle = GameObject.Instantiate(fa26bPrefab, VTMapManager.GlobalToWorldPoint(position), rotation);
            break;

        case VTOLVehicles.F45A:
            if (null == f45Prefab)
            {
                SetPrefabs();
            }
            newVehicle = GameObject.Instantiate(f45Prefab, VTMapManager.GlobalToWorldPoint(position), rotation);
            break;
        }
        //Debug.Log("Setting vehicle name");
        newVehicle.name = $"Client [{player.cSteamID}]";
        Debug.Log($"Spawned new vehicle at {newVehicle.transform.position}");
        if (Networker.isHost)
        {
            HealthNetworker_Receiver healthNetworker = newVehicle.AddComponent <HealthNetworker_Receiver>();
            healthNetworker.networkUID = networkID;
        }
        else
        {
            HealthNetworker_ReceiverHostEnforced healthNetworker = newVehicle.AddComponent <HealthNetworker_ReceiverHostEnforced>();
            healthNetworker.networkUID = networkID;
        }
        RigidbodyNetworker_Receiver rbNetworker = newVehicle.AddComponent <RigidbodyNetworker_Receiver>();

        rbNetworker.networkUID = networkID;

        PlaneNetworker_Receiver planeReceiver = newVehicle.AddComponent <PlaneNetworker_Receiver>();

        planeReceiver.networkUID = networkID;

        if (player.vehicleType == VTOLVehicles.AV42C || player.vehicleType == VTOLVehicles.F45A)
        {
            //Debug.Log("Adding Tilt Controller to this vehicle " + message.networkID);
            EngineTiltNetworker_Receiver tiltReceiver = newVehicle.AddComponent <EngineTiltNetworker_Receiver>();
            tiltReceiver.networkUID = networkID;
        }

        Rigidbody rb      = newVehicle.GetComponent <Rigidbody>();
        AIPilot   aIPilot = newVehicle.GetComponent <AIPilot>();

        RotationToggle wingRotator = aIPilot.wingRotator;

        if (wingRotator != null)
        {
            WingFoldNetworker_Receiver wingFoldReceiver = newVehicle.AddComponent <WingFoldNetworker_Receiver>();
            wingFoldReceiver.networkUID     = networkID;
            wingFoldReceiver.wingController = wingRotator;
        }

        LockingRadar lockingRadar = newVehicle.GetComponentInChildren <LockingRadar>();

        if (lockingRadar != null)
        {
            Debug.Log($"Adding LockingRadarReciever to vehicle {newVehicle.name}");
            LockingRadarNetworker_Receiver lockingRadarReceiver = newVehicle.AddComponent <LockingRadarNetworker_Receiver>();
            lockingRadarReceiver.networkUID = networkID;
        }

        ExteriorLightsController extLight = newVehicle.GetComponentInChildren <ExteriorLightsController>();

        if (extLight != null)
        {
            ExtLight_Receiver extLightReceiver = newVehicle.AddComponent <ExtLight_Receiver>();
            extLightReceiver.lightsController = extLight;
            extLightReceiver.networkUID       = networkID;
        }

        aIPilot.enabled = false;
        Debug.Log($"Changing {newVehicle.name}'s position and rotation\nPos:{rb.position} Rotation:{rb.rotation.eulerAngles}");
        aIPilot.kPlane.SetToKinematic();
        aIPilot.kPlane.enabled = false;
        rb.interpolation       = RigidbodyInterpolation.None;

        aIPilot.kPlane.enabled = true;
        aIPilot.kPlane.SetVelocity(Vector3.zero);
        aIPilot.kPlane.SetToDynamic();
        rb.interpolation = RigidbodyInterpolation.Interpolate;

        Debug.Log($"Finished changing {newVehicle.name}\n Pos:{rb.position} Rotation:{rb.rotation.eulerAngles}");

        AvatarManager.SetupAircraftRoundels(newVehicle.transform, player.vehicleType, player.cSteamID, Vector3.zero);

        GameObject parent  = new GameObject("Name Tag Holder");
        GameObject nameTag = new GameObject("Name Tag");

        parent.transform.SetParent(newVehicle.transform);
        parent.transform.localRotation = Quaternion.Euler(0, 180, 0);
        nameTag.transform.SetParent(parent.transform);
        nameTag.AddComponent <Nametag>().SetText(
            SteamFriends.GetFriendPersonaName(player.cSteamID),
            newVehicle.transform, VRHead.instance.transform);
        if (isLeft != PlayerManager.teamLeftie)
        {
            aIPilot.actor.team = Teams.Enemy;
        }

        TargetManager.instance.RegisterActor(aIPilot.actor);
        player.leftie  = isLeft;
        player.vehicle = newVehicle;

        if (!VTOLVR_Multiplayer.AIDictionaries.allActors.ContainsKey(networkID))
        {
            VTOLVR_Multiplayer.AIDictionaries.allActors[networkID]            = aIPilot.actor;
            VTOLVR_Multiplayer.AIDictionaries.reverseAllActors[aIPilot.actor] = networkID;
        }
        else
        {
            VTOLVR_Multiplayer.AIDictionaries.allActors.Remove(networkID);
            VTOLVR_Multiplayer.AIDictionaries.reverseAllActors.Remove(aIPilot.actor);

            VTOLVR_Multiplayer.AIDictionaries.allActors[networkID]            = aIPilot.actor;
            VTOLVR_Multiplayer.AIDictionaries.reverseAllActors[aIPilot.actor] = networkID;
        }

        return(newVehicle);
    }
Пример #3
0
    /// <summary>
    /// This is used by the client and only the client to spawn ai vehicles.
    /// </summary>
    public static void SpawnAIVehicle(Packet packet) // This should never run on the host
    {
        if (Networker.isHost)
        {
            Debug.LogWarning("Host shouldn't be trying to spawn an ai vehicle.");
            return;
        }
        Message_SpawnAIVehicle message = (Message_SpawnAIVehicle)((PacketSingle)packet).message;

        if (!PlayerManager.gameLoaded)
        {
            Debug.LogWarning("Our game isn't loaded, adding spawn vehicle to queue");
            AIsToSpawnQueue.Enqueue(packet);
            return;
        }
        foreach (ulong id in spawnedAI)
        {
            if (id == message.rootActorNetworkID)
            {
                Debug.Log("Got a spawnAI message for a vehicle we have already added! Name == " + message.unitName + " Returning...");
                return;
            }
        }

        spawnedAI.Add(message.rootActorNetworkID);
        //Debug.Log("Got a new aiSpawn uID.");
        if (message.unitName == "Player")
        {
            Debug.LogWarning("Player shouldn't be sent to someones client....");
            return;
        }
        Debug.Log("Trying to spawn AI " + message.aiVehicleName);

        GameObject prefab = UnitCatalogue.GetUnitPrefab(message.unitName);

        if (prefab == null)
        {
            Debug.LogError(message.unitName + " was not found.");
            return;
        }
        GameObject newAI = GameObject.Instantiate(prefab, VTMapManager.GlobalToWorldPoint(message.position), message.rotation);

        //Debug.Log("Setting vehicle name");
        newAI.name = message.aiVehicleName;
        Actor actor = newAI.GetComponent <Actor>();

        if (actor == null)
        {
            Debug.LogError("actor is null on object " + newAI.name);
        }

        if (message.redfor)
        {
            actor.team = Teams.Enemy;
        }
        else
        {
            actor.team = Teams.Allied;
        }

        AirportManager airport = newAI.GetComponent <AirportManager>();

        UnitSpawn unitSP = newAI.GetComponent <UnitSpawn>();

        GameObject.Destroy(unitSP);
        if (airport != null)
        {
            newAI.AddComponent <UnitSpawn>();
        }
        else
        {
            newAI.AddComponent <AICarrierSpawn>();
        }

        unitSP = newAI.GetComponent <UnitSpawn>();

        UnitSpawner UnitSpawner = new UnitSpawner();

        actor.unitSpawn             = unitSP;
        actor.unitSpawn.unitSpawner = UnitSpawner;
        unitSP.actor = actor;
        Traverse.Create(actor.unitSpawn.unitSpawner).Field("_spawnedUnit").SetValue(unitSP);
        Traverse.Create(actor.unitSpawn.unitSpawner).Field("_spawned").SetValue(true);
        Traverse.Create(actor.unitSpawn.unitSpawner).Field("_unitInstanceID").SetValue(message.unitInstanceID); // To make objectives work.
        UnitSpawner.team     = actor.team;
        UnitSpawner.unitName = actor.unitSpawn.unitName;

        if (!PlayerManager.teamLeftie)
        {
            UnitSpawner.team = actor.team;
        }
        else
        {
            if (actor.team == Teams.Enemy)
            {
                foreach (Actor subActor in newAI.GetComponentsInChildren <Actor>())
                {
                    subActor.team = Teams.Allied;
                    TargetManager.instance.UnregisterActor(subActor);
                    TargetManager.instance.RegisterActor(subActor);
                }
            }
            else
            if (actor.team == Teams.Allied)
            {
                foreach (Actor subActor in newAI.GetComponentsInChildren <Actor>())
                {
                    subActor.team = Teams.Enemy;
                    TargetManager.instance.UnregisterActor(subActor);
                    TargetManager.instance.RegisterActor(subActor);
                }
            }
            UnitSpawner.team = actor.team;


            if (airport != null)
            {
                airport.team = actor.team;
                SetUpCarrier(newAI, message.rootActorNetworkID, actor.team);
            }
        }

        TargetManager.instance.UnregisterActor(actor);
        TargetManager.instance.RegisterActor(actor);
        VTScenario.current.units.AddSpawner(actor.unitSpawn.unitSpawner);

        if (message.hasGroup)
        {
            VTScenario.current.groups.AddUnitToGroup(UnitSpawner, message.unitGroup);
        }
        Debug.Log(actor.name + $" has had its unitInstanceID set at value {actor.unitSpawn.unitSpawner.unitInstanceID}.");
        VTScenario.current.units.AddSpawner(actor.unitSpawn.unitSpawner);
        Debug.Log($"Spawned new vehicle at {newAI.transform.position}");

        newAI.AddComponent <FloatingOriginTransform>();

        newAI.transform.position = VTMapManager.GlobalToWorldPoint(message.position);
        newAI.transform.rotation = message.rotation;

        Debug.Log("This unit should have " + message.networkIDs.Length + " actors! ");

        int currentSubActorID = 0;

        foreach (Actor child in newAI.GetComponentsInChildren <Actor>())
        {
            Debug.Log("setting up actor: " + currentSubActorID);
            UIDNetworker_Receiver uidReciever = child.gameObject.AddComponent <UIDNetworker_Receiver>();
            uidReciever.networkUID = message.networkIDs[currentSubActorID];

            if (child.gameObject.GetComponent <Health>() != null)
            {
                HealthNetworker_Receiver healthNetworker = child.gameObject.AddComponent <HealthNetworker_Receiver>();
                healthNetworker.networkUID = message.networkIDs[currentSubActorID];
                //HealthNetworker_Sender healthNetworkerS = newAI.AddComponent<HealthNetworker_Sender>();
                //healthNetworkerS.networkUID = message.networkID;
                // Debug.Log("added health Sender to ai");
                // Debug.Log("added health reciever to ai");
            }
            else
            {
                Debug.Log(message.aiVehicleName + " has no health?");
            }

            if (child.gameObject.GetComponent <ShipMover>() != null)
            {
                ShipNetworker_Receiver shipNetworker = child.gameObject.AddComponent <ShipNetworker_Receiver>();
                shipNetworker.networkUID = message.networkIDs[currentSubActorID];
            }
            else if (child.gameObject.GetComponent <GroundUnitMover>() != null)
            {
                if (child.gameObject.GetComponent <Rigidbody>() != null)
                {
                    GroundNetworker_Receiver groundNetworker = child.gameObject.AddComponent <GroundNetworker_Receiver>();
                    groundNetworker.networkUID = message.networkIDs[currentSubActorID];
                }
            }
            else if (child.gameObject.GetComponent <Rigidbody>() != null)
            {
                Rigidbody rb = child.gameObject.GetComponent <Rigidbody>();
                RigidbodyNetworker_Receiver rbNetworker = child.gameObject.AddComponent <RigidbodyNetworker_Receiver>();
                rbNetworker.networkUID = message.networkIDs[currentSubActorID];
            }
            if (child.role == Actor.Roles.Air)
            {
                PlaneNetworker_Receiver planeReceiver = child.gameObject.AddComponent <PlaneNetworker_Receiver>();
                planeReceiver.networkUID = message.networkIDs[currentSubActorID];
                AIPilot aIPilot = child.gameObject.GetComponent <AIPilot>();
                aIPilot.enabled = false;
                aIPilot.kPlane.SetToKinematic();
                aIPilot.kPlane.enabled = false;
                aIPilot.commandState   = AIPilot.CommandStates.Navigation;
                aIPilot.kPlane.enabled = true;
                aIPilot.kPlane.SetVelocity(Vector3.zero);
                aIPilot.kPlane.SetToDynamic();

                RotationToggle wingRotator = aIPilot.wingRotator;
                if (wingRotator != null)
                {
                    WingFoldNetworker_Receiver wingFoldReceiver = child.gameObject.AddComponent <WingFoldNetworker_Receiver>();
                    wingFoldReceiver.networkUID     = message.networkIDs[currentSubActorID];
                    wingFoldReceiver.wingController = wingRotator;
                }
                if (aIPilot.isVtol)
                {
                    //Debug.Log("Adding Tilt Controller to this vehicle " + message.networkID);
                    EngineTiltNetworker_Receiver tiltReceiver = child.gameObject.AddComponent <EngineTiltNetworker_Receiver>();
                    tiltReceiver.networkUID = message.networkIDs[currentSubActorID];
                }

                if (child.gameObject.GetComponentInChildren <ExteriorLightsController>() != null)
                {
                    ExtLight_Receiver extLight = child.gameObject.AddComponent <ExtLight_Receiver>();
                    extLight.networkUID = message.networkIDs[currentSubActorID];
                }

                Rigidbody rb = child.gameObject.GetComponent <Rigidbody>();

                foreach (Collider collider in child.gameObject.GetComponentsInChildren <Collider>())
                {
                    if (collider)
                    {
                        collider.gameObject.layer = 9;
                    }
                }

                Debug.Log("Doing weapon manager shit on " + child.gameObject.name + ".");
                WeaponManager weaponManager = child.gameObject.GetComponent <WeaponManager>();
                if (weaponManager == null)
                {
                    Debug.LogError(child.gameObject.name + " does not seem to have a weapon maanger on it.");
                }
                else
                {
                    PlaneEquippableManager.SetLoadout(child.gameObject, message.networkIDs[currentSubActorID], message.normalizedFuel, message.hpLoadout, message.cmLoadout);
                }
            }

            AIUnitSpawn aIUnitSpawn = child.gameObject.GetComponent <AIUnitSpawn>();
            if (aIUnitSpawn == null)
            {
                Debug.LogWarning("AI unit spawn is null on respawned unit " + aIUnitSpawn);
            }
            // else
            // newAI.GetComponent<AIUnitSpawn>().SetEngageEnemies(message.Aggresive);
            VehicleMover vehicleMover = child.gameObject.GetComponent <VehicleMover>();
            if (vehicleMover != null)
            {
                vehicleMover.enabled  = false;
                vehicleMover.behavior = GroundUnitMover.Behaviors.Parked;
            }
            else
            {
                GroundUnitMover ground = child.gameObject.GetComponent <GroundUnitMover>();
                if (ground != null)
                {
                    ground.enabled  = false;
                    ground.behavior = GroundUnitMover.Behaviors.Parked;
                }
            }

            Debug.Log("Checking for gun turrets on child " + child.name);
            if (child.gameObject.GetComponentsInChildren <Actor>().Length <= 1)
            {//only run this code on units without subunits
                Debug.Log("This is a child, with " + child.gameObject.GetComponentsInChildren <Actor>().Length + " actors, so it could have guns!");
                ulong turretCount = 0;
                foreach (ModuleTurret moduleTurret in child.gameObject.GetComponentsInChildren <ModuleTurret>())
                {
                    TurretNetworker_Receiver tRec = child.gameObject.AddComponent <TurretNetworker_Receiver>();
                    tRec.networkUID = message.networkIDs[currentSubActorID];
                    tRec.turretID   = turretCount;
                    Debug.Log("Added turret " + turretCount + " to actor " + message.networkIDs[currentSubActorID] + " uid");
                    turretCount++;
                }
                ulong gunCount = 0;
                foreach (GunTurretAI turretAI in child.gameObject.GetComponentsInChildren <GunTurretAI>())
                {
                    turretAI.SetEngageEnemies(false);
                    AAANetworker_Reciever aaaRec = child.gameObject.AddComponent <AAANetworker_Reciever>();
                    aaaRec.networkUID = message.networkIDs[currentSubActorID];
                    aaaRec.gunID      = gunCount;
                    Debug.Log("Added gun " + gunCount + " to actor " + message.networkIDs[currentSubActorID] + " uid");
                    gunCount++;
                }
            }
            else
            {
                Debug.Log("This isnt a child leaf thing, it has " + child.gameObject.GetComponentsInChildren <Actor>().Length + " actors");
            }
            IRSamLauncher iLauncher = child.gameObject.GetComponent <IRSamLauncher>();
            if (iLauncher != null)
            {
                //iLauncher.ml.RemoveAllMissiles();
                iLauncher.ml.LoadAllMissiles();
                iLauncher.SetEngageEnemies(false);
                MissileNetworker_Receiver mlr;
                //iLauncher.ml.LoadCount(message.IRSamMissiles.Length);
                Debug.Log($"Adding IR id's on IR SAM, len = {message.IRSamMissiles.Length}.");
                for (int i = 0; i < message.IRSamMissiles.Length; i++)
                {
                    mlr            = iLauncher.ml.missiles[i]?.gameObject.AddComponent <MissileNetworker_Receiver>();
                    mlr.thisML     = iLauncher.ml;
                    mlr.networkUID = message.IRSamMissiles[i];
                }
                Debug.Log("Added IR id's.");
            }
            Soldier soldier = child.gameObject.GetComponent <Soldier>();
            if (soldier != null)
            {
                soldier.SetEngageEnemies(false);
                if (soldier.soldierType == Soldier.SoldierTypes.IRMANPAD)
                {
                    soldier.SetEngageEnemies(false);
                    IRMissileLauncher ir = soldier.irMissileLauncher;
                    if (ir != null)
                    {
                        //ir.RemoveAllMissiles();
                        ir.LoadAllMissiles();
                        MissileNetworker_Receiver mlr;
                        //ir.LoadCount(message.IRSamMissiles.Length);
                        Debug.Log($"Adding IR id's on manpads, len = {message.IRSamMissiles.Length}.");
                        for (int i = 0; i < message.IRSamMissiles.Length; i++)
                        {
                            mlr            = ir.missiles[i]?.gameObject.AddComponent <MissileNetworker_Receiver>();
                            mlr.thisML     = ir;
                            mlr.networkUID = message.IRSamMissiles[i];
                        }
                        Debug.Log("Added IR id's on manpads.");
                    }
                    else
                    {
                        Debug.Log($"Manpad {message.networkIDs} forgot its rocket launcher pepega.");
                    }
                }
            }

            Debug.Log("Checking for SAM launchers");
            SAMLauncher launcher = child.gameObject.GetComponent <SAMLauncher>();
            if (launcher != null)
            {
                Debug.Log("I found a sam launcher!");
                SamNetworker_Reciever samNetworker = launcher.gameObject.AddComponent <SamNetworker_Reciever>();
                samNetworker.networkUID = message.networkIDs[currentSubActorID];
                samNetworker.radarUIDS  = message.radarIDs;
                //Debug.Log($"Added samNetworker to uID {message.networkID}.");
                launcher.SetEngageEnemies(false);
                launcher.fireInterval  = float.MaxValue;
                launcher.lockingRadars = null;
            }

            /*IRSamLauncher ml = actor.gameObject.GetComponentInChildren<IRSamLauncher>();
             * if (ml != null)
             * {
             *  ml.SetEngageEnemies(false);
             *  MissileNetworker_Receiver lastRec;
             *  for (int i = 0; i < ml.ml.missiles.Length; i++)
             *  {
             *      lastRec = ml.ml.missiles[i].gameObject.AddComponent<MissileNetworker_Receiver>();
             *      lastRec.networkUID = message.IRSamMissiles[i];
             *      lastRec.thisML = ml.ml;
             *  }
             * }*/
            //this code for ir missiles was here twice, so i dissable the seccond copy

            Debug.Log("Checking for locking radars");
            foreach (LockingRadar radar in child.GetComponentsInChildren <LockingRadar>())
            {
                if (radar.GetComponent <Actor>() == child)
                {
                    Debug.Log($"Adding radar receiver to object {child.name} as it is the same game object as this actor.");
                    LockingRadarNetworker_Receiver lastLockingReceiver = child.gameObject.AddComponent <LockingRadarNetworker_Receiver>();
                    lastLockingReceiver.networkUID = message.networkIDs[currentSubActorID];
                    Debug.Log("Added locking radar!");
                }
                else if (radar.GetComponentInParent <Actor>() == child)
                {
                    Debug.Log($"Adding radar receiver to object {child.name} as it is a child of this actor.");
                    LockingRadarNetworker_Receiver lastLockingReceiver = child.gameObject.AddComponent <LockingRadarNetworker_Receiver>();
                    lastLockingReceiver.networkUID = message.networkIDs[currentSubActorID];
                    Debug.Log("Added locking radar!");
                }
                else
                {
                    Debug.Log("This radar is not direct child of this actor, ignoring");
                }
            }
            AIVehicles.Add(new AI(child.gameObject, message.aiVehicleName, child, message.networkIDs[currentSubActorID]));
            Debug.Log("Spawned in AI " + child.gameObject.name);

            if (!VTOLVR_Multiplayer.AIDictionaries.allActors.ContainsKey(message.networkIDs[currentSubActorID]))
            {
                VTOLVR_Multiplayer.AIDictionaries.allActors.Add(message.networkIDs[currentSubActorID], child);
            }
            if (!VTOLVR_Multiplayer.AIDictionaries.reverseAllActors.ContainsKey(actor))
            {
                VTOLVR_Multiplayer.AIDictionaries.reverseAllActors.Add(child, message.networkIDs[currentSubActorID]);
            }

            currentSubActorID++;
        }
    }