Beispiel #1
0
        public override void Update(float elapsedSeconds)
        {
            foreach (Entity e in this.actives.Values)
            {
                TransformComponent transform = e.GetComponent <TransformComponent>();
                MotionComponent    motion    = e.GetComponent <MotionComponent>();
                TargetComponent    target    = e.GetComponent <TargetComponent>();

                System.Diagnostics.Debug.Assert(transform != null, "PositionComponent not found");
                System.Diagnostics.Debug.Assert(motion != null, "VelocityComponent not found");
                System.Diagnostics.Debug.Assert(target != null, "TargetComponent not found");

                if (EntityWorld.EntityManager.IsAlive(target.TargetId))
                {
                    TransformComponent targetTransform = this.EntityWorld.ComponentManager.GetComponent <TransformComponent>(target.TargetId);

                    if (targetTransform != null)
                    {
                        Vector2 delta = targetTransform.Position - transform.Position;
                        float   destinationDirection = VectorExtensions.Vector2ToAngle(delta);
                        int     accelFactor          = 10;
                        float   newRotation          = MathExtensions.LerpAngle(MathHelper.WrapAngle(transform.Rotation), MathHelper.WrapAngle(destinationDirection), /*0.1f*/ 2 * elapsedSeconds);
                        transform.Rotation = newRotation;
                        motion.AddAcceleration(VectorExtensions.AngleToVector2(transform.Rotation) * GameConfig.Projectile.Velocity * accelFactor);
                        //motion.AddAngularAcceleration((destinationDirection - transform.Rotation) * 0.5f);
                    }
                }
                //else
                //{
                //    //target lost
                //    motion.AddAcceleration(VectorExtensions.AngleToVector2(transform.Rotation) * GameConfig.Projectile.Velocity * accelFactor);
                //}
            }
        }
Beispiel #2
0
        public override void OnUpdate(float elapsedSeconds)
        {
            base.OnUpdate(elapsedSeconds);

            if (!this.poweringUp)
            {
                if (Kadro.Input.KeyboardInput.IsKeyDown(Keys.Left))
                {
                    this.Transform.Rotation -= this.resolutionsPerSecond * elapsedSeconds;
                    this.Transform.Position  = VectorExtensions.Rotate(this.Transform.Position, -this.resolutionsPerSecond * elapsedSeconds, focusPosition - offset);
                }

                if (Kadro.Input.KeyboardInput.IsKeyDown(Keys.Right))
                {
                    this.Transform.Rotation += this.resolutionsPerSecond * elapsedSeconds;
                    this.Transform.Position  = VectorExtensions.Rotate(this.Transform.Position, this.resolutionsPerSecond * elapsedSeconds, focusPosition - offset);
                }

                if (MouseInput.OnMouseMoved())
                {
                    float targetAngle = (GameScene.ActiveScene.ViewToWorld(MouseInput.PositionVector) - focusPosition + offset).PerpRight().ToPolar();
                    //targetAngle = MathExtensions.LerpAngle(this.Transform.Rotation, targetAngle, 1.0f * elapsedSeconds);
                    this.Transform.Rotation = targetAngle;
                    this.Transform.Position = VectorExtensions.Rotate(focusPosition, targetAngle, focusPosition - offset);
                }

                if (Kadro.Input.KeyboardInput.OnKeyDown(Keys.Space))
                {
                    this.poweringUp = true;

                    // add constant velocity to move back
                    RigidBody r = this.Components.Get <RigidBodyComponent>().RigidBody;
                    r.AddVelocityChange(VectorExtensions.AngleToVector2(this.Transform.Rotation).PerpLeft() * this.poweringUpVelocity * elapsedSeconds);
                }
            }

            if (this.poweringUp && Kadro.Input.KeyboardInput.IsKeyDown(Keys.Space))
            {
                this.timeSinceStart += elapsedSeconds;
            }

            if ((this.poweringUp && Kadro.Input.KeyboardInput.OnKeyUp(Keys.Space)) || this.timeSinceStart > this.releaseTime)
            {
                // release
                this.timeSinceStart = 0f;
                this.releasing      = true;
                RigidBody r = this.Components.Get <RigidBodyComponent>().RigidBody;
                r.Velocity        = Vector2.Zero;
                r.AngularVelocity = 0;
            }

            if (this.releasing)
            {
                // accelerate cue until it hits the ball
                RigidBody r = this.Components.Get <RigidBodyComponent>().RigidBody;
                r.AddForce(-VectorExtensions.AngleToVector2(this.Transform.Rotation).PerpLeft() * this.releaseForce * elapsedSeconds);
            }
        }
Beispiel #3
0
 public virtual void AddAsteroid(Entity e, Vector2 position, float rotation, Vector2 scale)
 {
     e.AddComponent(new TransformComponent(position, rotation, scale));
     e.AddComponent(new MotionComponent(VectorExtensions.AngleToVector2(rotation) * GameConfig.Asteroid.Velocity, ((rotation % 2 * Math.PI) - Math.PI) < 0 ? -GameConfig.Asteroid.AngularVelocity : +GameConfig.Asteroid.AngularVelocity, GameConfig.Asteroid.Velocity, GameConfig.Asteroid.AngularVelocity));
     e.AddComponent(new CollidableComponent(this.LayerManager.GetBit(GameConfig.Asteroid.CollisionLayer),
                                            this.LayerManager.GetBit(GameConfig.Spaceship.CollisionLayer) | this.LayerManager.GetBit(GameConfig.Projectile.CollisionLayer),
                                            new CircleCollider(GameConfig.Asteroid.Large * scale.X / 2f), e.EntityId));
     e.AddComponent(new TagComponent(GameConfig.Asteroid.Tag));
 }
        public override void Update(float elapsedSeconds)
        {
            foreach (Entity e in this.actives.Values)
            {
                TransformComponent    transform    = e.GetComponent <TransformComponent>();
                MotionComponent       motion       = e.GetComponent <MotionComponent>();
                AccelerationComponent acceleration = e.GetComponent <AccelerationComponent>();
                IntentComponent       intent       = e.GetComponent <IntentComponent>();

                System.Diagnostics.Debug.Assert(transform != null, "PositionComponent not found");
                System.Diagnostics.Debug.Assert(motion != null, "VelocityComponent not found");
                System.Diagnostics.Debug.Assert(acceleration != null, "MotionComponent not found");
                System.Diagnostics.Debug.Assert(intent != null, "IntentComponent not found");

                if (intent.IntentManager.HasIntent(Intent.Accelerate))
                {
                    // add force to center of object;
                    // TODO: add rocketengine/boostercomponent with offset from origin
                    motion.AddAcceleration(VectorExtensions.AngleToVector2(transform.Rotation) * acceleration.AccelerationFactor);
                }

                if (intent.IntentManager.HasIntent(Intent.Decelerate))
                {
                    motion.Drag = 0.05f;
                }
                else
                {
                    motion.Drag = 0.005f; //stop after a while
                }

                if (intent.IntentManager.HasIntent(Intent.RotateLeft))
                {
                    motion.AngularDrag = 0f;
                    motion.AddAngularAcceleration(-acceleration.RotationAccelerationFactor);
                }

                if (intent.IntentManager.HasIntent(Intent.RotateRight))
                {
                    motion.AngularDrag = 0f;
                    motion.AddAngularAcceleration(acceleration.RotationAccelerationFactor);
                }

                if (!(intent.IntentManager.HasIntent(Intent.RotateLeft) || intent.IntentManager.HasIntent(Intent.RotateRight)))
                {
                    motion.AngularDrag = 0.75f;
                }
            }
        }
Beispiel #5
0
 public virtual void AddMissile(Entity e, Vector2 position, float rotation)
 {
     e.AddComponent(new TransformComponent(position, rotation, Vector2.One));
     e.AddComponent(new MotionComponent(
                        VectorExtensions.AngleToVector2(rotation) * GameConfig.Projectile.Velocity,
                        0,
                        GameConfig.Projectile.Velocity,
                        GameConfig.Spaceship.MaxAngularVelocity));
     e.AddComponent(new TagComponent(GameConfig.Projectile.Tag));
     e.AddComponent(new CollidableComponent(this.LayerManager.GetBit(GameConfig.Projectile.CollisionLayer),
                                            this.LayerManager.GetBit(GameConfig.Asteroid.CollisionLayer),
                                            new CircleCollider(GameConfig.Projectile.Size / 2f), e.EntityId));
     e.AddComponent(new ParentComponent(0));
     e.AddComponent(new LifetimeComponent(GameConfig.Projectile.Lifetime * 10));
     e.AddComponent(new TargetComponent(0));
     // add explosioncomponent, that destroys asteroids in a small range after first impact
     // add animation, that shows the explosion
 }
Beispiel #6
0
        private Vector2[] CreateSpawnPoints(int playerCount)
        {
            Vector2[] spawnPoints = new Vector2[playerCount];
            Vector2   center      = GameConfig.PlayArea.Center.ToVector2();
            float     radius      = Math.Min(GameConfig.PlayArea.Size.X, GameConfig.PlayArea.Size.Y) / 3f;
            float     increment   = MathHelper.TwoPi / playerCount;
            float     theta       = (float)new Random().NextDouble() * MathHelper.TwoPi; //start at random angle

            if (spawnPoints.Length == 1)
            {
                spawnPoints[0] = center;
            }
            else
            {
                for (int i = 0; i < spawnPoints.Length; i++)
                {
                    spawnPoints[i] = center + radius * VectorExtensions.AngleToVector2(theta);
                    theta         += increment;
                }
            }

            return(spawnPoints);
        }
Beispiel #7
0
        public void Draw(float elapsedSeconds, SpriteBatch spriteBatch)
        {
            if (this.isDebug)
            {
                if (this.UseSpatialHashing)
                {
                    //note: test only
                    Kadro.Physics.PhysicsVisualizer.DrawSpatialGridCells(spriteBatch, this.spatialGrid.GetCells());

                    //todo: move this to another class (maybe even spatialgrid)
                    //for (int i = 0; i < GameConfig.NumCells.X; i++)
                    //{
                    //    spriteBatch.DrawLineSegment(new Vector2(i * GameConfig.CellSize.X, 0), new Vector2(i * GameConfig.CellSize.X, GameConfig.WorldSize.Y), Color.Blue, 1);
                    //}

                    //for (int i = 0; i < GameConfig.NumCells.Y; i++)
                    //{
                    //    spriteBatch.DrawLineSegment(new Vector2(0, i * GameConfig.CellSize.Y), new Vector2(GameConfig.WorldSize.X, i * GameConfig.CellSize.Y), Color.Blue, 1);
                    //}
                }

                for (int i = 0; i < this.targets.Count; i++)
                {
                    CollidableComponent collidable = this.targets[i].GetComponent <CollidableComponent>();
                    TransformComponent  transform  = this.targets[i].GetComponent <TransformComponent>();
                    System.Diagnostics.Debug.Assert(collidable != null, "CollidableComponent not found");
                    System.Diagnostics.Debug.Assert(transform != null, "TransformComponent not found");

                    collidable.Collider.ApplyTransform(transform.Position, transform.Rotation, transform.Scale);
                    collidable.DebugDraw(spriteBatch, 1);
                    spriteBatch.DrawLineSegment(transform.Position, transform.EntityToWorld(VectorExtensions.AngleToVector2(0f) * 20), Color.Green, 1);
                    spriteBatch.DrawLineSegment(transform.Position, transform.Position + Vector2.One, Color.Brown, 1);
                    //collidable.Collider.GetBounds().Draw(spriteBatch, Color.Violet, 1);
                }
            }
        }