private void PopulatePhysicsWorld(PhysicWorldModel physicsModel, PhysicWorldController physicsController)
 {
     // TODO: read from somewhere.. right now it's ardcoded
     // TODO: do it in assets packer, then load from file
     GameObject[] quadObjects = GameObject.FindGameObjectsWithTag("quad");
     foreach (GameObject quadObj in quadObjects)
     {
         ProducePlaneFromQuad(physicsModel, physicsController, quadObj);
     }
 }
Exemplo n.º 2
0
    private void UpdateShadow(GameEntityModel model, GameObject entityObject)
    {
        GameObject       shadowObj  = CharacterLoader.GetCharacterShadow(model, entityObject);
        PhysicPointModel pointModel = GameEntityController.GetPointModel(model);

        if (shadowObj == null || pointModel == null)
        {
            return;
        }

        Vector3 newShadowPosition;

        if (pointModel.collisionInpact.Y < 0)
        {
            newShadowPosition = pointModel.position.AsVector3();
        }
        else
        {
            // Raycast on physics
            WorldModel world = StateManager.state.MainModel as WorldModel;
            if (world == null)
            {
                return;
            }
            PhysicWorldModel physicsModel = StateManager.state.GetModel(world.physicsModelId) as PhysicWorldModel;
            if (physicsModel == null)
            {
                return;
            }
            PhysicWorldController physicsController = physicsModel.Controller() as PhysicWorldController;
            if (physicsController == null)
            {
                return;
            }

            PhysicPointModel entityPoint  = GameEntityController.GetPointModel(model);
            FixedVector3     intersection = physicsController.Raycast(physicsModel, entityPoint.position, FixedVector3.Down);

            newShadowPosition = intersection.AsVector3();
        }
        // TODO: coordinates conversion from a setup file
        shadowObj.transform.position      = PhysicPoint2DView.ConvertGameToViewCoordinates(newShadowPosition);
        shadowObj.transform.localPosition = new Vector3(0, shadowObj.transform.localPosition.y, 0.02f);
    }
    private void ProducePlaneFromQuad(PhysicWorldModel physicsModel, PhysicWorldController physicsController, GameObject quadObj)
    {
        MeshFilter meshFilter = quadObj.GetComponent <MeshFilter>();

        if (meshFilter == null)
        {
            return;
        }
        Vector3[] vertices = meshFilter.mesh.vertices;
        if (vertices.Length < 4)
        {
            return;
        }

        PhysicPlaneModel plane = new PhysicPlaneModel(
//			null, "debug_planes",
            quadObj.transform.TransformPoint(vertices[0]).AsFixedVetor3(),
            quadObj.transform.TransformPoint(vertices[3]).AsFixedVetor3(),
            quadObj.transform.TransformPoint(vertices[1]).AsFixedVetor3(),
            quadObj.transform.TransformPoint(vertices[2]).AsFixedVetor3()
            );

        physicsController.AddPlane(physicsModel, plane);
    }
    protected override void Update(WorldModel model)
    {
        // Get physics model. If doesn't exist, create it
        PhysicWorldModel      physicsModel;
        PhysicWorldController physicsController;
        TeamsManagerModel     teamsManagerModel;
        Model playerModel;

        if (model.physicsModelId == ModelReference.InvalidModelIndex)
        {
            // create world with map name and gravity
            physicsModel         = new PhysicWorldModel("map 1", new FixedVector3(0, gravityY, 0));
            model.physicsModelId = StateManager.state.AddModel(physicsModel);
            physicsController    = physicsModel.Controller() as PhysicWorldController;
            // populate world
            PopulatePhysicsWorld(physicsModel, physicsController);
        }
        else
        {
            physicsModel      = StateManager.state.GetModel(model.physicsModelId) as PhysicWorldModel;
            physicsController = physicsModel.Controller() as PhysicWorldController;
        }



        // Teams manager
        if (model.teamsModelId == ModelReference.InvalidModelIndex)
        {
            teamsManagerModel  = new TeamsManagerModel(3);
            model.teamsModelId = StateManager.state.AddModel(teamsManagerModel);


            // Create some dummy enemy
            FixedVector3 initialPosition = GetRandomSpawnPosition(model);
            playerModel = new GameEntityModel(
                StateManager.state,
                physicsModel,
                new PhysicPointModel(
                    null,
                    initialPosition,
                    new FixedVector3(0, 0.5, 0),
                    DefaultVCFactoryIds.PhysicPointControllerFactoryId,
                    SorVCFactories.Point2DViewFactoryId,
                    DefaultUpdateOrder.PhysicsUpdateOrder
                    ),
                new AnimationModel(
                    null,
                    "Axel",
                    "standing",
                    CharacterLoader.GetCharacterSkinName("Axel", 0)
                    ),
                null,                 // no input
                DefaultVCFactoryIds.GameEntityControllerFactoryId,
                SorVCFactories.Entity2DViewFactoryId,
                DefaultUpdateOrder.EntitiesUpdateOrder
                );
            // Model initial state
            GameEntityModel playerEntity = (GameEntityModel)playerModel;
            playerEntity.mIsFacingRight = initialPosition.X < 0;
            // hardcoded energy
            playerEntity.customVariables["energy"] = 100;
            teamsManagerModel.teams[2].entities.Add(StateManager.state.AddModel(playerModel));


//			// Create some weapon
//			initialPosition = new FixedVector3(2, 8, 3);
//			playerModel = new GameEntityModel(
//				StateManager.state,
//				physicsModel,
//				new PhysicPointModel(
//					null,
//					initialPosition,
//					new FixedVector3(0, 0.5, 0),
//					DefaultVCFactoryIds.PhysicPointControllerFactoryId,
//					SorVCFactories.Point2DViewFactoryId,
//					DefaultUpdateOrder.PhysicsUpdateOrder
//				),
//				new AnimationModel(
//					null,
//					"Bat_HD",
//					"bat_air",
//					CharacterLoader.GetCharacterSkinName("Bat_HD", 0)
//				),
//				null, // no input
//				DefaultVCFactoryIds.GameEntityControllerFactoryId,
//				SorVCFactories.Entity2DViewFactoryId,
//				DefaultUpdateOrder.EntitiesUpdateOrder
//			);

//			// Model initial state
//			playerEntity = (GameEntityModel)playerModel;
//			playerEntity.isFacingRight = initialPosition.X < 0;
//			// hardcoded energy
//			playerEntity.customVariables["energy"] = 100;
            // Goodies going on team 0
//			teamsManagerModel.teams[0].entities.Add(StateManager.state.AddModel(playerModel));
        }
        else
        {
            teamsManagerModel = StateManager.state.GetModel(model.teamsModelId) as TeamsManagerModel;
        }


        List <uint> allPlayers;

        if (StateManager.Instance.IsNetworked)
        {
            allPlayers = NetworkCenter.Instance.GetAllNumbersOfConnectedPlayers();
        }
        else
        {
            allPlayers = new List <uint>();
            allPlayers.Add(0);
        }

        // Remove characters for inactive players
        foreach (KeyValuePair <uint, ModelReference> pair in model.players)
        {
            if (!allPlayers.Exists(x => x == pair.Key))
            {
                // Doesn't exist anymore, remove ship
                playerModel = StateManager.state.GetModel(pair.Value);
                //worldController.RemovePoint(shipModel, OnShipDestroyed, model);
                StateManager.state.RemoveModel(playerModel, OnPlayerRemoved, model);
            }
        }

        // Create characters for new players
        NetworkSorPlayerData playerData;

        foreach (uint playerId in allPlayers)
        {
            if (!model.players.ContainsKey(playerId))
            {
                if (StateManager.Instance.IsNetworked)
                {
                    playerData = NetworkCenter.Instance.GetPlayerData <NetworkSorPlayerData>(playerId);
                }
                else
                {
                    playerData = NetworkCenter.Instance.GetPlayerData <NetworkSorPlayerData>();
                }
                if (playerData == null)
                {
                    RetroBread.Debug.LogWarning("Player " + playerId + " have invalid player data!");
                    continue;
                }
                RetroBread.Debug.Log("Player " + playerId + " have character " + playerData.selectedCharacter);
                Model        inputModel      = new PlayerInputModel(playerId);
                FixedVector3 initialPosition = GetRandomSpawnPosition(model);
                playerModel = new GameEntityModel(
                    StateManager.state,
                    physicsModel,
                    new PhysicPointModel(
                        null,
                        initialPosition,
                        new FixedVector3(0, 0.5, 0),
                        DefaultVCFactoryIds.PhysicPointControllerFactoryId,
                        SorVCFactories.Point2DViewFactoryId,                         // DefaultVCFactoryIds.PhysicPointViewFactoryId,
                        DefaultUpdateOrder.PhysicsUpdateOrder
                        ),
                    new AnimationModel(
                        null,
                        "Axel",
                        "standing",
                        CharacterLoader.GetCharacterSkinName("Axel", 0)
                        ),
                    inputModel,
                    DefaultVCFactoryIds.GameEntityControllerFactoryId,
                    SorVCFactories.Entity2DViewFactoryId,                     //DefaultVCFactoryIds.GameEntityViewFactoryId,
                    DefaultUpdateOrder.EntitiesUpdateOrder
                    );
                // Model initial state
                GameEntityModel playerEntity = (GameEntityModel)playerModel;
                playerEntity.mIsFacingRight = initialPosition.X < 0;
                model.players[playerId]     = StateManager.state.AddModel(playerModel);
                // hardcoded energy
                playerEntity.customVariables["energy"] = 100;
                teamsManagerModel.teams[1 + playerId % 2].entities.Add(model.players[playerId]);
            }

            GameObject obj = UnityObjectsPool.Instance.GetGameObject(model.players[playerId]);
            // using the name as a hack around having a variable in a script to tell the object is "initialized"
            // should be done in a better way.. but whatever, will do for the demo
            if (obj != null && !obj.name.EndsWith("[initiated]"))
            {
                bool isOwnPlayer;
                if (StateManager.Instance.IsNetworked)
                {
                    isOwnPlayer = playerId == NetworkCenter.Instance.GetPlayerNumber();
                }
                else
                {
                    isOwnPlayer = true;                     // warning: local multiplayer would be different, playerId == 0?, actually different camera
                }

                if (isOwnPlayer)
                {
                    // Add camera tracking to own player :)
                    obj.AddComponent <CameraTracker>();

                    // TODO: add HUD tracking to this player
                    // Note: I'm doing this here at the moment, but if I need to derive GameEntity
                    // this can go inside it's view.
                    // However I can create a new model just to keep player stats (kills, energy, etc)
                    // and use the respective view to display the stats in the HUD
                }
                obj.name += "[initiated]";
            }
        }

        // check end of the game
        if (CheckGameOver(model))
        {
            // game over
            return;
        }
    }