<commit_before>import { Entity, EntityContainer } from "../ecs/entities";
import { System } from "../ecs/system";
import { Position, Rotation } from "../systems/spatial";
import { Vec2, norm, norm2 } from "../vec2/vec2";

export class MoveToTarget
{
  target : Vec2;
  static readonly t : string = "MoveToTarget";
};

export class MoveTo implements System
{
  constructor(private entities : EntityContainer, private speed : number) {}

  update(dt : number) : void
  {
    this.entities.forEachEntity([Position.t, Rotation.t, MoveToTarget.t], (e : Entity, components : any[]) =>
    {
      let [position, rotation, target] = components as [Position, Rotation, MoveToTarget];
      if (!target.target)
        return;

      const toTarget = target.target.clone().subtract(position.pos);
      if (norm2(toTarget) > 5)
      {
        rotation.angle = toTarget.angle();
        position.pos.add(toTarget.normalized().multiply( Math.min(norm(toTarget), this.speed * dt) ));
      }
      else
      {
        target.target = null;
      }
    });<commit_msg>Quick fix-up of the move-to-target behavior<commit_after>import { Entity, EntityContainer } from "../ecs/entities";
import { System } from "../ecs/system";
import { Position, Rotation } from "../systems/spatial";
import { Vec2, norm, norm2 } from "../vec2/vec2";

export class MoveToTarget
{
  target : Vec2;
  static readonly t : string = "MoveToTarget";
};

export class MoveTo implements System
{
  constructor(private entities : EntityContainer, private speed : number) {}

  update(dt : number) : void
  {
    this.entities.forEachEntity([Position.t, Rotation.t, MoveToTarget.t], (e : Entity, components : any[]) =>
    {
      let [position, rotation, target] = components as [Position, Rotation, MoveToTarget];
      if (!target.target)
        return;

      const toTarget = target.target.clone().subtract(position.pos);
      if (norm2(toTarget) > 0.5)
      {
        rotation.angle = toTarget.angle();
        position.pos.add(toTarget.normalized().multiply( Math.min(norm(toTarget), this.speed * dt) ));
      }
      else
      {
        target.target = null;
      }
    });
{'fraction_non_alphanumeric': 0.1435228331780056, 'fraction_numerical': 0.006989748369058714, 'mean_word_length': 3.050943396226415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6775127', 'n_tokens_mistral': 650, 'n_tokens_neox': 643, 'n_words': 161}