protected override bool TickCore(RealmTime time) { if (Host.Self.HasConditionEffect(ConditionEffects.Paralyzed)) return true; var speed = this.speed * GetSpeedMultiplier(Host.Self); WanderingState state; object o; if (!Host.StateStorage.TryGetValue(Key, out o)) Host.StateStorage[Key] = state = new WanderingState(); else { state = (WanderingState)o; var dist = speed / 1.5f * (time.thisTickTimes / 1000f); state.remainingDist -= dist; ValidateAndMove(Host.Self.X + state.x * dist, Host.Self.Y + state.y * dist); Host.Self.UpdateCount++; } bool ret; if (state.remainingDist <= 0) { state.x = rand.Next(-1, 2); state.y = rand.Next(-1, 2); state.remainingDist = dist + dist * (float)(rand.NextDouble() * 0.1 - 0.05); ret = true; } else ret = false; return ret; }
private WanderingState() { if (_instance != null) { return; } _instance = this; }
protected override bool TickCore(RealmTime time) { if (Host.Self.HasConditionEffect(ConditionEffects.Paralyzed)) { return(true); } var speed = this.speed * GetSpeedMultiplier(Host.Self); WanderingState state; object o; if (!Host.StateStorage.TryGetValue(Key, out o)) { Host.StateStorage[Key] = state = new WanderingState() { beginPos = new Position() { X = Host.Self.X, Y = Host.Self.Y } } } ; else { state = (WanderingState)o; double dist = (speed / 1.5f) * (time.thisTickTimes / 1000.0); double x = Math.Cos(state.angle) * dist; double y = Math.Sin(state.angle) * dist; state.remainingDist -= (float)dist; ValidateAndMove(Host.Self.X + (float)x, Host.Self.Y + (float)y); Host.Self.UpdateCount++; } bool ret; if (state.remainingDist <= 0) { double randAngle = Math.PI * 2 * rand.NextDouble(); double randRadius = radius * rand.NextDouble(); Position newPos = new Position() { X = (float)(Math.Cos(randAngle) * randRadius) + state.beginPos.X, Y = (float)(Math.Sin(randAngle) * randRadius) + state.beginPos.Y, }; var dx = newPos.X - Host.Self.X; var dy = newPos.Y - Host.Self.Y; state.angle = (float)Math.Atan2(dy, dx); state.remainingDist = (float)Math.Sqrt(dx * dx + dy * dy); ret = true; } else { ret = false; } return(ret); }
private void FixedUpdate() { if (currentState == WanderingState.Sleeping) { return; } if (Time.time > newStateAt) { // Go other way for some amount of time. newStateAt = Time.time + Random.Range(MinTimeBetweenChanges, MaxTimeBetweenChanges); var previousState = currentState; while (previousState == currentState || currentState == WanderingState.Sleeping) { currentState = Enum.GetValues(typeof(WanderingState)).Cast <WanderingState>().ElementAt(Random.Range(0, Enum.GetNames(typeof(WanderingState)).Length)); } if (currentState == WanderingState.Hop) { body.velocity = new Vector2(); body.AddForce(Vector2.up * HopForce, ForceMode2D.Impulse); } } switch (currentState) { case WanderingState.MovingLeft: body.velocity = new Vector2(-MoveSpeed, body.velocity.y); animator.SetInteger("Moving", -1); break; case WanderingState.MovingRight: body.velocity = new Vector2(MoveSpeed, body.velocity.y); animator.SetInteger("Moving", 1); break; case WanderingState.Still: body.velocity = new Vector2(0, body.velocity.y); animator.SetInteger("Moving", 0); break; case WanderingState.Hop: animator.SetInteger("Moving", 0); break; } }
protected override bool TickCore(RealmTime time) { if (Host.Self.HasConditionEffect(ConditionEffects.Paralyzed)) return true; var speed = this.speed * GetSpeedMultiplier(Host.Self); WanderingState state; object o; if (!Host.StateStorage.TryGetValue(Key, out o)) Host.StateStorage[Key] = state = new WanderingState { beginPos = new Position { X = Host.Self.X, Y = Host.Self.Y } }; else { state = (WanderingState)o; var dist = speed / 1.5f * (time.thisTickTimes / 1000.0); var x = Math.Cos(state.angle) * dist; var y = Math.Sin(state.angle) * dist; state.remainingDist -= (float)dist; ValidateAndMove(Host.Self.X + (float)x, Host.Self.Y + (float)y); Host.Self.UpdateCount++; } bool ret; if (state.remainingDist <= 0) { var randAngle = Math.PI * 2 * rand.NextDouble(); var randRadius = radius * rand.NextDouble(); var newPos = new Position { X = (float)(Math.Cos(randAngle) * randRadius) + state.beginPos.X, Y = (float)(Math.Sin(randAngle) * randRadius) + state.beginPos.Y }; var dx = newPos.X - Host.Self.X; var dy = newPos.Y - Host.Self.Y; state.angle = (float)Math.Atan2(dy, dx); state.remainingDist = (float)Math.Sqrt(dx * dx + dy * dy); ret = true; } else ret = false; return ret; }
protected override bool TickCore(RealmTime time) { if (Host.Self.HasConditionEffect(ConditionEffects.Paralyzed)) { return(true); } var speed = this.speed * GetSpeedMultiplier(Host.Self); WanderingState state; object o; if (!Host.StateStorage.TryGetValue(Key, out o)) { Host.StateStorage[Key] = state = new WanderingState(); } else { state = (WanderingState)o; float dist = (speed / 1.5f) * (time.thisTickTimes / 1000f); state.remainingDist -= dist; ValidateAndMove(Host.Self.X + state.x * dist, Host.Self.Y + state.y * dist); Host.Self.UpdateCount++; } bool ret; if (state.remainingDist <= 0) { state.x = rand.Next(-1, 2); state.y = rand.Next(-1, 2); state.remainingDist = dist + dist * (float)(rand.NextDouble() * 0.1 - 0.05); ret = true; } else { ret = false; } return(ret); }
public void Sleep() { currentState = WanderingState.Sleeping; animator.SetInteger("Moving", 0); animator.SetBool("Sleeping", true); }