void Update() { RaycastHit hit; Ray forward = new Ray(transform.position, transform.forward); Ray backward = new Ray(transform.position, transform.forward * -1); if (Physics.Raycast(forward, out hit) || Physics.Raycast(backward, out hit)) { if (hit.transform.CompareTag("Enemy")) { direction *= -1; } else if (hit.transform.gameObject.CompareTag("Player") && !isCoolingDown) { isCoolingDown = true; FireComponent.numberOfCollectedParasites = 1; FireComponent.Fire(); Invoke("ResetCoolDown", 2 / difficultyMultipyer); } else if (hit.distance < 1.1f && Physics.Raycast(transform.position + Vector3.up * 0.2f, -transform.up, 0.25f)) { //JumpComponent.Jump(); } } MovmentComponent.Move(direction); RotationComponent.RotateTowards(target); MovmentComponent.thisUpdate(); }
private void Awake() { selfTrans = transform; MovementComp = GetComponent <MovementComponent>(); RotationComp = GetComponent <RotationComponent>(); onAwake(); }
public override void Update(float dt) { foreach (Entity rotationEntity in _rotationEntities) { RotationComponent rotationComp = rotationEntity.GetComponent <RotationComponent>(); rotationEntity.GetComponent <TransformComponent>().Rotate(rotationComp.RotationSpeed * dt); } }
// Set new target if score of other ship is higher than current target void UpdateTarget(int enemyScore, int targetScore, RotationComponent rotationC, Transform target) { if (enemyScore >= targetScore) { targetScore = enemyScore; rotationC.target = target; } }
public override void Process(Entity entity) { RotationComponent rc = entity.GetComponent <RotationComponent>(); if (rc.AutoRotate) { rc.Angle = MathFunctions.ClampAngle(rc.Angle + rc.RotationSpeed); } }
/// <summary> /// 把GameObject转为 实体(Entity) /// </summary> /// <param name="entity"></param> /// <param name="dstManager"></param> /// <param name="conversionSystem"></param> public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { /// 创建一个组件 RotationComponent data = new RotationComponent { Speed = Speed }; // 把 组件 加入EntityManager 中,让Untiy 内置的环境实体管理器 管理 dstManager.AddComponentData(entity, data); }
//TODO Can I return RotationComponent? public static (float x, float y) CalculateAddedRotation(this RotationComponent currentRotation, float addingDegree) { var rad = (float)(Math.PI / 180F) * addingDegree; var cos = (float)Math.Cos(rad); var sin = (float)Math.Sin(rad); var rotationX = currentRotation.x * cos - currentRotation.y * sin; var rotationY = currentRotation.y * cos + currentRotation.x * sin; return(rotationX, rotationY); }
private GameObject CreatePlayer(int playerNumber, Color color) { // MonoGame works in Radians // http://msdn.microsoft.com/en-us/library/system.math.sin%28v=vs.110%29.aspx var startX = GameHelper.GetRelativeScaleX(0.1f); var startLocation = new Vector2(startX, _waveTopY) + new Vector2(playerNumber * startX, 0); var startRotation = (float)(90 * (Math.PI / 180)); var texture = SurfingGraphics.Surfboard_Large_White_Asset; var player = new GameObject("Player", startLocation, startRotation); var airGravityMovement = new AccelerateMovementComponent(new Vector2(0, 0.05f)); var outOfBoundary = new OutOfBoundsComponent(ObjectEvent.ResetEntity); var boundary = new BoundaryComponent(texture, texture.Width, texture.Height); var rotationMovement = new RotationMovementComponent(2, Vector2.Zero); var rotation = new RotationComponent(Vector2.Zero); var sprite = new SpriteComponent(texture, rotationOrigin: texture.Centre(), color: color); player.AddComponent(airGravityMovement); player.AddComponent(sprite); player.AddComponent(rotationMovement); player.AddComponent(boundary); player.AddComponent(outOfBoundary); player.AddComponent(rotation); var onWave = "OnWave"; var inAir = "InAir"; var state = new StateStringComponent(); state.AddComponentState(rotationMovement, onWave, inAir); state.AddComponentState(rotation, inAir, onWave); state.AddComponentState(airGravityMovement, inAir, onWave); player.AddComponent(state); var boundaryState = new BoundaryStateComponent(new Rectangle(0, _waveTopY, GameHelper.GetRelativeScaleX(1f), (int)(_waveHeight * 1.3f)), onWave, inAir); player.AddComponent(boundaryState); ForegroundLayer.AddGameObject(player); return(player); }
void OnDrawGizmosSelected() { AISpawnBoltData spawnData = Value; Gizmos.color = Color.blue; Vector3 position = new Vector3(spawnData.spawnPosition.x, spawnData.spawnPosition.y, spawnData.spawnPosition.z); RotationComponent rotationComponent = GetComponent <RotationComponent>(); if (rotationComponent) { Vector3 forward = math.forward(rotationComponent.Value.Value); position += forward * spawnData.offset; } Gizmos.DrawSphere(position, 0.1f); }
public override void Process(Entity entity) { ThrustComponent tc = entity.GetComponent <ThrustComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); if (InputManager.Instance.isKeyReleased(Keys.Space)) { PositionComponent pc = entity.GetComponent <PositionComponent>(); InertiaComponent ic = entity.GetComponent <InertiaComponent>(); Entity laser = this.EntityWorld.CreateEntity(); laser.AddComponent(new ProjectileComponent(15, new TimeSpan(0, 0, 0, 0, 450))); laser.AddComponent(new PositionComponent(pc.Position)); laser.AddComponent(new RotationComponent(rc.Angle, 0f, false)); float radians = MathHelper.ToRadians(rc.Angle); laser.AddComponent(new InertiaComponent(Vector2.Multiply(Vector2.Normalize(new Vector2((float)Math.Sin(radians), -(float)Math.Cos(radians))), laser.GetComponent <ProjectileComponent>().MaxSpeed))); laser.AddComponent(new TextureComponent(new Coord(0, 0), "lasers", new Vector2(0, 0))); } }
public override void Process(Entity entity) { TextureComponent tc = entity.GetComponent <TextureComponent>(); PositionComponent pc = entity.GetComponent <PositionComponent>(); RotationComponent rc = (entity.HasComponent <RotationComponent>()) ? entity.GetComponent <RotationComponent>() : new RotationComponent(0, 0, false); ShadowComponent sc = (entity.HasComponent <ShadowComponent>()) ? entity.GetComponent <ShadowComponent>() : null; Rectangle sourceRect = ResourceManager.Instance.GetSpriteSheet(tc.Texture).GetSourceRect(tc.Position.X, tc.Position.Y); Rectangle destRect = new Rectangle((int)pc.Position.X, (int)pc.Position.Y, sourceRect.Width, sourceRect.Height); Texture2D texture = ResourceManager.Instance.GetSpriteSheet(tc.Texture).Texture; ScreenManager.Instance.SpriteBatch.Draw(texture, destRect, sourceRect, Color.White, MathHelper.ToRadians(rc.Angle), tc.Origin, SpriteEffects.None, 0f); //if (sc != null) //{ // Rectangle shadowSourceRect = ResourceManager.Instance.GetSpriteSheet(sc.Texture).GetSourceRect(sc.Position.X, sc.Position.Y); // Texture2D shadowTexture = ResourceManager.Instance.GetSpriteSheet(sc.Texture).Texture; // ScreenManager.Instance.SpriteBatch.Draw(shadowTexture, destRect, shadowSourceRect, Color.White, sc.Angle, sc.Origin, SpriteEffects.None, 0f); //} }
private void OnRotationChanged(RotationComponent component) { OnRotationChanged(component, ref _rotation); }
public override void Process(Entity entity) { CollisionComponent cc = entity.GetComponent <CollisionComponent>(); PositionComponent pc = entity.GetComponent <PositionComponent>(); RotationComponent rc = (entity.HasComponent <RotationComponent>()) ? entity.GetComponent <RotationComponent>() : new RotationComponent(0, 0, false); }
public override void Process(Entity entity) { ThrustComponent tc = entity.GetComponent <ThrustComponent>(); if (tc.ForwardThrust) { InertiaComponent ic = entity.GetComponent <InertiaComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); double angleInDegrees = MathHelper.ToRadians(rc.Angle); ic.Inertia.X += (float)Math.Sin(angleInDegrees) * tc.Acceleration; ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * tc.Acceleration; if (ic.Inertia.Length() > tc.MaxSpeed) { ic.Inertia.Normalize(); ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed); } } if (tc.BackwardThrust) { InertiaComponent ic = entity.GetComponent <InertiaComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); float currentSpeed = ic.Inertia.Length(); ic.Inertia = Vector2.Multiply(ic.Inertia, tc.Deceleration); if (ic.Inertia.Length() < tc.Acceleration) { ic.Inertia.X = 0; ic.Inertia.Y = 0; } } if (tc.LeftThrust) { InertiaComponent ic = entity.GetComponent <InertiaComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); double angleInDegrees = MathHelper.ToRadians(rc.Angle - 90); // 90 degrees anti-clockwise ic.Inertia.X += (float)Math.Sin(angleInDegrees) * 0.05f; ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * 0.05f; if (ic.Inertia.Length() > tc.MaxSpeed) { ic.Inertia.Normalize(); ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed); } } if (tc.RightThrust) { InertiaComponent ic = entity.GetComponent <InertiaComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); double angleInDegrees = MathHelper.ToRadians(rc.Angle + 90); // 90 degrees clockwise ic.Inertia.X += (float)Math.Sin(angleInDegrees) * 0.05f; ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * 0.05f; if (ic.Inertia.Length() > tc.MaxSpeed) { ic.Inertia.Normalize(); ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed); } } }
protected override void OnRotationChanged(RotationComponent component, ref Vector2 rotation) { if (component.HasFlag(RotationComponent.Pitch)) { InvalidateProjectionMatrix(); } else { base.OnRotationChanged(component, ref rotation); } }
/// <summary> /// Method called when the camera's rotation has been modified. /// </summary> /// <param name="component">The component(s) of the camera's rotation /// that were modified</param> protected virtual void OnRotationChanged(RotationComponent component, ref Vector2 rotation) { InvalidateViewMatrix(); }
public void UpdateRotation(string valueStr, bool start, RotationComponent component) { RotationUI ui = start ? _StartRotUI : _EndRotUI; Quaternion rotation = start ? _StartRot : _EndRot; var ea = rotation.eulerAngles; float value; if (!float.TryParse(valueStr, out value)) { return; } switch (component) { case RotationComponent.EulerX: ea.x = value; rotation.eulerAngles = ea; SetQuatFields(rotation, ui); break; case RotationComponent.EulerY: ea.y = value; rotation.eulerAngles = ea; SetQuatFields(rotation, ui); break; case RotationComponent.EulerZ: ea.z = value; rotation.eulerAngles = ea; SetQuatFields(rotation, ui); break; case RotationComponent.QuatX: rotation.x = value; SetEulerFields(rotation, ui); break; case RotationComponent.QuatY: rotation.y = value; SetEulerFields(rotation, ui); break; case RotationComponent.QuatZ: rotation.z = value; SetEulerFields(rotation, ui); break; case RotationComponent.QuatW: rotation.w = value; SetEulerFields(rotation, ui); break; } if (start) { _StartRot = rotation; } else { _EndRot = rotation; } ApplyInterpolation(_InterpSlider.value); }
private void Start() { parentRotationComponent = gameObject.GetComponentInParent <RotationComponent>(); }
public override void Process(Entity entity) { NavigationComponent nc = entity.GetComponent <NavigationComponent>(); if (nc.TargetLocation.HasValue) { PositionComponent pc = entity.GetComponent <PositionComponent>(); float deltaX = pc.Position.X - nc.TargetLocation.Value.X; float deltaY = pc.Position.Y - nc.TargetLocation.Value.Y; float distanceToTarget = (float)Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY)); if (distanceToTarget < 15) { nc.TargetLocation = null; entity.GetComponent <ThrustComponent>().ForwardThrust = false; entity.GetComponent <ThrustComponent>().BackwardThrust = true; return; } float angle = (float)Math.Atan2((nc.TargetLocation.Value.Y - pc.Position.Y), (nc.TargetLocation.Value.X - pc.Position.X)) + MathHelper.PiOver2; float angleDegrees = MathHelper.ToDegrees(angle); if (angleDegrees < 0) { angleDegrees += 360; } RotationComponent rc = entity.GetComponent <RotationComponent>(); float angleDiff = angleDegrees - rc.Angle; if (angleDiff > 180) { angleDiff -= 360; } if (angleDiff < -180) { angleDiff += 360; } if (angleDiff < -rc.RotationSpeed) { entity.GetComponent <ThrustComponent>().ForwardThrust = false; entity.GetComponent <ThrustComponent>().BackwardThrust = true; rc.Angle -= rc.RotationSpeed; if (rc.Angle < 0) { rc.Angle += 360; } } else if (angleDiff > rc.RotationSpeed) { entity.GetComponent <ThrustComponent>().ForwardThrust = false; entity.GetComponent <ThrustComponent>().BackwardThrust = true; rc.Angle += rc.RotationSpeed; if (rc.Angle > 360) { rc.Angle -= 360; } } else { ThrustComponent tc = entity.GetComponent <ThrustComponent>(); InertiaComponent ic = entity.GetComponent <InertiaComponent>(); float ticksToTarget = distanceToTarget / ic.Inertia.Length(); float ticksToStop = (float)Math.Log(ic.Inertia.Length() / tc.Deceleration) / tc.Deceleration; if (ticksToTarget > ticksToStop + 15) { tc.ForwardThrust = true; tc.BackwardThrust = false; } else { tc.ForwardThrust = false; tc.BackwardThrust = true; } } } else { if (entity.GetComponent <InertiaComponent>().Inertia.Length() == 0) { entity.GetComponent <ThrustComponent>().ForwardThrust = false; entity.GetComponent <ThrustComponent>().BackwardThrust = false; } } }
void DistanceToWaypointCheck(Transform waypoint, Transform spaceship, int maxDistance, RotationComponent rotationC, bool closeEnoughToWaypoint) { var dist = Vector3.Distance(waypoint.position, spaceship.position); // Calculate distance between ship and waypoint if (dist > maxDistance) // If distance is larger than max distance { rotationC.target = waypoint; // Set target to waypoint and set isCloseEnoughToWaypoint boolean closeEnoughToWaypoint = false; } else { closeEnoughToWaypoint = true; // Else set isCloseEnoughToWaypoint boolean } }