Beispiel #1
0
    public void RemoveSceneCharacter(int id)
    {
        Debug.Log("Player disconnected with char ID " + id);
        var sceneChar = SceneCharacters [id];

        if (sceneChar == null)
        {
            return;
        }

        SceneCharacters.Remove(id);
        sceneChar.GoAway();
    }
Beispiel #2
0
    /// <summary>
    /// Instantiates an AI to attack players
    /// </summary>
    private void InstantiateAI()
    {
        Debug.Log("instantiating AI");
        Debug.Log("spawn point is " + SceneInformation.AiSpawn);

        var aiInstantiation = (GameObject)GameObject.Instantiate(
            AIPrefab,
            SceneInformation.AiSpawn.position,
            SceneInformation.AiSpawn.rotation);

        Debug.Log("AI Instantiation: " + aiInstantiation);

        IArtificialIntelligence aiSceneCharacter = null;

        aiSceneCharacter = aiInstantiation.GetComponent <AI3D>();


        if (aiSceneCharacter == null)
        {
            throw new InvalidOperationException("AI prefab needs an AI component that matches the 3D/2D aspect of the game");
        }

        ////Debug.Log ("AI scene char: " + aiSceneCharacter);

        int charId = AI_ID_INDEX + AICount;

        AICount++;

        aiSceneCharacter.Server = this;
        ((Character)aiSceneCharacter.GetSceneChar()).Team = "AI";
        ((Character)aiSceneCharacter.GetSceneChar()).Id   = charId;

        Debug.Log("Adding AI character " + aiSceneCharacter.BaseCharacter.CharName + " with ID " + charId);

        SceneCharacters.Add(charId, aiSceneCharacter.GetSceneChar());
        foreach (var charId2 in SceneCharacters.Keys)
        {
            Debug.Log("Scene characters contains key: " + charId2);
        }

        GetComponent <NetworkView>().RPC("InitNewSceneCharacter", RPCMode.Others, aiSceneCharacter.BaseCharacter.Serialize());
    }
Beispiel #3
0
    /// <summary>
    /// Update is called once per frame
    /// </summary>
    void Update()
    {
        if (!IsActive)
        {
            return;
        }

        //Debug.Log("Level: " + Application.loadedLevelName + "\n MyCharacterx: " + MyCharacter);
        if (MyCharacter == null || Application.loadedLevelName == MainMenuScene)
        {
            return;
        }

        if (SceneInformation == null)
        {
            LoadSceneInfo();
            GameLevel = new Level();
            GameLevel.load(GameLevelFile);
            GameObject level_object = GameLevel.game_object();
            level_object.transform.parent = SceneInformation.transform;
        }



        if (CharactersToInstantiate.Count > 0)
        {
            foreach (var character in CharactersToInstantiate)
            {
                InstantiateSceneCharacter(character);
            }

            CharactersToInstantiate.Clear();
        }

        if (ProjectilesToInstantiate.Count > 0)
        {
            foreach (var proj in ProjectilesToInstantiate)
            {
                InstantiatePlasma(SceneCharacters[proj]);
            }
            ProjectilesToInstantiate.Clear();
        }

        if (MyPlayer == null && SceneCharacters != null &&
            SceneCharacters.ContainsKey(MyCharacter.Id))
        {
            MyPlayer                 = SceneCharacters[MyCharacter.Id];
            MyPlayer.Client          = this;
            MyPlayer.BaseCharacter   = MyCharacter;
            MyPlayer.tag             = "Player";
            MyPlayer.BaseCharacter.R = MyColor.r;
            MyPlayer.BaseCharacter.G = MyColor.g;
            MyPlayer.BaseCharacter.B = MyColor.b;
            //recolor_walker(MyPlayer.gameObject, MyColor);
            GetComponent <NetworkView>().RPC("ClientColor", RPCMode.Server, MyColor.r, MyColor.g, MyColor.b);
        }

        if (CurrentGameState != GameStates.LevelLoaded ||
            MyPlayer == null)
        {
            return;
        }

        InterpolateCharacters();
        UpdateControls();
        MovePlayer();
        UpdateCamera();
        UpdateCombatControls();
        ValidateMyPosition();
        HandleProjectiles();
    }
Beispiel #4
0
    public void UpdateCharacter(int charId, Vector3 position, float direction, int walking, Quaternion headRot, Vector3 velocity, float crouchfactor)
    {
        ////Debug.Log ("Update called for " + charId + " at " + position.x + ", " + position.y + ", " + position.z);

        // Don't update self if using client-side prediction
        if (CurrentGameState != Game.GameStates.LevelLoaded ||
            MyPlayer == null ||
            (ClientSidePrediction && charId.Equals(((Character)MyPlayer).Id)) ||
            !SceneCharacters.ContainsKey(charId)
            )
        {
            return;
        }

        SceneCharacter3D character = SceneCharacters[charId];
        ObjectState      charState = new ObjectState(charId, position, direction, headRot, velocity, crouchfactor, walking);

        // TODO: fix this to call a special method just updating the legs
        // character.GetComponent<SceneCharacter3D>().Move(1f, 0f, Time.deltaTime, false);

        if (!CharacterInterpolation)
        {
            character.transform.position           = position;
            character.state.velocity               = velocity;
            character.state.angle                  = direction;
            character.head.transform.localRotation = headRot;
            character.crouch_factor                = crouchfactor;
            character.transform.localEulerAngles   = new Vector3(0, direction, 0);
            return;
        }

        // 7A) Store the character state from the server
        if (CharacterIntendedStates.ContainsKey(charId))
        {
            CharacterIntendedStates.Remove(charId);
            CharacterPositionDiffs.Remove(charId);
            CharacterDirectionDiffs.Remove(charId);
            CharacterHeadRotDiffs.Remove(charId);
            CharacterCrouchFactorDiffs.Remove(charId);
        }

        CharacterIntendedStates.Add(charId, charState);

        // 7B) Calculate the position difference
        Vector3 currentCharPosition       = character.transform.position;
        Vector3 intendedCharacterPosition = charState.Position;

        CharacterPositionDiffs.Add(charId, new Vector3(
                                       intendedCharacterPosition.x - currentCharPosition.x,
                                       intendedCharacterPosition.y - currentCharPosition.y,
                                       intendedCharacterPosition.z - currentCharPosition.z
                                       ));

        // Calculate the direction difference
        float currentCharDirection       = character.transform.localEulerAngles.y;
        float intendedCharacterDirection = charState.Angle;

        /*CharacterDirectionDiffs.Add(charId, new Vector3(
         *          intendedCharacterDirection.x - currentCharDirection.x,
         *          intendedCharacterDirection.y - currentCharDirection.y,
         *          intendedCharacterDirection.z - currentCharDirection.z
         * ));
         */

        // TODO: use Mathf.DeltaAngle instead and test
        //float phi = Mathf.Abs(currentCharDirection - intendedCharacterDirection) % 360f;
        //float dist = phi > 180 ? 360 - phi : phi;
        float dist = Mathf.DeltaAngle(currentCharDirection, intendedCharacterDirection);

        CharacterDirectionDiffs.Add(charId, dist);


        Quaternion currentCharHeadRot       = character.head.transform.localRotation;
        Quaternion intendedCharacterHeadRot = charState.HeadRot;

        CharacterHeadRotDiffs.Add(charId, new Quaternion(
                                      intendedCharacterHeadRot.x - currentCharHeadRot.x,
                                      intendedCharacterHeadRot.y - currentCharHeadRot.y,
                                      intendedCharacterHeadRot.z - currentCharHeadRot.z,
                                      intendedCharacterHeadRot.w - currentCharHeadRot.w
                                      ));

        float currentCharCrouchFactor       = character.crouch_factor;
        float intendedCharacterCrouchFactor = charState.CrouchFactor;

        CharacterCrouchFactorDiffs.Add(charId, intendedCharacterCrouchFactor - currentCharCrouchFactor);
    }