private ItemParams GetTarget() { // TODO: How do we handle focusing on the foreground or background Vector2 rayDirection = ((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - RayOrigin).normalized; float rayDistance = Mathf.Min(((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - RayOrigin).magnitude, PlayerVars.Reach); RaycastHit2D hit = Utils.Raycast(RayOrigin, rayDirection, rayDistance, CollisionMask, Color.red); // Once we get the focus raycast we need to determine what we have hit (tile, entity, etc) // Once we know the target type we can collect required data into a container if (hit.collider != null) { Vector2 targetPosition = hit.point + (-hit.normal * 0.1f); Vector2 adjacentPosition = hit.point + (hit.normal * 0.1f); // Check for tile collision TileBasic tile = TilemapManager.Instance.TerrainTilemap .GetTile <TileBasic>(TilemapManager.Instance.TerrainTilemap.WorldToCell(targetPosition)); if (tile != null) { return(new ItemParams { TargetType = TargetType.Tile, TileClass = tile.Item.Class, TargetPosition = targetPosition, AdjacentPosition = adjacentPosition, OriginPosition = Center, HitDirection = _normalToHitDirection[hit.normal], Damage = PlayerVars.BaseDamage }); } // TODO: Check for Background collision // TODO: Check for entity collision } return(new ItemParams { TargetType = TargetType.None, TargetPosition = RayOrigin + rayDirection * rayDistance, AdjacentPosition = RayOrigin + rayDirection * rayDistance, OriginPosition = Center }); }
public static void DestroyTile(this Tilemap tilemap, Vector3Int position) { TileBasic tile = tilemap.GetTile <TileBasic>(position); if (tile != null) { WorldItem worldItem = WorldItem.CreateWorldItem(new InstanceItem(tile.Item), tilemap.CellToWorld(position)); tilemap.SetTile(position, null); } }
public static bool PlaceTile(this Tilemap tilemap, Vector3Int position, ItemTile item) { if (!tilemap.HasTile(position) && item != null) { TileBasic tile = ScriptableObject.CreateInstance <TileBasic>().Initialise(item); tilemap.SetTile(position, tile); return(true); } return(false); }
public void DamageTile(TileLayer layer, Vector3Int position, int amount, HitDirection hitDirection) { Tilemap tilemap = GetTilemap(layer); TileBasic tile = tilemap.GetTile <TileBasic>(position); if (tile != null) { if (!_terrainWorldTiles.ContainsKey(position)) { // Check if the tile is destroyed instantly if (amount > tile.Item.WorldTileDamage) { WorldItem worldItem = WorldItem.CreateWorldItem(new InstanceItem(tile.Item), tilemap.CellToWorld(position)); tilemap.SetTile(position, null); } // else make a worldtile and apply damage else { _terrainWorldTiles.Add(position, new WorldTile { Damage = amount }); // Create a break effect tile to represent damage EffectTilemap.SetTile(position, CreateBreakEffectTile(hitDirection, amount, tile.Item.WorldTileDamage)); } } else { // Get world tile from dictionary and apply damage _terrainWorldTiles[position].Damage += amount; // check for tile destruction if (_terrainWorldTiles[position].Damage >= tile.Item.WorldTileDamage) { _terrainWorldTiles.Remove(position); WorldItem worldItem = WorldItem.CreateWorldItem(new InstanceItem(tile.Item), tilemap.CellToWorld(position)); tilemap.SetTile(position, null); EffectTilemap.SetTile(position, null); } else { // Update the break effect tile EffectTilemap.SetTile(position, CreateBreakEffectTile(hitDirection, _terrainWorldTiles[position].Damage, tile.Item.WorldTileDamage)); } } } }