void ICmpUpdatable.OnUpdate() { if (heading == kHeading.kHeadingWest) { m_RigidBody.ApplyLocalForce(new Vector2(-Speed, 0)); if (m_RigidBody.LinearVelocity.X <= -TopSpeed) { m_RigidBody.LinearVelocity = new Vector2(-TopSpeed, 0); } } else if (heading == kHeading.kHeadingEast) { m_RigidBody.ApplyLocalForce(new Vector2(Speed, 0)); if (m_RigidBody.LinearVelocity.X >= TopSpeed) { m_RigidBody.LinearVelocity = new Vector2(TopSpeed, 0); } } else if (heading == kHeading.kHeadingNorth) { m_RigidBody.ApplyLocalForce(new Vector2(0, -Speed)); if (m_RigidBody.LinearVelocity.Y <= -TopSpeed) { m_RigidBody.LinearVelocity = new Vector2(0, -TopSpeed); } } else if (heading == kHeading.kHeadingSouth) { m_RigidBody.ApplyLocalForce(new Vector2(0, Speed)); if (m_RigidBody.LinearVelocity.Y >= TopSpeed) { m_RigidBody.LinearVelocity = new Vector2(0, TopSpeed); } } }
private void MoveLaterally(RigidBody body) { float xVel = MoveUnits * Time.TimeMult; float yVel = body.LinearVelocity.Y; //// deal with lateral (horizonal) movement //if (IsOnGround) // xVel = MoveUnits * Time.TimeMult; // lateral movement speed while on the ground //else // xVel = MoveUnits / 4 * Time.TimeMult; // in air player can still move laterally but less quickly, this is unrealistic but it's what the player expects from a 2d platformer (because it's always been that way) //if (_collisionData != null) //{ // var playerVelocity = GameObj.GetComponent<RigidBody>().LinearVelocity; // if (_collisionData.Tangent.Angle != playerVelocity.Angle) // { // var angle = Vector2.AngleBetween(_collisionData.Tangent, playerVelocity); // if (angle > 0.02F) // yVel = -2; // } //} if (KeyPressed(Key.A, Key.Left)) { body.ApplyLocalForce(new Vector2(-300, 0)); //body.LinearVelocity = new Vector2(-xVel, yVel); } else if (KeyPressed(Key.D, Key.Right)) { body.ApplyLocalForce(new Vector2(300, 0)); //body.LinearVelocity = new Vector2(xVel, yVel); } }
void ICmpUpdatable.OnUpdate() { cameraTransform.MoveTo(GameObj.Transform.Pos + cameraOffset); cameraTransform.Angle = GameObj.Transform.Angle; RigidBody body = this.GameObj.GetComponent <RigidBody>(); if (DualityApp.Keyboard[Key.Left]) { body.ApplyLocalForce(-0.001f * body.Inertia); } else if (DualityApp.Keyboard[Key.Right]) { body.ApplyLocalForce(0.001f * body.Inertia); } else { body.AngularVelocity -= body.AngularVelocity * 0.1f * Time.TimeMult; } if (DualityApp.Keyboard[Key.Up]) { body.ApplyLocalForce(Vector2.UnitY * -0.2f * body.Mass); } else if (DualityApp.Keyboard[Key.Down]) { body.ApplyLocalForce(Vector2.UnitY * 0.2f * body.Mass); } //code for bullets m_FiringDelayCounter += Time.TimeMult; if (DualityApp.Keyboard[Key.Space] && Particles != null && m_FiringDelayCounter > FiringDelay) { for (int i = 0; i < 360; i += 10) { m_FiringDelayCounter = 0; Transform transform = GameObj.GetComponent <Transform>(); GameObject particle = Particles.Res.Instantiate(transform.Pos + transform.GetWorldVector(FiringOffset), transform.Angle + i); ParticleController particleController = particle.GetComponent <ParticleController>(); particleController.Creator = GameObj; Scene.Current.AddObject(particle); } } }
private void ControlByKeyboard() { RigidBody body = GameObj.GetComponent <RigidBody>(); if (KeyPressed(Key.A, Key.D, Key.Left, Key.Right)) { MoveLaterally(body); } // deal with jumping bool jumpKeyHit = (DualityApp.Keyboard.KeyHit(Key.Space) || DualityApp.Keyboard.KeyHit(Key.Up)); bool inCollision = _isInCollision; EventAggregator.AnnounceEvent(new DebugMessageEvent($"In Collision?: {inCollision}")); if (jumpKeyHit && inCollision) { body.LinearVelocity = new Vector2(body.LinearVelocity.X, -JumpUnits * Time.TimeMult); } body.ApplyLocalForce(new Vector2(0, FallUnits * Time.TimeMult)); EventAggregator.AnnounceEvent(new PlayerMovedEvent(GameObj.Transform.Pos)); }
public void OnUpdate() { var keyboard = DualityApp.Keyboard; var movementDir = Vector2.Zero; if (keyboard.KeyPressed(Key.Right)) { movementDir += Vector2.UnitX; } if (keyboard.KeyPressed(Key.Left)) { movementDir += -Vector2.UnitX; } if (keyboard.KeyPressed(Key.Up)) { movementDir += -Vector2.UnitY; } if (keyboard.KeyPressed(Key.Down)) { movementDir += Vector2.UnitY; } body.ApplyLocalForce(movementDir * movementSpeed); }
void ICmpUpdatable.OnUpdate() { Transform transform = this.GameObj.Transform; RigidBody body = this.GameObj.GetComponent <RigidBody>(); // Determine how fast we want to be and apply a force to reach the target velocity Vector2 clampedTargetMovement = this.targetMovement / MathF.Max(1.0f, this.targetMovement.Length); Vector2 targetVelocity = clampedTargetMovement * this.speed; Vector2 appliedForce = (targetVelocity - body.LinearVelocity) * body.Mass * this.acceleration; body.ApplyLocalForce(appliedForce); // Do we have an animated actor? Let it know how to animate! ActorAnimator animator = this.GameObj.GetComponent <ActorAnimator>(); if (animator != null) { if (targetVelocity.Length > 0.01f) { animator.AnimationSpeed = clampedTargetMovement.Length; animator.AnimationDirection = clampedTargetMovement.Angle; animator.PlayAnimation("Walk"); } else { animator.AnimationSpeed = 1.0f; animator.PlayAnimation("Idle"); } } }
void ICmpUpdatable.OnUpdate() { Transform transform = this.GameObj.Transform; RigidBody body = this.GameObj.GetComponent <RigidBody>(); Vector2 adjustedTargetMovement = this.targetMovement / MathF.Max(1.0f, this.targetMovement.Length); if (adjustedTargetMovement.Length > 0.01f && this.moveSenseRadius > 0.0f) { Vector2 targetDir = this.targetMovement.Normalized; float senseRadius = this.moveSenseRadius * adjustedTargetMovement.Length; RayCastData firstHit; Vector2 rayStart = transform.Pos.Xy; Vector2 rayEnd = rayStart + targetDir * senseRadius; bool hitAnything = RigidBody.RayCast(rayStart, rayEnd, data => data.Fraction, out firstHit); if (hitAnything) { float movementRatio = 0.5f + 0.5f * ((firstHit.Pos - rayStart).Length / (rayEnd - rayStart).Length); adjustedTargetMovement *= movementRatio; } //VisualLog.Default // .DrawConnection(rayStart.X, rayStart.Y, 0.0f, hitAnything ? firstHit.Pos.X : rayEnd.X, hitAnything ? firstHit.Pos.Y : rayEnd.Y) // .WithOffset(-100); } Vector2 targetVelocity = adjustedTargetMovement * this.speed; Vector2 appliedForce = (targetVelocity - body.LinearVelocity) * body.Mass * this.acceleration; body.ApplyLocalForce(appliedForce); }
private void TryToHoldPosition() { Vector3 position = GameObj.Transform.Pos; Vector3 velocity = GameObj.Transform.Vel; float forceY = 0, forceX = 0; if (position.Y > TargetPosition.Y && velocity.Y >= 0) { forceY -= _verticalThrottleStep * Time.TimeMult; } else if (position.Y < TargetPosition.Y && velocity.Y <= 0) { forceY += _verticalThrottleStep * Time.TimeMult; } if (position.X < TargetPosition.X && velocity.X < MaximumVelocity) // velocity.X <= 0 && { forceX += _lateralThrottleStep * Time.TimeMult; } else if (position.X > TargetPosition.X && velocity.X > -MaximumVelocity) // velocity.X >= 0 && { forceX -= _lateralThrottleStep * Time.TimeMult; } RigidBody body = GameObj.GetComponent <RigidBody>(); //EventAggregator.AnnounceEvent(new DebugMessageEvent($"Force: ({forceX}, {forceY})")); body.ApplyLocalForce(new Vector2(forceX, forceY)); }
void ICmpUpdatable.OnUpdate() { RigidBody body = this.GameObj.GetComponent <RigidBody>(); float targetRotation = 0.0f; if (DualityApp.Keyboard[Key.Left]) { targetRotation = -1.0f; } else if (DualityApp.Keyboard[Key.Right]) { targetRotation = 1.0f; } body.AngularVelocity = targetRotation * this.turnSpeed; Vector2 targetMovement = Vector2.Zero; if (DualityApp.Keyboard[Key.Up]) { targetMovement = -Vector2.UnitY; } else if (DualityApp.Keyboard[Key.Down]) { targetMovement = Vector2.UnitY; } body.ApplyLocalForce(targetMovement * this.moveAcceleration * body.Mass); }
void ICmpUpdatable.OnUpdate() { FiringDelayCounter += Time.TimeMult; Transform transform = GameObj.GetComponent <Transform>(); RigidBody body = GameObj.GetComponent <RigidBody>(); if (BulletPrefab != null) { if (DualityApp.Keyboard[Key.Space]) { Fire(transform, body); } else if (DualityApp.Keyboard[Key.X]) { CanReload = false; Fire(transform, body, RapidFireMultiplier); } if (DualityApp.Keyboard.KeyReleased(Key.X)) { CanReload = true; } Reload(); } if (DualityApp.Keyboard[Key.Left] || DualityApp.Keyboard[Key.A]) { body.ApplyLocalForce(-RotationForce * body.Inertia); } else if (DualityApp.Keyboard[Key.Right] || DualityApp.Keyboard[Key.D]) { body.ApplyLocalForce(RotationForce * body.Inertia); } else { body.AngularVelocity -= body.AngularVelocity * 0.1f * Time.TimeMult; } if ((DualityApp.Keyboard[Key.Up] || DualityApp.Keyboard[Key.W]) && body.LinearVelocity.LengthSquared < 60f) { body.ApplyLocalForce(Vector2.UnitY * -0.2f * body.Mass); } }
//Aplicar ao bloco impulso public void applyForce() { //Obter da scene o objecto Ball GameObject theBallObject = Duality.Resources.Scene.Current.FindGameObject("Ball"); RigidBody bodyBall = theBallObject.GetComponent <RigidBody>(); if (bodyBall != null) { targetMovement = -Vector2.UnitY; bodyBall.ApplyLocalForce(targetMovement * 0.07f * bodyBall.Inertia); } }
void ICmpUpdatable.OnUpdate() { Transform transform = this.GameObj.Transform; RigidBody body = this.GameObj.GetComponent <RigidBody>(); // Determine how fast we want to be and apply a force to reach the target velocity Vector2 clampedTargetMovement = this.targetMovement / MathF.Max(1.0f, this.targetMovement.Length); Vector2 targetVelocity = clampedTargetMovement * this.speed; Vector2 appliedForce = (targetVelocity - body.LinearVelocity) * body.Mass * this.acceleration; body.ApplyLocalForce(appliedForce); }
//Aplicar movimento ao bloco public void moveBlock(GameObject Object, RigidBody body, Vector2 targetMovement) { Transform transformComponent = Object.GetComponent <Transform>(); if (transformComponent.Pos.X <= 350 && transformComponent.Pos.X >= -350) { body.ApplyLocalForce(targetMovement * 0.2f * body.Mass); } else { transformComponent.Pos = keepInPanel(transformComponent.Pos.X, transformComponent.Pos.Y); } }
void ICmpUpdatable.OnUpdate() { RigidBody body = this.GameObj.GetComponent <RigidBody>(); float targetRotation = 0.0f; if (DualityApp.Keyboard[Key.Left]) { targetRotation = -1.0f; } else if (DualityApp.Keyboard[Key.Right]) { targetRotation = 1.0f; } body.AngularVelocity = targetRotation * this.turnSpeed; Vector2 targetMovement = Vector2.Zero; if (DualityApp.Keyboard[Key.Up]) { targetMovement = -Vector2.UnitY; } else if (DualityApp.Keyboard[Key.Down]) { targetMovement = Vector2.UnitY; } body.ApplyLocalForce(targetMovement * this.moveAcceleration * body.Mass); if (DualityApp.Keyboard.KeyHit(Key.Space)) { GameObject laserObj = this.laserPrefab.Res.Instantiate(); laserObj.Transform.Pos = this.GameObj.Transform.Pos; laserObj.Transform.Angle = this.GameObj.Transform.Angle; RigidBody laserBody = laserObj.GetComponent <RigidBody>(); laserBody.LinearVelocity = Vector2.FromAngleLength(this.GameObj.Transform.Angle, 10.0f); this.GameObj.ParentScene.AddObject(laserObj); } }
void ICmpUpdatable.OnUpdate() { cameraTransform.MoveTo(GameObj.Transform.Pos + cameraOffset); cameraTransform.Angle = GameObj.Transform.Angle; RigidBody body = this.GameObj.GetComponent <RigidBody>(); //pause sound code if (sonarSound.Sources.ElementAt(0).Paused == false && (Time.MainTimer.TotalSeconds - soundStart > 5.50)) { sonarSound.Sources.ElementAt(0).Paused = true; } if (DualityApp.Keyboard[Key.Left]) { body.ApplyLocalForce(-0.001f * body.Inertia); } else if (DualityApp.Keyboard[Key.Right]) { body.ApplyLocalForce(0.001f * body.Inertia); } else { body.AngularVelocity -= body.AngularVelocity * 0.1f * Time.TimeMult; } if (DualityApp.Keyboard[Key.Up]) { body.ApplyLocalForce(Vector2.UnitY * -0.2f * body.Mass); } else if (DualityApp.Keyboard[Key.Down]) { body.ApplyLocalForce(Vector2.UnitY * 0.2f * body.Mass); } //code for bullets m_FiringDelayCounter += Time.TimeMult; if (DualityApp.Keyboard[Key.Space] && Particles != null && m_FiringDelayCounter > FiringDelay) { for (int i = 0; i < 360; i += 10) { m_FiringDelayCounter = 0; Transform transform = GameObj.GetComponent <Transform>(); GameObject particle = Particles.Res.Instantiate(transform.Pos + transform.GetWorldVector(FiringOffset), transform.Angle + i); ParticleController particleController = particle.GetComponent <ParticleController>(); particleController.Creator = GameObj; //DualityApp.Sound.PlaySound(Sound.Beep); if (sonarSound.Sources.ElementAt(0).Paused == true) { sonarSound.Sources.ElementAt(0).Paused = false; soundStart = Time.MainTimer.TotalSeconds; } Scene.Current.AddObject(particle); } } //code for arrow Vector3 difference = arrow.GetLocalVector(finish.Pos) - arrow.Pos; //Log.Game.Write(difference.ToString()); double angleDegrees = Math.Acos(difference.Y / difference.X); double angleRads = (Math.PI / 180f) * angleDegrees; //Log.Game.Write("degrees: " + angleDegrees.ToString()); Log.Game.Write("rads" + angleRads.ToString()); if (!Double.IsNaN(angleRads)) { arrow.Angle = (float)angleRads + (float)Math.PI; } }
private void handleMovement() { float xSpeed = Speed; float ySpeed = Speed; if (PlayerNum == 1) { if (DualityApp.Keyboard[Key.D]) { //m_Transform.Pos = new Vector3(m_Transform.Pos.X + (Speed * Time.TimeMult), m_Transform.Pos.Y, m_Transform.Pos.Z); //move right //m_Rigidbody.LinearVelocity = Vector2.UnitX * Speed; //m_Rigidbody.ApplyLocalForce(new Vector2(10,0)); currentHeading = kHeading.kHeadingEast; } else if (DualityApp.Keyboard[Key.A]) { xSpeed = xSpeed * -1; m_Rigidbody.ApplyLocalForce(new Vector2(-10, 0)); //m_Transform.Pos = new Vector3(m_Transform.Pos.X - (Speed * Time.TimeMult), m_Transform.Pos.Y, m_Transform.Pos.Z); //move left currentHeading = kHeading.kHeadingWest; } else { xSpeed = 0; } if (DualityApp.Keyboard[Key.W]) { //m_Transform.Pos = new Vector3(m_Transform.Pos.X, m_Transform.Pos.Y - (Speed * Time.TimeMult), m_Transform.Pos.Z); //move down ySpeed = ySpeed * -1; currentHeading = kHeading.kHeadingNorth; } else if (DualityApp.Keyboard[Key.S]) { //m_Transform.Pos = new Vector3(m_Transform.Pos.X, m_Transform.Pos.Y + (Speed * Time.TimeMult), m_Transform.Pos.Z); //move up currentHeading = kHeading.kHeadingSouth; } else { ySpeed = 0; } m_Rigidbody.LinearVelocity = new Vector2(xSpeed, ySpeed); } else if (PlayerNum == 2) { if (DualityApp.Keyboard[Key.Right]) { //m_Transform.Pos = new Vector3(m_Transform.Pos.X + (Speed * Time.TimeMult), m_Transform.Pos.Y, m_Transform.Pos.Z); //move right //m_Rigidbody.LinearVelocity = Vector2.UnitX * Speed; //m_Rigidbody.ApplyLocalForce(new Vector2(10,0)); currentHeading = kHeading.kHeadingEast; } else if (DualityApp.Keyboard[Key.Left]) { xSpeed = xSpeed * -1; m_Rigidbody.ApplyLocalForce(new Vector2(-10, 0)); //m_Transform.Pos = new Vector3(m_Transform.Pos.X - (Speed * Time.TimeMult), m_Transform.Pos.Y, m_Transform.Pos.Z); //move left currentHeading = kHeading.kHeadingWest; } else { xSpeed = 0; } if (DualityApp.Keyboard[Key.Up]) { //m_Transform.Pos = new Vector3(m_Transform.Pos.X, m_Transform.Pos.Y - (Speed * Time.TimeMult), m_Transform.Pos.Z); //move down ySpeed = ySpeed * -1; currentHeading = kHeading.kHeadingNorth; } else if (DualityApp.Keyboard[Key.Down]) { //m_Transform.Pos = new Vector3(m_Transform.Pos.X, m_Transform.Pos.Y + (Speed * Time.TimeMult), m_Transform.Pos.Z); //move up currentHeading = kHeading.kHeadingSouth; } else { ySpeed = 0; } m_Rigidbody.LinearVelocity = new Vector2(xSpeed, ySpeed); } }
public void OnUpdate() { RigidBody body = GameObj.GetComponent <RigidBody>(); var ship = GameObj.GetComponent <Ship>(); var transform = GameObj.Transform; if (ship.Health > 0) { if (DualityApp.Keyboard[Key.Left]) { body.ApplyLocalForce(-0.001f * body.Inertia); if (body.AngularVelocity < -_maxTurnSpeed) { body.AngularVelocity = -_maxTurnSpeed; } } else if (DualityApp.Keyboard[Key.Right]) { body.ApplyLocalForce(0.001f * body.Inertia); if (body.AngularVelocity > _maxTurnSpeed) { body.AngularVelocity = _maxTurnSpeed; } } else { body.AngularVelocity -= body.AngularVelocity * (1 / 60f) * Time.TimeMult; } if (DualityApp.Keyboard[Key.Up]) { body.ApplyLocalForce(Vector2.UnitY * -0.2f * body.Mass); } else if (DualityApp.Keyboard[Key.Down]) { body.ApplyLocalForce(Vector2.UnitY * 0.05f * body.Mass); } else { body.LinearVelocity -= body.LinearVelocity * (1 / 120f) * Time.TimeMult; } if (body.LinearVelocity.Length > MaxSpeed) { body.LinearVelocity = body.LinearVelocity.Normalized * MaxSpeed; } } if (_cannonBallInstance == null || (_cannonBallInstance != null && !_cannonBallInstance.Active)) { GameObject newCannonball = null; if (DualityApp.Keyboard.KeyHit(Key.E)) { newCannonball = CannonBallPrefab.Res.Instantiate(transform.Pos, (float)(transform.Angle + (Math.PI / 2))); } else if (DualityApp.Keyboard.KeyHit(Key.Q)) { newCannonball = CannonBallPrefab.Res.Instantiate(transform.Pos, (float)(transform.Angle - (Math.PI / 2))); } if (newCannonball != null) { _cannonBallInstance = newCannonball; var newCannonballComponent = newCannonball.GetComponent <CannonBall>(); newCannonballComponent.Creator = GameObj; Scene.Current.AddObject(newCannonball); } } }
public void OnUpdate() { if (GameObj.GetComponentsInChildren <WeaponController>().Count == 0 && DefaultGun != null) { GameObject gun = DefaultGun.Res.Instantiate(GameObj.Transform.Pos, GameObj.Transform.Angle); gun.Parent = GameObj; Scene.AddObject(gun); } if (Gamepad && DualityApp.Gamepads.Count > 1) { gamepad = DualityApp.Gamepads.ElementAt(0); } if (GamepadAvailable) { if (gamepad.LeftThumbstick.X != 0) { body.ApplyLocalForce(TurnRate * body.Inertia * gamepad.LeftThumbstick.X); } else { body.AngularVelocity -= body.AngularVelocity * 0.1f * Time.TimeMult; } Vector2 thrust = Vector2.UnitY * Speed * body.Mass * gamepad.RightThumbstick.Y; if (gamepad.RightThumbstick.Y > 0) { thrust *= 0.8f; } body.ApplyLocalForce(thrust); } else { if (DualityApp.Keyboard.KeyPressed(LeftKey)) { body.ApplyLocalForce(-TurnRate * body.Inertia); } else if (body.AngularVelocity < 0) { body.AngularVelocity -= body.AngularVelocity * 0.1f * Time.TimeMult; } if (DualityApp.Keyboard.KeyPressed(RightKey)) { body.ApplyLocalForce(TurnRate * body.Inertia); } else if (body.AngularVelocity > 0) { body.AngularVelocity -= body.AngularVelocity * 0.1f * Time.TimeMult; } if (DualityApp.Keyboard.KeyPressed(ThrustKey)) { body.ApplyLocalForce(Vector2.UnitY * -Speed * body.Mass); } else if (DualityApp.Keyboard.KeyPressed(ReverseKey)) { body.ApplyLocalForce(Vector2.UnitY * 0.8f * Speed * body.Mass); } } }