Ejemplo n.º 1
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Console.WriteLine("@Game.Initialize");

            GameService.AddService <GraphicsDevice>(GraphicsDevice);
            GameService.AddService <ContentManager>(Content);

            Components.Add(worldController      = new WorldController(this));
            Components.Add(playerController     = new PlayerController(this));
            Components.Add(enemyController      = new EnemyController(this));
            Components.Add(projectileController = new ProjectileController(this));

            GameService.AddService <ProjectileController>(projectileController);

            var player1 = playerController.Create(Input.Default());
            var world   = worldController.Create();

            player1.Spawn(world.spawnPoint);

            if (true)
            {
                // Spawn another player.
                var player2 = playerController.Create(Input.Alternative());
                player2.Spawn(world.spawnPoint);
            }

            if (true)
            {
                // Spawn the enemy.
                for (int i = 0; i < 5; i++)
                {
                    var enemy = enemyController.Create();
                    enemy.Spawn(world.spawnPoint);
                }
            }

            if (true)
            {
                // Add collision checks.
                collisions = new Collisions(this);
                collisions.playerController     = playerController;
                collisions.enemyController      = enemyController;
                collisions.projectileController = projectileController;
                Components.Add(collisions);
            }

            Cursor cursor = new Cursor(player1);
            Camera camera = new Camera(player1);

            GameService.AddService <Camera>(camera);
            GameService.AddService <Cursor>(cursor);
            GameService.AddService <World>(world);

            base.Initialize();
        }
Ejemplo n.º 2
0
        public void HandleActions()
        {
            ProjectileController projectileController = GameService.GetService <ProjectileController>();
            Cursor cursor = GameService.GetService <Cursor>();

            foreach (Player player in players)
            {
                if (Keyboard.GetState().IsKeyDown(player.input.Shoot))
                {
                    Vector2    spawnPoint  = player.Position;
                    Vector2    targetPoint = cursor.Position;
                    Projectile projectile  = new Projectile(game, player);
                    projectileController.Add(projectile);
                    projectile.Spawn(spawnPoint);
                    projectile.Shoot(targetPoint);
                }
            }
        }