示例#1
0
    public override void _Ready()
    {
        lapTimer        = GetNode <Timer>("LapTimer");
        levelStartTimer = GetNode <Timer>("CountDownTimer");
        //Get the HUD node to enable the minipulation of its child nodes
        hud = GetNode <CanvasLayer>("HUD");
        //Get the HUD script
        hudScript = GetNode <HUD>("HUD");
        //Load the levels in the list of levels
        levels.Add(ResourceLoader.Load("res://Nodes/Level1.tscn") as PackedScene);
        levels.Add(ResourceLoader.Load("res://Nodes/Level2.tscn") as PackedScene);
        levels.Add(ResourceLoader.Load("res://Nodes/Level3.tscn") as PackedScene);

        //Grabs the ball scene and loads it in a container for quick reference
        ballScene = (PackedScene)ResourceLoader.Load("res://Nodes/Ball.tscn");
        //Connect to the HUDs signals for he show menu button functionality
        hud.Connect("ShowMenu", this, nameof(ShowMenu));
        //Connect to the HUDs signals for he show menu button functionality
        hud.Connect("ShowHUD", this, nameof(StartLevel));

        levelResetTimer = GetNode <Timer>("LevelResetTimer");
    }
示例#2
0
 public void CreateConnection()
 {
     _gui.Connect("NewGame", this, nameof(StartGame));
 }
示例#3
0
    //Load stage with given stage number
    private void LoadStage(int stageNumber)
    {
        //Access stage configuration
        var aFile = new File();

        if (!aFile.FileExists(StageConfig))
        {
            return;             //Handling error
        }

        aFile.Open(StageConfig, File.ModeFlags.Read);

        while (!aFile.EofReached())
        {
            var currentLine = aFile.GetLine();
            var stages      = JsonConvert.DeserializeObject <System.Collections.Generic.List <StageObject> >(currentLine);
            if (stages == null)
            {
                continue;
            }

            if (stageNumber > stages.Count)
            {
                GD.Print("Put the portal at different position from the position of its previous stage portal.");
                return;
            }
            var stage = stages[stageNumber - 1].stage;

            //Set the start position of the player in this stage
            Position2D startPosition = (Position2D)GameWorld.GetNode("StartPosition");
            startPosition.Position = new Vector2(stage.startPosX, stage.startPosY);

            //Set Brackground
            Sprite background = (Sprite)GameWorld.GetNode("ParallaxBackground/ParallaxLayer/Background");
            background.Texture = ResourceLoader.Load(stage.backgroundImg) as Texture;

            //Create TileMap
            var tileMapScene = (PackedScene)GD.Load(stage.mapScene);
            if (tileMapScene == null)
            {
                return;                                 //Error handling
            }
            var tileMap = tileMapScene.Instance();      //Instance TileMap

            //Access HUD node
            CanvasLayer hud = (CanvasLayer)GameWorld.GetNode("HUD");

            //Attach TileMap below HUD node in World
            GameWorld.AddChildBelowNode(hud, tileMap);
            //Add tileMap to Replaceable group
            tileMap.AddToGroup("Replaceable");

            //Create objects of the TileMap here
            //Stage portal through which player can go to the next stage
            if (tileMap.HasNode("StagePortal"))
            {
                var portal = (Portal)tileMap.GetNode("StagePortal");
                portal.Connect("Entered", GameWorld, "OnStagePortalEntered");
            }

            //Dealing with enemeies
            //Access Enemies node
            Node enemiesNode = (Node)GameWorld.GetNode("Enemies");
            //Create and add an enemy
            var enemies = (List <Enemy>)stage.enemy;

            //ForEach loop: taking care of each enemy
            enemies.ForEach(delegate(Enemy enemy)
            {
                var enemyScene = (PackedScene)GD.Load(enemy.scene);
                if (enemyScene == null)
                {
                    return;                                                             //Error handling
                }

                if (enemy.mobPath != null)                                                                      //Is it FlyingMob mobs
                {
                    var mobPathScene = (PackedScene)GD.Load(enemy.mobPath);
                    if (mobPathScene == null)
                    {
                        return;                                                         //Error handling
                    }
                    var mobPath = mobPathScene.Instance();                              //Instance ModPath
                    GameWorld.AddChild(mobPath);

                    for (int i = 0; i < enemy.instances; ++i)
                    {
                        var enemyNode           = (FlyingMob)enemyScene.Instance();                     //Instance a mob
                        var spawnLocation       = (PathFollow2D)mobPath.GetNode("MobSpawnLocation" + " " + i.ToString());
                        enemyNode.SpawnLocation = spawnLocation;
                        enemyNode.Level         = enemy.level;
                        enemyNode.MinSpeed      = enemy.minSpeed;
                        enemyNode.MaxSpeed      = enemy.maxSpeed;
                        enemyNode.MovingTypes   = enemy.movingTypes;
                        enemiesNode.AddChild(enemyNode);
                        enemyNode.Connect("MobDie", GameWorld, "OnMobDie");

                        //Add it to Enemies group
                        enemyNode.AddToGroup("Enemies");
                    }
                }
                else
                {                                                                                       //WalkingMob mobs
                    var enemyNode         = (WalkingMob)enemyScene.Instance();                          //Instance a mob
                    enemyNode.Level       = enemy.level;
                    enemyNode.MinSpeed    = enemy.minSpeed;
                    enemyNode.MaxSpeed    = enemy.maxSpeed;
                    enemyNode.Position    = new Vector2(enemy.posX, enemy.posY);
                    enemyNode.MovingTypes = enemy.movingTypes;
                    enemiesNode.AddChild(enemyNode);
                    enemyNode.Connect("MobDie", GameWorld, "OnMobDie");

                    //Add it to Enemies group
                    enemyNode.AddToGroup("Enemies");
                }
            });             //ForEach loop: taking care of each enemy

            //Create and add Player
            Player player = null;
            if (!GameWorld.HasNode("Player"))
            {
                var playerScene = (PackedScene)GD.Load(PlayerScene);
                if (playerScene == null)
                {
                    return;                                                                                     //Error handling
                }
                player = (Player)playerScene.Instance();
                GameWorld.AddChild(player);

                //Establish Connects between player and hud
                player.Connect("ShootFireBall", hud, "OnPlayerShootFireBall");
                hud.Connect("EnableFeature", player, "OnHudEnableFeature");
                hud.Connect("CircleButtonPressed", player, "OnHudCircleButtonPressed");
                hud.Connect("SpotTarget", player, "OnHudSpotTarget");
            }
            else
            {
                player = (Player)GameWorld.GetNode("Player");
            }

            //Set stage
            player.Stage = stageNumber;

            //For player who never played before
            if (player.Level == 0)
            {
                player.LevelUp(0);
            }

            player.Position = startPosition.Position;
        }
        aFile.Close();
    }