public override void init()
        {
            Gamepad_Util.vibrate(0, 0.0f, 0.0f);
            Gamepad_Util.vibrate(1, 0.0f, 0.0f);
            Gamepad_Util.vibrate(2, 0.0f, 0.0f);
            Gamepad_Util.vibrate(3, 0.0f, 0.0f);
            graphicsDevice = Starburst.inst().GraphicsDevice;
            sprite_batch   = new SpriteBatch(graphicsDevice);


            add_subsystems(
                new Menu_Input_Handler(),
                new Sound()
                );

            font = Starburst.inst().get_content <SpriteFont>("sector034");

            // hämta inputs från föregående state
            var inputs = last_state.get_entities_fast(typeof(Input));

            for (int i = 0; i < inputs.Count; i++)
            {
                var input         = inputs[i].get_component <Input>();
                var gamepadPlayer = create_entity(Player.create_components(input));
            }

            restore_vol         = MediaPlayer.Volume;
            MediaPlayer.Volume *= 0.4f;
        }
        public override void init()
        {
            Gamepad_Util.vibrate(0, 0.0f, 0.0f);

            Gamepad_Util.vibrate(1, 0.0f, 0.0f);

            Gamepad_Util.vibrate(2, 0.0f, 0.0f);

            Gamepad_Util.vibrate(3, 0.0f, 0.0f);


            sprite_batch = new SpriteBatch(Starburst.inst().GraphicsDevice);

            add_subsystems(
                new Menu_Input_Handler(),
                new Sound(),
                new Particle_System(),
                new Background_Renderer(sprite_batch)
                );

            outDelay      = delay + inDuration + displayTime;
            animationTime = outDelay + outDuration;

            soundMgr = create_entity(SoundManager.create_backmusic_component());
            soundMgr.get_component <SoundLibrary>().song_index = 1;

            vp     = sprite_batch.GraphicsDevice.Viewport;
            lowRes = (vp.Height < 800 && vp.Width < 1600);

            // load textures
            background = Starburst.inst().get_content <Texture2D>("backdrops/backdrop4");
            rectBg     = new Texture2D(Fab5_Game.inst().GraphicsDevice, 1, 1);
            rectBg.SetData(new Color[] { Color.Black }, 1, 1);//Starburst.inst().get_content<Texture2D>("controller_rectangle");
            font                = Starburst.inst().get_content <SpriteFont>(!lowRes ? "sector034" : "small");
            largeFont           = Starburst.inst().get_content <SpriteFont>("large");
            controller_a_button = Starburst.inst().get_content <Texture2D>("menu/Xbox_A_white");
            keyboard_key        = Starburst.inst().get_content <Texture2D>("menu/Key");
            controller_l_stick  = Starburst.inst().get_content <Texture2D>("menu/Xbox_L_white");

            okSize = font.MeasureString(" ");

            controllerBtnSize = !lowRes ? 50 : 38; // ikon för knapp
            heightDiff        = (int)(controllerBtnSize - okSize.Y);
            yPos = (int)(vp.Height - controllerBtnSize - 15);

            for (int i = 0; i < players.Count; i++)
            {
                if (players[i].get_component <Input>() != null)
                {
                    create_entity(Player.create_components(players[i].get_component <Input>()));
                }
            }
            graphicsDevice    = sprite_batch.GraphicsDevice;
            resultsViewHeight = (int)(vp.Height * .83f);
            resultsRT         = new RenderTarget2D(graphicsDevice, vp.Width, resultsViewHeight);

            Comparer <Entity> sort_on_score = Comparer <Entity> .Create((e1, e2) => - e1.get_component <Score>().score.CompareTo(e2.get_component <Score>().score));

            if (gameConfig.mode == Game_Config.GM_TEAM_DEATHMATCH)
            {
                redTeam   = new List <Entity>();
                blueTeam  = new List <Entity>();
                redScore  = 0;
                blueScore = 0;
                redGoals  = 0;
                blueGoals = 0;

                // lägg spelare i rätt lag
                for (int p = 0; p < players.Count; p++)
                {
                    Ship_Info player_info  = players[p].get_component <Ship_Info>();
                    Score     player_score = players[p].get_component <Score>();
                    if (players[p].get_component <Velocity>() == null) // turret, lägg till poäng men inte entiteten i laget
                    {
                        if (player_info.team == 1)
                        {
                            redScore += (int)player_score.score;
                        }
                        else
                        {
                            blueScore += (int)player_score.score;
                        }
                        continue;
                    }
                    if (player_info == null)
                    {
                        continue;
                    }
                    if (player_info.team == 1)
                    {
                        redTeam.Add(players[p]);
                        redGoals  = player_score.num_goals;
                        redScore += (int)player_score.score;
                    }
                    else
                    {
                        blueTeam.Add(players[p]);
                        blueGoals  = player_score.num_goals;
                        blueScore += (int)player_score.score;
                    }
                }
                // sortera lag efter bäst score
                redTeam.Sort(sort_on_score);
                blueTeam.Sort(sort_on_score);

                redTeamHeight  = rowHeight * (redTeam.Count + 0) + vertSpacing * (redTeam.Count - 1);
                blueTeamHeight = rowHeight * (blueTeam.Count + 0) + vertSpacing * (blueTeam.Count - 1);

                totalPlayerHeight = redTeamHeight + 50 + blueTeamHeight + 50;

                totalResultsHeight = totalPlayerHeight - resultsViewHeight + 100;
                if (resultsViewHeight < redTeamHeight + 50 + blueTeamHeight + 50 + 100)
                {
                    scrollable = true;
                }
            }
            else
            {
                bestScore   = 0;
                bestPlayers = new List <Entity>();
                List <Entity> checkedPlayers = new List <Entity>();
                // ta bort turrets (ska inte finnas några här, men tas bort utifall att)
                for (int i = 0; i < players.Count; i++)
                {
                    if (players[i].get_component <Velocity>() != null)
                    {
                        checkedPlayers.Add(players[i]);
                    }
                }
                players = checkedPlayers;
                for (int i = 0; i < players.Count; i++)
                {
                    Score     player_score = players[i].get_component <Score>();
                    Ship_Info player_info  = players[i].get_component <Ship_Info>();

                    if (player_score == null)
                    {
                        continue;
                    }

                    if (player_score.score > bestScore)
                    {
                        bestPlayers.Clear();
                        bestScore = (int)player_score.score;
                        bestPlayers.Add(players[i]);
                    }
                    else if (player_score.score == bestScore)
                    {
                        bestPlayers.Add(players[i]);
                    }
                }
                players.Sort(sort_on_score);

                totalPlayerHeight  = rowHeight * (players.Count + 0) + vertSpacing * (players.Count - 1);
                totalResultsHeight = totalPlayerHeight + 50 - resultsViewHeight;
                if (resultsViewHeight < totalPlayerHeight + 50)
                {
                    scrollable = true;
                }
            }


            vertPadding = 10;
            horPadding  = 20;
            horSpacing  = 20;
            nameWidth   = 300;

            killsHeader  = "Kills";
            deathsHeader = "Deaths";
            scoreHeader  = "Score";
            handlerSize  = new Vector2(80, 64);
            killsSize    = font.MeasureString(killsHeader);
            deathsSize   = font.MeasureString(deathsHeader);
            scoreSize    = font.MeasureString("999999");

            iconSizeX       = 42;
            iconSizeY       = 30;
            iconSizeX       = 63;
            iconSizeY       = 45;
            totalScoreWidth = (int)(iconSizeX + horSpacing + nameWidth + horSpacing + killsSize.X + horSpacing + deathsSize.X + horSpacing + scoreSize.X);
            iconX           = (int)(vp.Width * .5f - totalScoreWidth * .5f);
            nameX           = iconX + iconSizeX + horSpacing;
            killsX          = nameX + nameWidth + horSpacing;
            deathsX         = (int)(killsX + killsSize.X + horSpacing);
            scoreX          = (int)(deathsX + deathsSize.X + horSpacing);

            textOffset = (int)((rowHeight - killsSize.Y) * .5f);
        }
Beispiel #3
0
        public override void draw(float t, float dt)
        {
            var entities       = Fab5_Game.inst().get_entities_fast(typeof(Input));
            int num_components = entities.Count;

            for (int i = 0; i < num_components; i++)
            {
                var entity = entities[i];
                var pos    = entity.get_component <Position>();
                var input  = entity.get_component <Input>();
                var angle  = entity.get_component <Angle>();

                if (input.left_vib != input.old_left_vib || input.right_vib != input.old_right_vib)
                {
                    //System.Console.WriteLine("vib " + input.left_vib + ", " + input.right_vib);
                    if (input.device == Input.InputType.Controller)
                    {
                        Gamepad_Util.vibrate((int)input.gp_index, input.left_vib, input.right_vib);
                    }

                    input.old_left_vib  = input.left_vib;
                    input.old_right_vib = input.right_vib;
                }

                input.left_vib  -= 4.0f * input.left_vib * dt;
                input.right_vib -= 4.0f * input.right_vib * dt;

                if (input.left_vib < 0.05f)
                {
                    input.left_vib = 0.0f;
                }

                if (input.right_vib < 0.05f)
                {
                    input.right_vib = 0.0f;
                }

                // all player controlled objects get an angular drag force to prevent the players from going insane lol
                angle.ang_vel -= angle.ang_vel * 5.0f * dt;

                if (!input.enabled)
                {
                    continue;
                }

                var velocity = entity.get_component <Velocity>();

                var              ship            = entity.get_component <Ship_Info>();
                Primary_Weapon   primaryWeapon   = entity.get_component <Primary_Weapon>();
                Secondary_Weapon secondaryWeapon = entity.get_component <Secondary_Weapon>();

                var max_speed = ship.top_velocity;
                var acc       = ship.acceleration;

                float turn = 0.0f;

                KeyboardState currentKeyboardState = Keyboard.GetState();

                // Keyboard device
                if (input.device == Input.InputType.Keyboard)
                {
                    if (currentKeyboardState.IsKeyDown(input.left))
                    {
                        turn -= 1.0f;
                    }
                    if (currentKeyboardState.IsKeyDown(input.right))
                    {
                        turn += 1.0f;
                    }

                    input.throttle = 0.0f;

                    if (currentKeyboardState.IsKeyDown(input.up))
                    {
                        input.throttle = 1.0f;
                    }
                    else if (currentKeyboardState.IsKeyDown(input.down))
                    {
                        input.throttle = -1.0f;
                    }

                    if (currentKeyboardState.IsKeyDown(input.primary_fire))
                    {
                        fire(entity, ship, primaryWeapon);
                    }
                    if (currentKeyboardState.IsKeyDown(input.secondary_fire))
                    {
                        fire(entity, ship, secondaryWeapon);
                    }

                    if (is_key_clicked(input.powerup_next, currentKeyboardState, input.keyboardState))
                    {
                        Fab5_Game.inst().message("play_sound", new { name = "nextpowerup", volume = 1 });
                        ship.powerup_inv_index = ship.powerup_inv_index >= ship.max_powerups_inv - 1 ? 0 : ship.powerup_inv_index + 1;
                    }
                    if (is_key_clicked(input.powerup_use, currentKeyboardState, input.keyboardState))
                    {
                        Fab5_Game.inst().message("play_sound", new { name = "use_powerup", volume = 1 });
                        ship.use_powerup(ship.powerup_inv_index);
                    }
                }
                // Gamepad device
                else
                {
                    GamePadState state = GamePad.GetState(input.gp_index);
                    turn = state.ThumbSticks.Left.X;
                    //turn = Math.Sign((float)Math.Atan2(-state.ThumbSticks.Left.Y, state.ThumbSticks.Left.X) - angle.angle) * 10.0f;;
                    input.throttle = (float)Math.Pow(state.Triggers.Right, 1.5f) - (float)Math.Pow(state.Triggers.Left, 1.5f);

                    if (state.Buttons.A == ButtonState.Pressed)
                    {
                        fire(entity, ship, primaryWeapon);
                    }
                    if (state.Buttons.X == ButtonState.Pressed)
                    {
                        fire(entity, ship, secondaryWeapon);
                    }

                    if (state.Buttons.LeftShoulder == ButtonState.Released &&
                        state.Buttons.RightShoulder == ButtonState.Released)
                    {
                        input.can_switch_powerups = true;
                    }
                    if (is_gamepad_clicked(Buttons.LeftShoulder, state, input.gamepadState))
                    {
                        ship.powerup_inv_index = ship.powerup_inv_index > 0 ? ship.powerup_inv_index - 1 : ship.max_powerups_inv - 1;
                        Fab5_Game.inst().message("play_sound", new { name = "nextpowerup", volume = 1 });
                    }
                    if (is_gamepad_clicked(Buttons.RightShoulder, state, input.gamepadState))
                    {
                        ship.powerup_inv_index = ship.powerup_inv_index >= ship.max_powerups_inv - 1 ? 0 : ship.powerup_inv_index + 1;
                        Fab5_Game.inst().message("play_sound", new { name = "nextpowerup", volume = 1 });
                    }
                    if (is_gamepad_clicked(Buttons.Y, state, input.gamepadState) && ship.powerup_inv_index < ship.powerup_inv.Length)
                    {
                        ship.use_powerup(ship.powerup_inv_index);
                        Fab5_Game.inst().message("play_sound", new { name = "use_powerup", volume = 1 });
                    }

                    if (is_gamepad_clicked(Buttons.Start, state, input.gamepadState))
                    {
                        Fab5_Game.inst().message("start", null);
                    }
                    input.gamepadState = state;
                }

                if (Math.Abs(turn) > 0.01f)
                {
                    var ang_acc = 4.5f * dt;// @To-do: borde specas i shipinfo eller nåt

                    //angle.ang_vel += ang_acc * turn;
                    angle.angle += ang_acc * turn;
                }
                if (Math.Abs(input.throttle) > 0.01f)
                {
                    Fab5_Game.inst().message("play_sound", new { name = "thrust", gp_index = input.gp_index, pos = pos });

                    velocity.x += dt * (float)(Math.Cos(angle.angle)) * acc * input.throttle;
                    velocity.y += dt * (float)(Math.Sin(angle.angle)) * acc * input.throttle;

                    var speed = (float)Math.Sqrt(velocity.x * velocity.x + velocity.y * velocity.y);

                    if (speed > max_speed && !ship.has_powerup(typeof(Boost_Powerup)))
                    {
                        velocity.x = max_speed * (velocity.x / speed);
                        velocity.y = max_speed * (velocity.y / speed);
                    }
                }
                else
                {
                    Fab5_Game.inst().message("stop_sound", new { name = "thrust", gp_index = input.gp_index, pos = pos });
                }


                // Misc keys
                if (input.keyboardState.IsKeyDown(Keys.N))
                {
                    Fab5_Game.inst().message("change_song", null);
                }

                if (input.keyboardState.IsKeyDown(Keys.M))
                {
                    Fab5_Game.inst().message("play_sound", new { name = "mute" });
                }

                if (currentKeyboardState.IsKeyDown(Keys.LeftAlt) && is_key_clicked(Keys.Enter, currentKeyboardState, input.keyboardState))
                {
                    Fab5_Game.inst().message("fullscreen", null);
                }
                else if (is_key_clicked(Keys.Escape, currentKeyboardState, input.keyboardState))
                {
                    Fab5_Game.inst().message("start", null);
                }

                input.keyboardState = Keyboard.GetState();
            }
        }