public Butterfly(GameplayScreen screen) : base(screen) { Radius = 4; Vector3 initPos = new Vector3(); initPos.X = MathHelper.Lerp(screen.world.Limits.Min.X, screen.world.Limits.Max.X, screen.random.NextFloat()); initPos.Y = MathHelper.Lerp(screen.world.Limits.Min.Y, screen.world.Limits.Max.Y, screen.random.NextFloat()); initPos.Z = 0; Position = initPos; MaxForce = 5 * 1.65f; MaxSpeed = MathHelper.Lerp(8, 12, screen.random.NextFloat()) * 1.65f; MaxSpeed = 75; MaxForce = 3 * MaxSpeed; Speed = MathHelper.Lerp(MaxSpeed / 2, MaxSpeed, screen.random.NextFloat()); float angle = (float)(screen.random.NextFloat() * MathHelper.TwoPi); Velocity = new Vector3((float)Math.Cos(angle), (float)Math.Sin(angle), 0) * Speed; alpha = new Controller(screen.random.NextFloat(), MathHelper.Lerp(0.4f, 0.8f, screen.random.NextFloat()), 0, 1, Controller.Mode.OSCILLATE); radiusOffset = new Controller( screen.random.NextFloat() * 4 - 2, 50, -2, 2, Controller.Mode.OSCILLATE); Controller animSpeed = new Controller(2, 1, 3, 7, Controller.Mode.OSCILLATE); anim = new AnimController(animSpeed); }
/// <summary> /// Constructor for constant position /// </summary> /// <param name="initial">constant position</param> public Controller(float initial) { this.pos = initial; this.speed = null; this.min = initial; this.max = initial; SetMode(Mode.STOPPED); }
/// <summary> /// Constructor /// </summary> /// <param name="initial">initial position</param> /// <param name="speed">time multiplier</param> /// <param name="min">minimum position</param> /// <param name="max">maxiumum position</param> /// <param name="mode">initial mode</param> public Controller(float initial, Controller speed, float min, float max, Mode mode) { Debug.Assert(min <= max); this.pos = MathHelper.Clamp(initial, min, max); this.speed = speed; this.min = min; this.max = max; SetMode(mode); }
/// <summary> /// Constructor /// </summary> /// <param name="screen">gameplay screen</param> public Player(GameplayScreen screen) : base(screen) { Radius = 8; Position = new Vector3(0,0,0); MaxSpeed = 75; MaxForce = 3 * MaxSpeed; Speed = 0; Controller animSpeed = new Controller(2, 1, 2, 3, Controller.Mode.OSCILLATE); anim = new AnimController(animSpeed); ghostEffect = new GhostEffect(screen, this); sonar = new Sonar(screen); }
/// <summary> /// Constructor /// </summary> /// <param name="speed">animation speed</param> public AnimController(Controller speed) : base(0, speed, 0, 1, Controller.Mode.CYCLE) { }
/// <summary> /// update time step /// </summary> /// <param name="dt">delta time</param> public void Update(float dt) { if (null != speed) { speed.Update(dt); } switch (mode) { case Mode.ONCE: // Stop at start or end depending on the direction (speed). { pos = pos + speed * dt; if (pos < min) { pos = min; mode = Mode.STOPPED; } else if (pos > max) { pos = max; mode = Mode.STOPPED; } } break; case Mode.CYCLE: // When at end, jump back to start or visa versa. { pos = pos + speed * dt; if (pos < min) { pos = pos + (max - min); //wrap } else if (pos > max) { pos = pos + (min - max); //wrap } } break; case Mode.OSCILLATE: // Go from start to end back to start. { pos = pos + speed * dt; if (pos < min) { pos = min; speed = -speed; } else if (pos > max) { pos = max; speed = -speed; } } break; case Mode.RING: // Oscillate once. { pos = pos + speed * dt; if (pos < min) { pos = min; speed = -speed; ringCounter++; } else if (pos > max) { pos = max; speed = -speed; ringCounter++; } if (ringCounter >= 2) { mode = Mode.STOPPED; } } break; case Mode.WANDER: // Wander randomly back and forth { // create a random number generator if needed float rand01 = (float)GameplayScreen.Instance.random.NextDouble(); // 0 to 1 pos = pos + (((rand01 * 2) - 1) * dt); pos = MathHelper.Clamp(pos, min, max); } break; } }