コード例 #1
0
ファイル: Agent.cs プロジェクト: n1tros/Chocky
    private void Awake()
    {
        _body        = new AgentBody();
        _physics     = new AgentPhysics(transform, GetComponent <Rigidbody2D>(), this);
        _state       = new StateMachine();
        _animation   = new AgentAnimation(this, GetComponent <SkeletonAnimation>());
        _groundcheck = GetComponent <GroundCheck>();
        _stats       = new AgentStats(this);
        _agentSkills = GetComponent <AgentSkills>();
        _health      = GetComponent <AgentHealth>();
        _state.ChangeState(new GroundedState(this));

        if (tag == "Player")
        {
            DeathManager.Player = this;
        }
        else
        {
            if (DeathManager.Enemies == null)
            {
                DeathManager.Enemies = new List <Agent>();
            }
            DeathManager.Enemies.Add(this);
        }
    }
コード例 #2
0
        public override async Task Execute()
        {
            spriteSheet          = BulletSheet;
            agentSpriteComponent = Entity.Get <SpriteComponent>();
            var animComponent = Entity.Get <AnimationComponent>();
            PlayingAnimation playingAnimation = null;

            // Calculate offset of the bullet from the Agent if he is facing left and right side // TODO improve this
            var bulletOffset = new Vector3(1.3f, 1.65f, 0f);

            // Initialize game entities
            if (!IsLiveReloading)
            {
                shootDelayCounter     = 0f;
                isAgentFacingRight    = true;
                currentAgentAnimation = AgentAnimation.Idle;
            }
            CurrentAgentAnimation = currentAgentAnimation;

            var normalScaleX = Entity.Transform.Scale.X;

            var bulletCS = BulletColliderShape;

            Task animTask = null;

            while (Game.IsRunning)
            {
                await Script.NextFrame();

                var inputState = GetKeyboardInputState();

                if (inputState == InputState.None)
                {
                    inputState = GetPointerInputState();
                }

                if (inputState == InputState.RunLeft || inputState == InputState.RunRight)
                {
                    // Update Agent's position
                    var dt = (float)Game.UpdateTime.Elapsed.TotalSeconds;

                    Entity.Transform.Position.X += ((inputState == InputState.RunRight) ? AgentMoveDistance : -AgentMoveDistance) * dt;

                    if (Entity.Transform.Position.X < -gameWidthHalfX)
                    {
                        Entity.Transform.Position.X = -gameWidthHalfX;
                    }

                    if (Entity.Transform.Position.X > gameWidthHalfX)
                    {
                        Entity.Transform.Position.X = gameWidthHalfX;
                    }

                    isAgentFacingRight = inputState == InputState.RunRight;

                    // If agent face left, flip the sprite
                    Entity.Transform.Scale.X = isAgentFacingRight ? normalScaleX : -normalScaleX;

                    // Update the sprite animation and state
                    CurrentAgentAnimation = AgentAnimation.Run;
                    if (playingAnimation == null || playingAnimation.Name != "Run")
                    {
                        playingAnimation = animComponent.Play("Run");
                    }
                }
                else if (inputState == InputState.Shoot)
                {
                    if (animTask != null && !animTask.IsCompleted)
                    {
                        continue;
                    }
                    if (animTask != null && animTask.IsCompleted)
                    {
                        playingAnimation = null;
                    }

                    animTask = null;

                    var rb = new RigidbodyComponent {
                        CanCollideWith = CollisionFilterGroupFlags.CustomFilter1, CollisionGroup = CollisionFilterGroups.DefaultFilter
                    };
                    rb.ColliderShapes.Add(new ColliderShapeAssetDesc {
                        Shape = bulletCS
                    });

                    // Spawns a new bullet
                    var bullet = new Entity
                    {
                        new SpriteComponent {
                            SpriteProvider = SpriteFromSheet.Create(spriteSheet, "bullet")
                        },
                        rb,
                        new BeamScript()
                    };
                    bullet.Name = "bullet";

                    bullet.Transform.Position = (isAgentFacingRight) ? Entity.Transform.Position + bulletOffset : Entity.Transform.Position + (bulletOffset * new Vector3(-1, 1, 1));
                    bullet.Transform.UpdateWorldMatrix();

                    SceneSystem.SceneInstance.RootScene.Entities.Add(bullet);

                    rb.LinearFactor  = new Vector3(1, 0, 0);
                    rb.AngularFactor = new Vector3(0, 0, 0);
                    rb.ApplyImpulse(isAgentFacingRight ? new Vector3(25, 0, 0) : new Vector3(-25, 0, 0));

                    // Start animation for shooting
                    CurrentAgentAnimation = AgentAnimation.Shoot;
                    if (playingAnimation == null || playingAnimation.Name != "Attack")
                    {
                        playingAnimation = animComponent.Play("Attack");
                        animTask         = animComponent.Ended(playingAnimation);
                    }
                }
                else
                {
                    CurrentAgentAnimation = AgentAnimation.Idle;
                    if (playingAnimation == null || playingAnimation.Name != "Stance")
                    {
                        playingAnimation = animComponent.Play("Stance");
                    }
                }
            }
        }
コード例 #3
0
        public override async Task Execute()
        {
            spriteSheet          = SpriteSheet;
            agentSpriteComponent = Entity.Get <SpriteComponent>();

            // Calculate offset of the bullet from the Agent if he is facing left and right side // TODO improve this
            var bulletOffset = new Vector3(1f, 0.2f, 0f);

            // Initialize game entities
            if (!IsLiveReloading)
            {
                shootDelayCounter     = 0f;
                isAgentFacingRight    = true;
                currentAgentAnimation = AgentAnimation.Idle;
            }
            CurrentAgentAnimation = currentAgentAnimation;

            while (Game.IsRunning)
            {
                await Script.NextFrame();

                var inputState = GetKeyboardInputState();

                if (inputState == InputState.None)
                {
                    inputState = GetPointerInputState();
                }

                // Reset the shoot delay, if state changes
                if (inputState != InputState.Shoot && CurrentAgentAnimation == AgentAnimation.Shoot)
                {
                    shootDelayCounter = 0;
                }

                if (inputState == InputState.RunLeft || inputState == InputState.RunRight)
                {
                    // Update Agent's position
                    var dt = (float)Game.UpdateTime.Elapsed.TotalSeconds;

                    Entity.Transform.Position.X += ((inputState == InputState.RunRight) ? AgentMoveDistance : -AgentMoveDistance) * dt;

                    if (Entity.Transform.Position.X < -gameWidthHalfX)
                    {
                        Entity.Transform.Position.X = -gameWidthHalfX;
                    }

                    if (Entity.Transform.Position.X > gameWidthHalfX)
                    {
                        Entity.Transform.Position.X = gameWidthHalfX;
                    }

                    isAgentFacingRight = inputState == InputState.RunRight;

                    // If agent face left, flip the sprite
                    Entity.Transform.Scale.X = isAgentFacingRight ? 1f : -1f;

                    // Update the sprite animation and state
                    CurrentAgentAnimation = AgentAnimation.Run;
                }
                else if (inputState == InputState.Shoot)
                {
                    // Update shootDelayCounter, and check whether it is time to create a new bullet
                    shootDelayCounter -= (float)Game.UpdateTime.Elapsed.TotalSeconds;

                    if (shootDelayCounter > 0)
                    {
                        continue;
                    }


                    // Reset shoot delay
                    shootDelayCounter = AgentShootDelay;

                    // Spawns a new bullet
                    var bullet = new Entity
                    {
                        new SpriteComponent {
                            SpriteProvider = SpriteFromSheet.Create(spriteSheet, "bullet")
                        },

                        // Will make the beam move along a direction at each frame
                        new BeamScript {
                            DirectionX = isAgentFacingRight ? 1f : -1f, SpriteSheet = SpriteSheet
                        },
                    };

                    bullet.Transform.Position = (isAgentFacingRight) ? Entity.Transform.Position + bulletOffset : Entity.Transform.Position + (bulletOffset * new Vector3(-1, 1, 1));

                    SceneSystem.SceneInstance.Scene.Entities.Add(bullet);
                    Logic.WatchBullet(bullet);

                    // Start animation for shooting
                    CurrentAgentAnimation = AgentAnimation.Shoot;
                }
                else
                {
                    CurrentAgentAnimation = AgentAnimation.Idle;
                }
            }
        }
コード例 #4
0
        public override async Task Execute()
        {
            spriteSheet = SpriteSheet;
            agentSpriteComponent = Entity.Get<SpriteComponent>();

            // Calculate offset of the bullet from the Agent if he is facing left and right side // TODO improve this
            var bulletOffset = new Vector3(1f, 0.2f, 0f);

            // Initialize game entities
            if(!IsLiveReloading)
            {
                shootDelayCounter = 0f;
                isAgentFacingRight = true;
                currentAgentAnimation = AgentAnimation.Idle;
            }
            CurrentAgentAnimation = currentAgentAnimation;

            while (Game.IsRunning)
            {
                await Script.NextFrame();

                var inputState = GetKeyboardInputState();

                if (inputState == InputState.None)
                    inputState = GetPointerInputState();

                // Reset the shoot delay, if state changes
                if (inputState != InputState.Shoot && CurrentAgentAnimation == AgentAnimation.Shoot)
                    shootDelayCounter = 0;

                if (inputState == InputState.RunLeft || inputState == InputState.RunRight)
                {
                    // Update Agent's position
                    var dt = (float) Game.UpdateTime.Elapsed.TotalSeconds;

                    Entity.Transform.Position.X += ((inputState == InputState.RunRight) ? AgentMoveDistance : -AgentMoveDistance)*dt;

                    if (Entity.Transform.Position.X < -gameWidthHalfX)
                        Entity.Transform.Position.X = -gameWidthHalfX;

                    if (Entity.Transform.Position.X > gameWidthHalfX)
                        Entity.Transform.Position.X = gameWidthHalfX;

                    isAgentFacingRight = inputState == InputState.RunRight;

                    // If agent face left, flip the sprite
                    Entity.Transform.Scale.X = isAgentFacingRight ? 1f : -1f;

                    // Update the sprite animation and state
                    CurrentAgentAnimation = AgentAnimation.Run;
                }
                else if (inputState == InputState.Shoot)
                {
                    // Update shootDelayCounter, and check whether it is time to create a new bullet
                    shootDelayCounter -= (float) Game.UpdateTime.Elapsed.TotalSeconds;

                    if (shootDelayCounter > 0)
                        continue;


                    // Reset shoot delay
                    shootDelayCounter = AgentShootDelay;

                    // Spawns a new bullet
                    var bullet = new Entity
                    {
                        new SpriteComponent { SpriteProvider = SpriteFromSheet.Create(spriteSheet, "bullet") },

                        // Will make the beam move along a direction at each frame
                        new BeamScript {DirectionX = isAgentFacingRight ? 1f : -1f, SpriteSheet = SpriteSheet},
                    };

                    bullet.Transform.Position = (isAgentFacingRight) ? Entity.Transform.Position + bulletOffset : Entity.Transform.Position + (bulletOffset*new Vector3(-1, 1, 1));

                    SceneSystem.SceneInstance.Scene.Entities.Add(bullet);
                    Logic.WatchBullet(bullet);

                    // Start animation for shooting
                    CurrentAgentAnimation = AgentAnimation.Shoot;
                }
                else
                {
                    CurrentAgentAnimation = AgentAnimation.Idle;
                }
            }
        }
コード例 #5
0
        public override async Task Execute()
        {
            spriteSheet = BulletSheet;
            agentSpriteComponent = Entity.Get<SpriteComponent>();
            var animComponent = Entity.Get<AnimationComponent>();
            PlayingAnimation playingAnimation = null;

            // Calculate offset of the bullet from the Agent if he is facing left and right side // TODO improve this
            var bulletOffset = new Vector3(1.3f, 1.65f, 0f);

            // Initialize game entities
            if (!IsLiveReloading)
            {
                shootDelayCounter = 0f;
                isAgentFacingRight = true;
                currentAgentAnimation = AgentAnimation.Idle;
            }
            CurrentAgentAnimation = currentAgentAnimation;

            var normalScaleX = Entity.Transform.Scale.X;

            var bulletCS = BulletColliderShape;

            Task animTask = null;

            while (Game.IsRunning)
            {
                await Script.NextFrame();

                var inputState = GetKeyboardInputState();

                if (inputState == InputState.None)
                    inputState = GetPointerInputState();

                if (inputState == InputState.RunLeft || inputState == InputState.RunRight)
                {
                    // Update Agent's position
                    var dt = (float)Game.UpdateTime.Elapsed.TotalSeconds;

                    Entity.Transform.Position.X += ((inputState == InputState.RunRight) ? AgentMoveDistance : -AgentMoveDistance) * dt;

                    if (Entity.Transform.Position.X < -gameWidthHalfX)
                        Entity.Transform.Position.X = -gameWidthHalfX;

                    if (Entity.Transform.Position.X > gameWidthHalfX)
                        Entity.Transform.Position.X = gameWidthHalfX;

                    isAgentFacingRight = inputState == InputState.RunRight;

                    // If agent face left, flip the sprite
                    Entity.Transform.Scale.X = isAgentFacingRight ? normalScaleX : -normalScaleX;

                    // Update the sprite animation and state
                    CurrentAgentAnimation = AgentAnimation.Run;
                    if (playingAnimation == null || playingAnimation.Name != "Run")
                    {
                        playingAnimation = animComponent.Play("Run");
                    }
                }
                else if (inputState == InputState.Shoot)
                {
                    if(animTask != null && !animTask.IsCompleted) continue;
                    if (animTask != null && animTask.IsCompleted) playingAnimation = null;

                    animTask = null;

                    var rb = new RigidbodyComponent { CanCollideWith = CollisionFilterGroupFlags.CustomFilter1, CollisionGroup = CollisionFilterGroups.DefaultFilter };
                    rb.ColliderShapes.Add(new ColliderShapeAssetDesc { Shape = bulletCS });

                    // Spawns a new bullet
                    var bullet = new Entity
                    {
                        new SpriteComponent { SpriteProvider = SpriteFromSheet.Create(spriteSheet, "bullet") },
                        rb,
                        new BeamScript()
                    };
                    bullet.Name = "bullet";

                    bullet.Transform.Position = (isAgentFacingRight) ? Entity.Transform.Position + bulletOffset : Entity.Transform.Position + (bulletOffset * new Vector3(-1, 1, 1));
                    bullet.Transform.UpdateWorldMatrix();

                    SceneSystem.SceneInstance.Scene.Entities.Add(bullet);

                    rb.LinearFactor = new Vector3(1, 0, 0);
                    rb.AngularFactor = new Vector3(0, 0, 0);
                    rb.ApplyImpulse(isAgentFacingRight ? new Vector3(25, 0, 0) : new Vector3(-25, 0, 0));

                    // Start animation for shooting
                    CurrentAgentAnimation = AgentAnimation.Shoot;
                    if (playingAnimation == null || playingAnimation.Name != "Attack")
                    {
                        playingAnimation = animComponent.Play("Attack");
                        animTask = playingAnimation.Ended();
                    }
                }
                else
                {
                    CurrentAgentAnimation = AgentAnimation.Idle;
                    if (playingAnimation == null || playingAnimation.Name != "Stance")
                    {
                        playingAnimation = animComponent.Play("Stance");
                    }
                }
            }
        }