示例#1
0
        private void OnAnimationLoopFinished(SpriteAnimationType animType, SpriteAnimationPlaybackState oldPlaybackState, SpriteAnimationPlaybackState newPlaybackState)
        {
            bool isAttackAnimation = _attackAnimations.Contains(animType);

            if (isAttackAnimation)
            {
                if (oldPlaybackState == SpriteAnimationPlaybackState.Playing && newPlaybackState != SpriteAnimationPlaybackState.Playing)
                {
                    OwliverState newState = CurrentState;
                    newState.MovementMode = OwliverMovementMode.Walking;
                    ChangeState(ref newState);
                }
            }
        }
示例#2
0
        public void ChangeState(ref OwliverState newState)
        {
            OwliverState oldState = CurrentState;

            CurrentState = newState;

            OnStateChanged?.Invoke(oldState, newState);

            bool transferAnimationState = oldState.MovementMode == newState.MovementMode &&
                                          oldState.FacingDirection != newState.FacingDirection;

            SpriteAnimationType animType = GetAnimationType(ref newState);

            if (Animation.ChangeActiveAnimation(animType))
            {
                Console.WriteLine();
            }
        }
示例#3
0
 public static SpriteAnimationType GetAnimationType(ref OwliverState state)
 {
     return(GetAnimationType(state.MovementMode, state.FacingDirection, state.WeaponType));
 }
示例#4
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);
                });
            }
        }