/** * Checks if task performer has reached task target. Does not check target availability (map area). * Returns fail if checked from out of map. */ public ActionTargetStatusEnum check(EcsEntity performer) { Vector3Int performerPosition = performer.pos(); Vector3Int?target = getPosition(); if (!target.HasValue) { return(READY); // target without position } int distance = getDistance(performerPosition, target.Value); if (distance > 1) { return(WAIT); // target not yet reached } switch (type) { case ActionTargetTypeEnum.EXACT: return(distance == 0 ? READY : WAIT); case ActionTargetTypeEnum.NEAR: return(distance == 1 ? READY : STEP_OFF); case ActionTargetTypeEnum.ANY: return(READY); // distance is 0 or 1 here } return(FAIL); }
private EcsEntity createIdleTask(EcsEntity unit) { // Debug.Log("creating idle task for unit " + unit); Vector3Int current = unit.pos(); Vector3Int position = GameModel.localMap.util.getRandomPosition(current, 10, 4); return(GameModel.get().taskContainer.createTask(new MoveAction(position))); }
private void createSpriteForItem(EcsEntity entity) { ItemComponent item = entity.Get <ItemComponent>(); ItemVisualComponent visual = new ItemVisualComponent(); Vector3 spritePosition = ViewUtil.fromModelToScene(entity.pos()); visual.go = Object.Instantiate(itemPrefab, spritePosition + new Vector3(0, 0, -0.1f), Quaternion.identity); visual.go.transform.SetParent(GameView.get().mapHolder); visual.spriteRenderer = visual.go.GetComponent <SpriteRenderer>(); visual.spriteRenderer.sprite = createSprite(ItemTypeMap.getItemType(item.type)); entity.Replace(visual); }
public void registerItem(EcsEntity item) { validateForPlacing(item); Vector3Int position = item.pos(); if (!itemsOnMap.ContainsKey(position)) { itemsOnMap.Add(position, new List <EcsEntity>()); } itemsOnMap[position].Add(item); all.Add(item); }
public void takeItemFromMap(EcsEntity item) { validateForTaking(item); Vector3Int position = item.pos(); itemsOnMap[position].Remove(item); all.Remove(item); item.Del <PositionComponent>(); if (itemsOnMap[position].Count == 0) { itemsOnMap.Remove(position); // remove empty list } }
// validate that item is registered on its position private void validateForTaking(EcsEntity item) { if (!item.hasPos()) { Debug.LogWarning("Item " + item + " has no position."); } Vector3Int position = item.pos(); if (!itemsOnMap.ContainsKey(position)) { Debug.LogWarning("Tile on " + item + " position is empty."); } if (!itemsOnMap[position].Contains(item)) { Debug.LogWarning("Item " + item + " is not registered by its position."); } }
// gets any task for unit private EcsEntity?getTaskFromContainer(EcsEntity unit) { if (unit.Has <UnitJobsComponent>()) { UnitJobsComponent jobs = unit.Get <UnitJobsComponent>(); Debug.Log("enabled jobs: " + jobs.enabledJobs.Count); foreach (var enabledJob in jobs.enabledJobs) // TODO add jobs priorities { EcsEntity?task = GameModel.get().taskContainer.getTask(enabledJob, unit.pos()); if (task.HasValue) { return(task); } } } else { Debug.LogError("unit without jobs component attempts to get jobs from container."); } return(null); // TODO get from task container }
private float distanceToItem(EcsEntity item, Vector3Int position) { return(Vector3Int.Distance(item.pos(), position)); }