Example #1
0
        public BonbonPickup()
        {
            BodyComponent = new BodyComponent(this)
            {
                InitMode = BodyComponentInitMode.Manual,
            };

            BodyComponent.BeforeInitialize += () =>
            {
            };
            this.RootComponent = BodyComponent;

            Animation = new SpriteAnimationComponent(this)
            {
                AnimationTypes = new List <SpriteAnimationType>(),
            };
            Animation.AttachTo(RootComponent);

            MoneyBag = new MoneyBagComponent(this)
            {
                InitialAmount = 10,
            };

            Homing = GameObjectFactory.CreateDefaultHomingCircle(this, BodyComponent,
                                                                 sensorRadius: 1.0f,
                                                                 homingType: HomingType.ConstantSpeed,
                                                                 homingSpeed: 3.0f);

            Pickup = new PickupComponent(this)
            {
                BodyComponent = BodyComponent,
            };
        }
Example #2
0
        public KeyPickup()
        {
            BodyComponent = new BodyComponent(this)
            {
                InitMode = BodyComponentInitMode.Manual,
            };
            this.RootComponent = BodyComponent;

            Animation = new SpriteAnimationComponent(this)
            {
                AnimationTypes = new List <SpriteAnimationType>(),
            };
            Animation.AttachTo(RootComponent);

            KeyRing = new KeyRingComponent(this);

            Pickup = new PickupComponent(this)
            {
                BodyComponent = BodyComponent,
            };

            Homing = GameObjectFactory.CreateDefaultHomingCircle(this, BodyComponent,
                                                                 sensorRadius: 1.0f,
                                                                 homingType: HomingType.ConstantSpeed,
                                                                 homingSpeed: 3.0f);
        }
Example #3
0
        public static HomingComponent CreateDefaultHomingCircle(
            GameObject owner,
            BodyComponent bodyComponentToMove,
            float sensorRadius,
            HomingType homingType,
            float homingSpeed)
        {
            var tsc = new TargetSensorComponent(owner)
            {
                TargetCollisionCategories = CollisionCategory.Owliver,
                SensorType         = TargetSensorType.Circle,
                CircleSensorRadius = sensorRadius,
            };

            tsc.AttachTo(bodyComponentToMove);

            var hoc = new HomingComponent(owner)
            {
                BodyComponentToMove = bodyComponentToMove,
                TargetSensor        = tsc,
                Speed      = homingSpeed,
                HomingType = homingType,

                DebugDrawingEnabled = true,
            };

            hoc.AttachTo(bodyComponentToMove);

            return(hoc);
        }
Example #4
0
        public override void Initialize()
        {
            base.Initialize();

            if (BodyComponent == null)
            {
                BodyComponent = Owner.GetComponent <BodyComponent>();
                Debug.Assert(BodyComponent != null);
            }

            if (Chaser == null)
            {
                Chaser = Owner.GetComponent <HomingComponent>();
            }

            if (Health == null)
            {
                Health = Owner.GetComponent <HealthComponent>();
                Debug.Assert(Health != null);
            }

            if (Animation == null)
            {
                Animation = Owner.GetComponent <SpriteAnimationComponent>();
                Debug.Assert(Animation != null);
            }
        }
Example #5
0
        public Slurp()
        {
            BodyComponent = new BodyComponent(this)
            {
                InitMode = BodyComponentInitMode.Manual,
            };
            RootComponent = BodyComponent;

            Animation = new SpriteAnimationComponent(this)
            {
                AnimationTypes = new List <SpriteAnimationType>
                {
                    SpriteAnimationType.Slurp_Idle_Left,
                    SpriteAnimationType.Slurp_Idle_Right,
                },
            };
            Animation.AttachTo(BodyComponent);

            Health = GameObjectFactory.CreateDefaultHealth(this,
                                                           maxHealth: 3,
                                                           hitDuration: HitDuration,
                                                           deathParticleTimeToLive: TimeSpan.FromSeconds(1));

            HealthDisplay = new HealthDisplayComponent(this)
            {
                Health = Health,
                InitialDisplayOrigin = HealthDisplayComponent.DisplayOrigin.Bottom,
                HealthIcon           = SpriteAnimationFactory.CreateAnimationInstance(SpriteAnimationType.Cross),
            };
            HealthDisplay.AttachTo(Animation);

            GameObjectFactory.CreateOnHitSquasher(this, Health, Animation).SetDefaultCurves(HitDuration);

            GameObjectFactory.CreateOnHitBlinkingSequence(this, Health, Animation).SetDefaultCurves(HitDuration);

            Homing = GameObjectFactory.CreateDefaultHomingCircle(this, BodyComponent,
                                                                 sensorRadius: 3.0f,
                                                                 homingType: HomingType.ConstantSpeed,
                                                                 homingSpeed: 0.5f);
        }
Example #6
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);
                });
            }
        }