private bool CheckAgeAndRespawn(Entity e) { RespawnComponent respawn = e.GetComponent <RespawnComponent>(); LifeComponent life = e.GetComponent <LifeComponent>(); if (respawn != null && life != null) { if (respawn.TimeSinceRespawn >= respawn.Timeout) { respawn.TimeSinceRespawn = 0; e.GetComponent <IntentComponent>().IntentManager.Initialize(); TransformComponent position = e.GetComponent <TransformComponent>(); MotionComponent motion = e.GetComponent <MotionComponent>(); position.Position = respawn.Location; position.Rotation = 0; motion.Velocity = Vector2.Zero; motion.AngularVelocity = 0; life.Lives--; e.AddComponent(new ShieldComponent()); this.signalManager.AddSignal(SignalType.LifeLost, e.EntityId); if (life.Lives < 0) { e.Destroy(); this.signalManager.AddSignal(SignalType.EntityDestroyed, e.EntityId); } return(true); } } return(false); }
public override void Update(float elapsedSeconds) { foreach (Entity e in this.actives.Values) { TransformComponent transform = e.GetComponent <TransformComponent>(); MotionComponent motion = e.GetComponent <MotionComponent>(); System.Diagnostics.Debug.Assert(transform != null, "TransformComponent not found"); System.Diagnostics.Debug.Assert(motion != null, "VelocityComponent not found"); motion.Velocity += (-motion.Velocity * motion.Drag) + motion.Acceleration * elapsedSeconds; if (motion.Velocity.LengthSquared() > motion.MaxVelocity * motion.MaxVelocity) { motion.Velocity = Vector2.Normalize(motion.Velocity) * motion.MaxVelocity; } else if (motion.Velocity.LengthSquared() < 1 && motion.Acceleration == Vector2.Zero && motion.Velocity != Vector2.Zero) { // if object is not accelerating and still moving, set velocity to 0 if low enough motion.Velocity = Vector2.Zero; } motion.AngularVelocity += (-motion.AngularVelocity * motion.AngularDrag) + motion.AngularAcceleration * elapsedSeconds; motion.AngularVelocity = MathHelper.Clamp(motion.AngularVelocity, -motion.MaxAngularVelocity, motion.MaxAngularVelocity); transform.Position += motion.Velocity * elapsedSeconds; transform.Rotation += motion.AngularVelocity * elapsedSeconds; motion.ResetForces(); } }
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); //} } }
private bool RepellFromBorderInward(Entity e, float acceleration) { TransformComponent transform = e.GetComponent <TransformComponent>(); MotionComponent motion = e.GetComponent <MotionComponent>(); motion.AddAcceleration(Vector2.Normalize(GameConfig.PlayArea.Center.ToVector2() - transform.Position) * acceleration); return(true); }
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; } } }
private void HandleEntityData(EntityDataPacket p, float serverDeltaTime = 0) { Entity e = this.entityWorld.EntityManager.GetEntity(p.EntityId); if (e != null) { TransformComponent transform = e.GetComponent <TransformComponent>(); System.Diagnostics.Debug.Assert(transform != null, transform.GetType() + " not found"); // TODO: better client side prediction: /* position + (velocity * network_latency) * position + (velocity * network_latency) + (acceleration * network_latency^2) / 2 * might be inaccurate, due to high possible latency, > 100ms * * latency too high: extrapolate position using above (dead reckoning)? * latency in acceptable range: lerp predicted position and received position */ // adjust velocity continuosly, unless difference is too big, then change position float deltaValue = 30f; //pixel (unit; due to new scale) offset Vector2 dataPosition = new Vector2(p.PositionX, p.PositionY); if (!this.IsInbetween(transform.Position.X, dataPosition.X, deltaValue) || !this.IsInbetween(transform.Position.Y, dataPosition.Y, deltaValue)) { transform.Position = dataPosition; } if (!this.IsInbetween(transform.Rotation, p.Rotation, deltaValue / 120f)) { transform.Rotation = p.Rotation; } MotionComponent motion = e.GetComponent <MotionComponent>(); if (motion != null) { motion.Velocity = new Vector2(p.VelocityX, p.VelocityY); motion.AngularVelocity = p.AngularVelocity; //serverDeltaTime = serverDeltaTime; //time in totalseconds float convergeFactor = 0.1f; //percentage for adding velocity so that mismatched position catches up motion.Velocity += new Vector2((dataPosition.X - transform.Position.X) * convergeFactor * 100, (dataPosition.Y - transform.Position.Y) * convergeFactor * 100); motion.AngularVelocity += ((p.Rotation - transform.Rotation) * convergeFactor * 100); } } }
private bool StopAtBorder(Entity e, CollidableComponent collidable, CollidableComponent other) { TransformComponent transform = e.GetComponent <TransformComponent>(); MotionComponent motion = e.GetComponent <MotionComponent>(); Vector2 penetrationVector = collidable.Collider.PenetrationVector(other.Collider); if (!(penetrationVector == Vector2.Zero)) { transform.Position -= penetrationVector; Vector2 offsetDirection = Vector2.Normalize(penetrationVector); Vector2 offsetMagnitude = new Vector2(Math.Abs(motion.Velocity.X), Math.Abs(motion.Velocity.Y)); motion.Velocity -= offsetDirection * offsetMagnitude; } return(true); }