public void MoveTo(int x, int y, bool ignoreblocking = false) { SetupPathFinder(); if (Map.Map[x, y].Blocked && !ignoreblocking) { _currentpath = null; return; } TargetX = x; TargetY = y; var position = CordUtil.WorldToTile(transform.position); var currentx = position.First; var currenty = position.Second; Pathfinder.ResultFound = r => { _currentpath = r; if (_currentpath != null) { _nextworldpos = CordUtil.TileToWorld(_currentpath.X, _currentpath.Y); } }; _currentpath = null; Pathfinder.FindPath(currentx, currenty, x, y, this); }
public void FixedUpdate() { if (ConstructingUnit != null) { ConstructionTimeLeft -= Time.deltaTime; if (ConstructionTimeLeft <= 0f) { ConstructingUnit.gameObject.SetActive(true); ConstructingUnit = null; GetComponent <SpriteRenderer>().sprite = CompletedSprite; } } if (QueueTimeLeft > 0) { QueueTimeLeft -= Time.fixedDeltaTime; } else if (CurrentQueuedItem != QueueType.None) { var coords = CordUtil.WorldToTile(transform.position); var freespot = _map.FindNextFreeTile(coords.First, coords.Second); _unitfactory.Create(QueuedUnit, PlayerOwner, freespot.First, freespot.Second - 2); CurrentQueuedItem = QueueType.None; } if (HP <= 0) { //Building dies Destroy(gameObject); } }
public void Initialize() { _tileClick.Event += (btn, x, y) => { if (btn != 0) { return; } if (!_sessionModifiers.PlacingBuilding) { return; } var worldcords = _camera.ScreenToWorldPoint(Input.mousePosition); var tilecords = CordUtil.WorldToTile(worldcords); var obj = _buildingFactory.Create(_sessionModifiers.SelectedBuildingType, 0, tilecords.First, tilecords.Second); _selectionManager.PrimaryUnit.TargetBuilding = obj.GetComponent <BuildingController>(); _selectionManager.PrimaryUnit.GetComponent <PathFinderFollower>().MoveTo(tilecords.First, tilecords.Second); _sessionModifiers.PlacingBuilding = false; }; }
public GameObject Create(BuildingType type, int team, int x, int y) { var building = Container.CreateEmptyGameObject("Building"); var selection = Container.CreateEmptyGameObject("SelectionBox"); selection.transform.SetParent(building.transform); selection.transform.localScale = new Vector3(2, 2, 1); building.tag = "Building"; var render = Container.InstantiateComponent <SpriteRenderer>(building); render.sprite = _buildingSprite; render.sortingOrder = 1; var collider = Container.InstantiateComponent <BoxCollider2D>(building); var controller = Container.InstantiateComponent <BuildingController>(building); var selectrender = Container.InstantiateComponent <SpriteRenderer>(selection); selectrender.sprite = _SelectionSprite; collider.isTrigger = true; building.transform.position = CordUtil.TileToWorld(x, y); if (team != 0 && type != BuildingType.Farm) { var unitbuilder = Container.InstantiateComponent <UnitBuilderAI>(building); unitbuilder.Cooldown = 30f; unitbuilder.Type = type == BuildingType.TownCenter ? UnitType.Worker : UnitType.Warrior; } controller.PrimaryActionList = GetPrimaryActionList(type); controller.HP = 1; controller.MaxHP = 1; controller.PlayerOwner = team; controller.SelectionBox = selection; controller.ConstructionTimeLeft = 10f; controller.CompletedSprite = type == BuildingType.TownCenter ? _cityCenter : type == BuildingType.Farm ? _farm : _barracks; if (team != 0) { render.sprite = controller.CompletedSprite; } building.SetActive(true); selection.SetActive(false); return(building); }
public void Attack(UnitController unit) { TargeType = TragetType.Combat; CombatUnitTarget = unit; var location = CordUtil.WorldToTile(unit.transform.position); _pathFinder.MoveTo(location.First, location.Second, true); _combatTargetX = location.First; _combatTargetY = location.Second; _pathFindCheckCount = 10f; }
void FixedUpdate() { if (_unit.TargeType != TragetType.Harvest) { var pos = CordUtil.WorldToTile(transform.position); var tree = FindNextTree(pos.First, pos.Second); if (tree == null) { enabled = false; return; } _unit.Harvest(tree.First, tree.Second); } }
void Start() { _current = Cooldown; var pos = CordUtil.WorldToTile(transform.position); _x = pos.First; _y = pos.Second - 2; if (_y < 0) { _y = pos.Second + 2; } _controller = GetComponent <BuildingController>(); }
void FixedUpdate() { SetupPathFinder(); if (_currentpath == null) { return; } if (Vector3.Distance(_nextworldpos, transform.position) <= _distanceBeforeNext) { if (_currentpath.Next == null) { _currentpath = null; return; } if (Map.Map[_currentpath.Next.X, _currentpath.Next.Y].Blocked) { MoveTo(TargetX, TargetY); return; } _currentpath = _currentpath.Next; SwapPosition(_currentpath.X, _currentpath.Y); if (_currentpath == null) { return; } _nextworldpos = CordUtil.TileToWorld(_currentpath.X, _currentpath.Y); } transform.position = Vector3.MoveTowards(transform.position, _nextworldpos, Speed * Time.fixedDeltaTime); }
public void MoveToTile(int x, int y) { var loc = CordUtil.TileToWorld(x, y); Camera.transform.position = new Vector3(loc.x, loc.y, -10); }
public Frame3f(Vector3 origin, Vector3 setZ) { rotation = CordUtil.FromTo(Vector3.UnitZ, setZ); this.origin = origin; }
public void FixedUpdate() { if (_currentcooldown > 0f) { _currentcooldown -= Time.fixedDeltaTime; } if (TargeType == TragetType.Harvest) { if (HarvestHP <= 0f) { TargeType = TragetType.None; var player = _playerManager.Players.First(p => p.ID == PlayerOwner); switch (_map.Map[HarvestTargetX, HarvestTargetY].ResourceType) { case MinableResourceType.None: break; case MinableResourceType.Food: player.Food += 10; break; case MinableResourceType.Rock: player.Stone += 10; break; case MinableResourceType.Wood: player.Wood += 10; break; default: throw new ArgumentOutOfRangeException(); } _map.Map[HarvestTargetX, HarvestTargetY].Decorators.Clear(); _map.Map[HarvestTargetX, HarvestTargetY].Type = TileTypes.Dirt; _map.RebuildTile(HarvestTargetX, HarvestTargetY); _map.Apply(); HarvestHP = 10f; } else { var playerloc = CordUtil.WorldToTile(transform.position); var xdiff = playerloc.First - HarvestTargetX; var ydiff = playerloc.Second - HarvestTargetY; if (xdiff == -1) { xdiff = 1; } if (ydiff == -1) { ydiff = 1; } if (xdiff <= 1 && ydiff <= 1) { HarvestHP -= Time.fixedDeltaTime; } } } if (CombatUnitTarget != null && _currentcooldown <= 0f) { var loc = CordUtil.WorldToTile(CombatUnitTarget.transform.position); if (loc.First != _combatTargetX || loc.Second != _combatTargetY) { Attack(CombatUnitTarget); } if (Vector3.Distance(transform.position, CombatUnitTarget.transform.position) < CombatDistance) { CombatUnitTarget.Hit(CombatDamage); _currentcooldown = CombatCooldown; } } if (HP <= 0) { //Unit dies Destroy(gameObject); } }
public GameObject Create(UnitType type, int team, int x, int y) { var unit = Container.CreateEmptyGameObject("Unit"); var head = Container.CreateEmptyGameObject("Head"); var body = Container.CreateEmptyGameObject("Body"); var selectionBox = Container.CreateEmptyGameObject("Selection"); unit.tag = "Unit"; head.transform.parent = unit.transform; body.transform.parent = unit.transform; selectionBox.transform.parent = unit.transform; head.transform.position = new Vector3(0, 0.148f, 0); var col = unit.AddComponent <BoxCollider2D>(); col.offset = new Vector2(-0.0025f, 0.0126f); col.size = new Vector2(0.2365f, 0.5196f); col.isTrigger = true; var controller = Container.InstantiateComponent <UnitController>(unit); var pathfinder = Container.InstantiateComponent <PathFinderFollower>(unit); controller.SelectionBox = selectionBox; controller.PlayerOwner = team; controller.HP = 1; controller.MaxHP = 1; controller.PrimaryActionList = GetPrimaryActions(type); if (team != 0 && type == UnitType.Worker) { Container.InstantiateComponent <HarvestAIController>(unit); } if (type == UnitType.Warrior) { controller.CanAttack = true; var attacksight = Container.CreateEmptyGameObject("Sight"); attacksight.transform.SetParent(unit.transform); var sightcol = Container.InstantiateComponent <CircleCollider2D>(attacksight); Container.InstantiateComponent <AttackSight>(attacksight); sightcol.radius = 2.5f; sightcol.isTrigger = true; attacksight.transform.position = new Vector3(attacksight.transform.position.x, attacksight.transform.position.y, 5); var spear = Container.CreateEmptyGameObject("Spear"); spear.transform.SetParent(unit.transform); var spearrender = Container.InstantiateComponent <SpriteRenderer>(spear); spearrender.sortingOrder = 5; spearrender.sprite = _spearSprite; spearrender.transform.position = new Vector3(0.06f, 0, 0); } if (type == UnitType.Worker) { controller.CanHarvest = true; } var headsprite = Container.InstantiateComponent <SpriteRenderer>(head); var bodysprite = Container.InstantiateComponent <SpriteRenderer>(body); var selectionsprite = Container.InstantiateComponent <SpriteRenderer>(selectionBox); var rbody = Container.InstantiateComponent <Rigidbody2D>(unit); rbody.gravityScale = 0f; rbody.freezeRotation = true; rbody.isKinematic = true; selectionsprite.sortingOrder = 3; headsprite.sortingOrder = 2; bodysprite.sortingOrder = 1; headsprite.sortingLayerName = "Units"; bodysprite.sortingLayerName = "Units"; selectionsprite.sortingLayerName = "Units"; selectionsprite.sprite = _selectionSprite; bodysprite.sprite = _teamBodies[team]; headsprite.sprite = _heads[0]; selectionsprite.gameObject.SetActive(false); head.gameObject.SetActive(true); body.gameObject.SetActive(true); unit.gameObject.SetActive(true); unit.transform.position = CordUtil.TileToWorld(x, y); pathfinder.CurrentX = x; pathfinder.CurrentY = y; pathfinder.SwapPosition(x, y); return(unit); }