protected override void OnUpdate() { Entity eClicked = Entity.Null; Entities.ForEach((Entity entity, in UIState state) => { if (state.IsClicked) { eClicked = entity; } }).Run(); if (eClicked != null && eClicked != Entity.Null) { return; } var input = World.GetExistingSystem <InputSystem>(); var pos = input.GetInputPosition(); var s2w = World.GetExistingSystem <ScreenToWorld>(); short s = -1; var cam = GetSingletonEntity <Camera>(); var worldPos = s2w.ScreenSpaceToWorldSpace(pos, s, cam); pos = new float2(worldPos.x, worldPos.y); Entity nearSquareaEntity = Entity.Null; float distance = 10000; Entities .WithAll <Squarea>() .ForEach((Entity e, in Translation trans) => { float2 p = new float2(trans.Value.x, trans.Value.y); float d = math.distance(p, pos); if (d < distance) { nearSquareaEntity = e; distance = d; } }).Run(); if (nearSquareaEntity != null && nearSquareaEntity != Entity.Null) { Squarea nearSquarea = GetComponent <Squarea>(nearSquareaEntity); var selected = GetSingleton <SelectedSquareaEntity>(); if (distance < 0.5f) { selected.Value = nearSquareaEntity; } else { selected.Value = Entity.Null; } SetSingleton <SelectedSquareaEntity>(selected); } }
public Node(Squarea squarea, int2 goal, Node parentNode) { parent = parentNode; position = squarea.pos; realCost = parent.realCost + 1; maybeCost = math.abs(position.x - goal.x) + math.abs(position.y - goal.y); totalCost = realCost + maybeCost; isLock = squarea.isLock; }
public Node(Squarea squarea, int2 goal) { parent = new NullNode(); position = squarea.pos; realCost = 0; maybeCost = math.abs(position.x - goal.x) + math.abs(position.y - goal.y); totalCost = realCost + maybeCost; isLock = squarea.isLock; }
public AStar(int2 _start, int2 _goal, List <Squarea> _squareas) { start = _start; goal = _goal; // // noneList = new List<Squarea>(SquareaList); // foreach(Squarea s in SquareaList) // { // noneList.Add(s); // } noneList = _squareas; Squarea startSquarea = new Squarea(); foreach (Squarea s in noneList) { if (s.pos.x == start.x && s.pos.y == start.y) { startSquarea = s; } } // noneList // .Where(s => s.pos.x == start.x && s.pos.y == start.y) // .First(); Node StartNode = new Node(startSquarea, goal); openList.Add(StartNode); noneList.Remove(startSquarea); while (openList.Count > 0) { // openList.Sort((a,b)=>(a.totalCost - b.totalCost)); // Node first = openList[0]; // openList.OrderBy(s => s.totalCost); // Node first = openList.First(); Node first = openList[0]; for (int i = 1; i < openList.Count; i++) { if (openList[i].totalCost < first.totalCost) { first = openList[i]; } } Open(first); } }