Esempio n. 1
0
        public static void SpawnGameObjectsInRingFormation(Vector2 center, float radius, int numToSpawn, Random rand,
                                                           params KnownGameObject[] types)
        {
            Vector2 localPosition = new Vector2(radius, 0.0f);
            Angle   angle         = new Angle {
                Radians = MathHelper.TwoPi / numToSpawn
            };

            for (int bonbonIndex = 0; bonbonIndex < numToSpawn; bonbonIndex++)
            {
                KnownGameObject type = rand.Choose(types);
                GameObject      go   = GameObjectFactory.CreateKnown(type);
                go.SetWorldPosition(center + localPosition);
                Game.AddGameObject(go);

                localPosition = localPosition.GetRotated(angle);
            }
        }
Esempio n. 2
0
        public void LoadContent(Level level)
        {
            string groundTextureName    = string.Format(level.ContentNameFormat_Ground, GridPosition.Y, GridPosition.X);
            string collisionContentName = string.Format(level.ContentNameFormat_Collision, GridPosition.Y, GridPosition.X);
            string layoutContentName    = string.Format(level.ContentNameFormat_Layout, GridPosition.Y, GridPosition.X);

            //
            // Screen game object
            //
            {
                _screenGameObject = new BackgroundScreen();
                _screenGameObject.Spatial.Position        += WorldPosition;
                _screenGameObject.ShapeContentName         = collisionContentName;
                _screenGameObject.Sprite.SpriteContentName = groundTextureName;

                Global.Game.AddGameObject(_screenGameObject);
            }

            List <ScreenLayoutInfo> layoutInfos = Global.Game.Content.Load <List <ScreenLayoutInfo> >(layoutContentName);

            foreach (ScreenLayoutInfo layout in layoutInfos)
            {
                var deco = GameObjectFactory.CreateKnown(layout.ObjectType);

#if false
                SpriteAnimationComponent sa = deco.GetComponent <SpriteAnimationComponent>();
                // Choose random initial frame
                sa.OnPostInitialize += delegate()
                {
                    int startFrame = level.Random.Next(3);
                    for (int frameIndex = 0; frameIndex < startFrame; frameIndex++)
                    {
                        sa.ActiveAnimation.AdvanceFrameIndex();
                    }
                };
#endif
                deco.Spatial.Position += layout.OffsetInMeters;
                deco.AttachTo(_screenGameObject);

                Global.Game.AddGameObject(deco);

                _decorationObjects.Add(deco);
            }
        }
Esempio n. 3
0
        public override void Update(float deltaSeconds)
        {
            base.Update(deltaSeconds);

            Vector2 dp            = MyBody.LinearVelocity;
            float   movementSpeed = dp.Length();

            OwliverState newState = CurrentState;

            const float movementChangeThreshold = 0.01f;

            switch (CurrentState.MovementMode)
            {
            case OwliverMovementMode.Idle:
            {
                if (movementSpeed >= movementChangeThreshold)
                {
                    newState.MovementMode = OwliverMovementMode.Walking;
                }
            }
            break;

            case OwliverMovementMode.Walking:
            {
                if (movementSpeed < movementChangeThreshold)
                {
                    newState.MovementMode = OwliverMovementMode.Idle;
                }
            }
            break;
            }

            const float facingChangeThreshold = 0.01f;

            if (CurrentState.FacingDirection == OwliverFacingDirection.Left && dp.X > facingChangeThreshold)
            {
                newState.FacingDirection = OwliverFacingDirection.Right;
            }
            else if (CurrentState.FacingDirection == OwliverFacingDirection.Right && dp.X < -facingChangeThreshold)
            {
                newState.FacingDirection = OwliverFacingDirection.Left;
            }

            GameInput input = ConsumeInput();

            if (input.WantsAttack)
            {
                newState.MovementMode = OwliverMovementMode.Attacking;
            }

            if (input.WantsInteraction)
            {
                foreach (ShopItem shopItem in ConnectedShopItems)
                {
                    bool purchase          = false;
                    bool removeIfPurchased = true;

                    int price = shopItem.PriceValue;
                    if (MoneyBag.CurrentAmount >= price)
                    {
                        switch (shopItem.ItemType)
                        {
                        case ShopItemType.FruitBowl:
                        {
                            purchase          = true;
                            removeIfPurchased = false;
                            Health.Heal(int.MaxValue);
                        }
                        break;

                        case ShopItemType.FishingRod:
                        {
                            if (CurrentState.WeaponType != OwliverWeaponType.FishingRod)
                            {
                                purchase            = true;
                                newState.WeaponType = OwliverWeaponType.FishingRod;
                                ChangeState(ref newState);

                                var newGo = GameObjectFactory.CreateKnown(KnownGameObject.ShopItem_Stick);
                                newGo.Spatial.CopyFrom(shopItem.Spatial);
                                Global.Game.AddGameObject(newGo);
                            }
                        }
                        break;

                        case ShopItemType.Stick:
                        {
                            if (CurrentState.WeaponType != OwliverWeaponType.Stick)
                            {
                                purchase            = true;
                                newState.WeaponType = OwliverWeaponType.Stick;
                                ChangeState(ref newState);

                                var newGo = GameObjectFactory.CreateKnown(KnownGameObject.ShopItem_FishingRod);
                                newGo.Spatial.CopyFrom(shopItem.Spatial);
                                Global.Game.AddGameObject(newGo);
                            }
                        }
                        break;

                        default: throw new InvalidProgramException("Invalid item type.");
                        }
                    }

                    if (purchase)
                    {
                        MoneyBag.CurrentAmount -= price;
                        if (removeIfPurchased)
                        {
                            Global.Game.RemoveGameObject(shopItem);
                        }
                    }
                }

                foreach (ShopItem shopItem in ConnectedShopItems)
                {
                    shopItem.IsAffordable = MoneyBag.CurrentAmount >= shopItem.PriceValue;
                }
            }

            ChangeState(ref newState);

            AABB weaponAABB = WeaponAABB;

            if (input.WantsAttack && CurrentState.MovementMode == OwliverMovementMode.Attacking)
            {
                int            damage   = Damage;
                float          force    = 0.1f * damage;
                List <Fixture> fixtures = Global.Game.World.QueryAABB(ref weaponAABB);
                foreach (Body hitBody in fixtures.Where(f => f.CollisionCategories.HasFlag(CollisionCategory.Enemy))
                         .Select(f => f.Body)
                         .Distinct())
                {
                    Global.HandleDefaultHit(hitBody, MyBody.Position, damage, force);
                }

                bool throwProjectiles = true;
                if (throwProjectiles)
                {
                    float sign  = CurrentState.FacingDirection == OwliverFacingDirection.Left ? -1.0f : 1.0f;
                    float speed = 8.0f;

                    Projectile projectile = new Projectile()
                    {
                        MaxSpeed            = speed,
                        CollisionCategories = CollisionCategory.FriendlyWeapon,
                        CollidesWith        = CollisionCategory.World | CollisionCategory.AnyEnemy,
                    };
                    projectile.Spatial.CopyFrom(new SpatialData {
                        Position = WeaponAABB.Center
                    });
                    projectile.Spatial.Position.X           += sign * 0.1f;
                    projectile.AutoDestruct.DestructionDelay = TimeSpan.FromSeconds(2.0f);

                    Vector2 velocity = sign * new Vector2(speed, 0.0f);

#if false
                    var hoc = new HomingComponent(projectile)
                    {
                        Target      = Global.Game.GameObjects.Where(go => go.GetComponent <TanktonComponent>() != null).FirstOrDefault(),
                        HomingType  = HomingType.ConstantAcceleration,
                        TargetRange = 1.0f,
                        Speed       = velocity.Length() * 10,
                    };
                    hoc.AttachTo(projectile);
#endif

                    projectile.BodyComponent.BeforePostInitialize += () =>
                    {
                        Body    body    = projectile.BodyComponent.Body;
                        Vector2 impulse = body.Mass * velocity;
                        body.ApplyLinearImpulse(ref impulse);
                    };
                    Global.Game.AddGameObject(projectile);
                }
            }
            else
            {
                if (!Health.IsInvincible)
                {
                    Vector2 movementVector = Movement.ConsumeMovementVector();
                    Movement.PerformMovement(movementVector, deltaSeconds);
                }
            }

            // Debug drawing.
            {
                Color color = CurrentState.MovementMode == OwliverMovementMode.Attacking ? Color.Navy : Color.Gray;
                Global.Game.DebugDrawCommands.Add(view =>
                {
                    view.DrawAABB(ref weaponAABB, color);
                });
            }
        }
Esempio n. 4
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            WorldRenderer.Initialize(GraphicsDevice);
            UIRenderer.Initialize(GraphicsDevice);

            PhysicsDebugView.LoadContent(GraphicsDevice, Content);

            CurrentLevel = new Level(Content)
            {
                ContentNameFormat_Ground    = "level01/level1_ground_{0}{1}",
                ContentNameFormat_Collision = "level01/static_collision/static_collision_{0}{1}",
                ContentNameFormat_Layout    = "level01/layout/layout_{0}{1}",
            };

            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 7; y++)
                {
                    CurrentLevel.CreateScreen(x, y);
                }
            }

            {
                Owliver = new Owliver();
                Owliver.Spatial.Position += Conversion.ToMeters(450, 600);
                AddGameObject(Owliver);

                CurrentLevel.CullingCenter = Owliver;
            }

            CurrentLevel.LoadContent();

            {
                ActiveCamera = new CameraObject();

                ActiveCamera.CameraComponent.VisibilityBounds = CurrentLevel.LevelBounds;
                ActiveCamera.CameraComponent.OnGraphicsDeviceReset(GraphicsDevice);
                Window.ClientSizeChanged += (o, e) =>
                {
                    ActiveCamera.CameraComponent.OnGraphicsDeviceReset(GraphicsDevice);
                };

                ActiveCamera.SpringArm.BeforeInitialize += () =>
                {
                    ActiveCamera.SpringArm.Target = Owliver;
                };

                AddGameObject(ActiveCamera);
            }

#if true
            {
                var testSlurp = GameObjectFactory.CreateKnown(KnownGameObject.Slurp);
                testSlurp.Spatial.Position += Conversion.ToMeters(300, 250);
                AddGameObject(testSlurp);
            }

            {
                var testTankton = new Tankton();
                testTankton.Spatial.Position += Conversion.ToMeters(900, 350);
                AddGameObject(testTankton);
            }

            {
                Random rand = new Random();

                Global.SpawnGameObjectsInRingFormation(
                    center: Conversion.ToMeters(2400, 500),
                    radius: 2.5f,
                    numToSpawn: 15,
                    rand: rand,
                    types: new[] { KnownGameObject.Bonbon_Gold, KnownGameObject.Bonbon_Red });

                Global.SpawnGameObjectsInRingFormation(
                    center: Conversion.ToMeters(2400, 500),
                    radius: 1.0f,
                    numToSpawn: 5,
                    rand: rand,
                    types: new[] { KnownGameObject.Bonbon_Gold, KnownGameObject.Bonbon_Red });
            }

            {
                var testKey = new KeyPickup()
                {
                    KeyType = KeyType.Gold
                };
                testKey.Spatial.Position += Conversion.ToMeters(700, 720);
                AddGameObject(testKey);
            }

            {
                var testGate = new Gate();
                testGate.Spatial.Position += Conversion.ToMeters(2300, 1100);
                AddGameObject(testGate);
            }

            {
                var testShop = new Shop();
                testShop.Spatial.Position += Conversion.ToMeters(1300, 500);
                AddGameObject(testShop);

                var testFruitBowl = new ShopItem()
                {
                    ItemType = ShopItemType.FruitBowl
                };
                testFruitBowl.Price = ShopItemPriceType._20;
                testFruitBowl.AttachTo(testShop);
                testFruitBowl.Spatial.Position += Conversion.ToMeters(-110.0f, -90.0f);
                AddGameObject(testFruitBowl);

                var testRod = new ShopItem()
                {
                    ItemType = ShopItemType.FishingRod
                };
                testRod.Price = ShopItemPriceType._100;
                testRod.AttachTo(testShop);
                testRod.Spatial.Position += Conversion.ToMeters(128.0f, -80.0f);
                AddGameObject(testRod);
            }

            {
                var testSinger = new Singer();
                testSinger.Spatial.Position += Conversion.ToMeters(2600, 300);
                AddGameObject(testSinger);
            }

            {
                var testTrap = new SpikeTrap()
                {
                    Orientation = SpikeTrapOrientation.Vertical,
                    SensorReach = 4.0f,
                };
                testTrap.Spatial.Position += Conversion.ToMeters(1600, 500);
                AddGameObject(testTrap);
            }
#endif

            {
                var BackgroundMusic = Content.Load <Song>("snd/FiluAndDina_-_Video_Game_Background_-_Edit");
                MediaPlayer.IsRepeating = true;
#if false
                MediaPlayer.Play(BackgroundMusic);
#endif
            }

            Hud.Initialize();
            Hud.ResetLayout(GraphicsDevice.Viewport.Bounds);
            Window.ClientSizeChanged += (o, e) =>
            {
                Hud.ResetLayout(GraphicsDevice.Viewport.Bounds);
            };
        }