示例#1
0
    public GameSimulation()
    {
        activePlayers      = new List <Player>();
        teamNameToModelMap = new Dictionary <string, int>();
        teams                = new Dictionary <string, List <TankController> >();
        tankFactory          = new TankFactory();
        enqueuedCommands     = new Queue <GameCommand>();
        tankControllers      = new List <TankController>();
        allObjects           = new List <GameObjectState>();
        objectsInFieldOfView = new Dictionary <string, List <GameObjectState> >();
        tanksToBeRemoved     = new List <TankController>();

        healthPickups         = new List <GameObject>();
        ammoPickups           = new List <GameObject>();
        healthPickupsToRemove = new List <GameObject>();
        ammoPickupsToRemove   = new List <GameObject>();

        ammoPickupCount   = ConfigValueStore.GetIntValue("ammo_packs_active");
        healthPickupCount = ConfigValueStore.GetIntValue("health_packs_active");

        //event setup
        EventManager.Initialise();


        EventManager.clientDisconnect.AddListener(x =>
        {
            Debug.Log("Client disconnected, removing tank");

            lock (tankDisconnects)
            {
                tankDisconnects.Add(x.Token);
            }
        });
    }
示例#2
0
    // Use this for initialization
    public virtual void Start()
    {
        root        = this.gameObject;
        turret      = root.transform.Find("top").gameObject;
        barrel      = turret.transform.Find("barrel").gameObject;
        firingPoint = barrel.transform.Find("firingpoint").gameObject;

        uiLabel = Instantiate(Resources.Load("Prefabs/TextLabel")) as UnityEngine.GameObject;

        uiLabel.GetComponent <TextMeshPro>().text = Name;


        smokeParticleSystem = root.transform.Find("main").gameObject.GetComponent <ParticleSystem>();
        var em = smokeParticleSystem.emission;

        var sources = GetComponents <AudioSource>();

        fireSound     = sources[0];
        shellHit      = sources[1];
        tankExplosion = sources[2];


        em.enabled = false;

        pointObjects = new List <GameObject>();


        turnRate          = ConfigValueStore.GetFloatValue("turn_speed");
        barrelRotateSpeed = ConfigValueStore.GetFloatValue("turret_rotation_speed");
        fireInterval      = ConfigValueStore.GetIntValue("fire_interval");
        projectileForce   = ConfigValueStore.GetFloatValue("projectile_force");
        speed             = ConfigValueStore.GetFloatValue("movement_speed");
        turnRate          = ConfigValueStore.GetFloatValue("turn_speed");
        startingAmmo      = ConfigValueStore.GetIntValue("starting_ammo");
        startingHealth    = ConfigValueStore.GetIntValue("starting_health");
        snitchGoalPoints  = ConfigValueStore.GetIntValue("snitch_goal_points");
        snitchKillPoints  = ConfigValueStore.GetIntValue("snitch_kill_points");
        killPoints        = ConfigValueStore.GetIntValue("kill_points");

        explosivo1 = Instantiate(Resources.Load("Prefabs/Explosion")) as UnityEngine.GameObject;
        explosivo1.SetActive(false);
        explosivo2 = Instantiate(Resources.Load("Prefabs/TankExplosion")) as UnityEngine.GameObject;
        explosivo2.SetActive(false);

        Health = startingHealth;
        Ammo   = startingAmmo;
    }
示例#3
0
    public void Update()
    {
        allObjects.Clear();


        HandlePickupLogic();

        foreach (TankController t in tanksToBeRemoved)
        {
            RemoveTank(t);
        }
        tanksToBeRemoved.Clear();

        while (enqueuedCommands.Count > 0)
        {
            GameCommand command = enqueuedCommands.Dequeue();
            HandleCommand(command);
        }

        lock (allObjects)
        {
            UpdateTankState();
        }

        lock (objectsInFieldOfView)
        {
            var tanks = UnityEngine.GameObject.FindObjectsOfType <TankController>();
            foreach (TankController t in tanks)
            {
                UpdateTankViewObjects(t);
            }
        }


        if (TrainingRoomMain.currentGameState == TrainingRoomMain.GameState.playing)
        {
            if (healthPickups.Count < healthPickupCount)
            {
                SpawnHealthPickup();
            }
            if (ammoPickups.Count < ammoPickupCount)
            {
                SpawnAmmoPickup();
            }

            if (TrainingRoomMain.timeLeft.TotalSeconds < ConfigValueStore.GetIntValue("snitch_spawn_threshold"))
            {
                if (!snitchSpawned && ConfigValueStore.GetBoolValue("snitch_enabled"))
                {
                    SpawnSnitch();
                }
            }
        }

        lock (tankDisconnects)
        {
            foreach (string s in tankDisconnects)
            {
                RemoveTank(FindTankObject(s));
            }
            tankDisconnects.Clear();
        }
    }
示例#4
0
    // Update is called once per frame
    public virtual void Update()
    {
        if (currentState == TankState.normal)
        {
            if (toggleForward)
            {
                Forward();
            }
            if (toggleReverse)
            {
                Reverse();
            }
            if (toggleLeft)
            {
                TurnLeft();
            }
            if (toggleRight)
            {
                TurnRight();
            }
            if (toggleTurretLeft)
            {
                TurretLeft();
            }
            if (toggleTurretRight)
            {
                TurretRight();
            }


            if (autoTurn)
            {
                if (IsTurnLeft(Heading, desiredHeading))
                {
                    TurnLeft();
                }
                else
                {
                    TurnRight();
                }

                float diff = Heading - desiredHeading;
                if (Math.Abs(diff) < 1)
                {
                    autoTurn = false;
                }
            }

            if (autoTurretTurn)
            {
                if (IsTurnLeft(TurretHeading, desiredTurretHeading))
                {
                    TurretLeft();
                }
                else
                {
                    TurretRight();
                }

                float diff = TurretHeading - desiredTurretHeading;
                if (Math.Abs(diff) < 2)
                {
                    autoTurretTurn = false;
                }
            }

            if (autoMove)
            {
                if (desiredDistance > 0)
                {
                    Forward();

                    float distanceTravelled = (oldPosition - transform.position).magnitude;
                    if (desiredDistance > distanceTravelled)
                    {
                        desiredDistance -= distanceTravelled;
                    }
                    else
                    {
                        autoMove = false;
                    }
                }
                else
                {
                    Reverse();

                    float distanceTravelled = (oldPosition - transform.position).magnitude;
                    if (Math.Abs(desiredDistance) > distanceTravelled)
                    {
                        desiredDistance += distanceTravelled;
                    }
                    else
                    {
                        autoMove = false;
                    }
                }
            }


            Quaternion q = Quaternion.FromToRotation(transform.up, Vector3.up) * transform.rotation;
            transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * reorientateSpeed);

            oldPosition = transform.position;
        }
        else if (currentState == TankState.destroyed)
        {
            TimeSpan sinceDestruction = DateTime.Now - destroyTime;
            if (sinceDestruction.TotalSeconds > ConfigValueStore.GetIntValue("respawn_time"))
            {
                Sim.RespawnTank(this);
            }
        }


        if (Camera.current != null)
        {
            var pos = new Vector3(transform.position.x, 10, transform.position.z);

            Quaternion labelRotation = Quaternion.LookRotation(pos - Camera.current.transform.position, Camera.current.transform.up);
            uiLabel.transform.SetPositionAndRotation(pos, labelRotation);
        }

        X        = transform.position.x;
        Y        = transform.position.z;
        ForwardX = transform.forward.x;
        ForwardY = transform.forward.z;

        //A = atan2(V.y, V.x)
        Heading = (float)Math.Atan2(transform.forward.z, transform.forward.x);
        Heading = Heading * Mathf.Rad2Deg;
        Heading = (Heading - 360) % 360;
        Heading = Math.Abs(Heading);

        TurretHeading = (float)Math.Atan2(-turret.transform.up.z, -turret.transform.up.x);
        TurretHeading = TurretHeading * Mathf.Rad2Deg;
        TurretHeading = (TurretHeading - 360) % 360;
        TurretHeading = Math.Abs(TurretHeading);


        TurretForwardX = turret.transform.up.x;
        TurretForwardY = turret.transform.up.z;


        if (transform.position.y < -10)
        {
            DestroyTank();
        }


        if (infiniteAmmo)
        {
            ReplenishAmmo();
        }
        if (infiniteHealth)
        {
            ReplenishHealth();
        }

        int i = 0;

        foreach (GameObject o in pointObjects)
        {
            o.transform.position = transform.position + new Vector3(0, 5 + (i * 0.5f), 0);
            i++;
        }
    }