public static BucketResult HasVegetalNutrimentsInBucket(Vector2Int position) { foreach (LayerGrid grid in Loki.map.grids.Values) { LayerGridBucket bucket = grid.GetBucketAt(position); if (bucket != null && bucket.properties.vegetalNutriments > 0f) { Tilable rt = WorldUtils.ClosestTilableFromEnum( position, bucket.tilables.Where( t => t != null && Loki.map[t.position].reserved == false && t.def.nutriments > 0 ) ); if (rt != null) { return(new BucketResult { result = true, bucket = bucket, tilable = rt }); } } } return(new BucketResult { result = false, bucket = null, tilable = null }); }
/// Spawn a tilable on the map public void Spawn(Vector2Int position, Tilable tilable, bool force = false) { if (force || tilable.def.layer == Layer.Undefined || this.GetTilableAt(position, tilable.def.layer) == null) { this.grids[tilable.def.layer].AddTilable(tilable); } }
public void DelTilable(Tilable tilable) { Vector2Int localPosition = this.GetLocalPosition(tilable.position); // Titlable properties if (tilable.def.type == TilableType.Grass) // Maybe we want some categories like "Food -> Vegetal Food" { if (tilable.def.nutriments > 0f) { this.properties.vegetalNutriments -= tilable.def.nutriments; this.properties.nutriments -= tilable.def.nutriments; } } // Remove all data this.tilables[localPosition.x + localPosition.y * this.rect.width] = null; Loki.map[tilable.position].Update(); if (tilable.def.type != TilableType.Undefined) { this.titlablesByType[tilable.def.type].Remove(tilable); if (this.titlablesByType[tilable.def.type].Count == 0) { this.titlablesByType.Remove(tilable.def.type); } } if (tilable.def.graphics.isInstanced) { this.rebuildMatrices = true; } }
public override bool Perform() { Tilable tilable = (Tilable)this.task.targets.current.tilable; this.character.stats.vitals[Vitals.Hunger].currentValue += tilable.def.nutriments * 100f; tilable.Destroy(); return(true); }
/// Get all tilable at a specific position. public IEnumerable <Tilable> GetAllTilablesAt(Vector2Int position) { foreach (LayerGrid grid in this.grids.Values) { Tilable tilable = grid.GetTilableAt(position); if (tilable != null) // Need to optimize this! { yield return(tilable); } } }
// Closest plant to dirt to the character position public override Task GetTask() { Tilable tilable = WorldUtils.FieldNextTileToDirt(this.character.position); if (tilable != null) { return(new Task( Defs.tasks["task_dirt"], new TargetList(new Target(tilable)) )); } return(null); }
public static bool FieldHasPlantsToCut() { foreach (GrowArea area in GrowArea.areas) { foreach (Vector2Int position in area.positions) { Tilable tilable = Loki.map.grids[Layer.Plant].GetTilableAt(position); if (!Loki.map[position].reserved && tilable != null && tilable.def != area.plantDef && tilable.def.cuttable) { return(true); } } } return(false); }
public override TaskData GetTaskData() { Tilable tilable = WorldUtils.FieldNextTileToSow(this.character.position); if (tilable != null) { return(new TaskData( Defs.tasks["task_sow"], new TargetList(new Target(tilable)), this.character )); } return(null); }
public override Task GetTask() { // Closest plant to cut to the character position Tilable tilable = WorldUtils.FieldNextToCut(this.character.position); if (tilable != null) { return(new Task( Defs.tasks["task_cut"], new TargetList(new Target(tilable)) )); } return(null); }
public static bool FieldHasPlantsToSow() { foreach (GrowArea area in GrowArea.areas) { foreach (Vector2Int position in area.positions) { Tilable tilable = Loki.map.grids[Layer.Plant].GetTilableAt(position); Field field = (Field)Loki.map.grids[Layer.Helpers].GetTilableAt(position); if (!Loki.map[position].reserved && tilable == null && field.dirt == true) { return(true); } } } return(false); }
public static Tilable FieldNextToCut(Vector2Int playerPosition) { List <Tilable> toCut = new List <Tilable>(); foreach (GrowArea area in GrowArea.areas) { foreach (Vector2Int position in area.positions) { Tilable tilable = Loki.map.grids[Layer.Plant].GetTilableAt(position); if (!Loki.map[position].reserved && tilable != null && tilable.def != area.plantDef) { toCut.Add(tilable); } } } return(WorldUtils.ClosestTilableFromEnum(playerPosition, toCut)); }
public static Tilable ClosestTilableFromEnum(Vector2Int position, IEnumerable <Tilable> tilables) { Tilable result = null; float minDistance = float.MaxValue; foreach (Tilable tilable in tilables) { float currentMinDistance = Utils.Distance(position, tilable.position); if (currentMinDistance < minDistance) { minDistance = currentMinDistance; result = tilable; } } return(result); }
public static Tilable FieldNextTileToSow(Vector2Int playerPosition) { List <Tilable> toSow = new List <Tilable>(); foreach (GrowArea area in GrowArea.areas) { foreach (Vector2Int position in area.positions) { Tilable tilable = Loki.map.grids[Layer.Plant].GetTilableAt(position); Field field = (Field)Loki.map.grids[Layer.Helpers].GetTilableAt(position); if (!Loki.map[position].reserved && tilable == null && field.dirt == true) { toSow.Add((Tilable)field); } } } return(WorldUtils.ClosestTilableFromEnum(playerPosition, toSow)); }
public void AddTilable(Tilable tilable) { // Maybe we set the local position for the tilable Vector2Int localPosition = this.GetLocalPosition(tilable.position); this.tilables[localPosition.x + localPosition.y * this.rect.width] = tilable; tilable.SetBucket(this); Loki.map[tilable.position].Update(); // Add to tilableByType dictionary if (tilable.def.type != TilableType.Undefined) { if (!this.titlablesByType.ContainsKey(tilable.def.type)) { this.titlablesByType.Add(tilable.def.type, new HashSet <Tilable>()); } this.titlablesByType[tilable.def.type].Add(tilable); } // Titlable properties if (tilable.def.type == TilableType.Grass) // Maybe we want some categories like "Food -> Vegetal Food" { if (tilable.def.nutriments > 0f) { this.properties.vegetalNutriments += tilable.def.nutriments; this.properties.nutriments += tilable.def.nutriments; } } // Add matrice to list if isInstanced. if (tilable.def.graphics.isInstanced) { this.AddMatrice(tilable.mainGraphic.uid, tilable.GetMatrice(tilable.mainGraphic.uid)); if (tilable.addGraphics != null) { foreach (GraphicInstance graphicInstance in tilable.addGraphics.Values) { this.AddMatrice(graphicInstance.uid, tilable.GetMatrice(graphicInstance.uid)); } } this.rebuildMatrices = true; } }
public void DropOnTheFloor() { if (this.inventory.count > 0 && this.inventory.def != null) { HashSet <Vector2Int> tilablesInRadius = new HashSet <Vector2Int>(); Stackable stack = (Stackable)Loki.map.GetTilableAt(this.position, Layer.Stackable); if (stack == null) { Loki.map.Spawn(position, new Stackable( this.position, this.inventory.def, 0 )); } stack = (Stackable)Loki.map.GetTilableAt(this.position, Layer.Stackable); Tilable.InRadius(20, stack.position, stack.position, ref tilablesInRadius); foreach (Vector2Int position in tilablesInRadius) { if (this.inventory.count == 0) { break; } stack = (Stackable)Loki.map.GetTilableAt(position, Layer.Stackable); if (stack != null && stack.def == this.inventory.def) { this.inventory.TransfertTo(stack.inventory, stack.inventory.free); } else if (stack == null) { Loki.map.Spawn(position, new Stackable( position, this.inventory.def, 0 )); stack = (Stackable)Loki.map.GetTilableAt(position, Layer.Stackable); this.inventory.TransfertTo(stack.inventory, stack.inventory.free); } } } }
public Target(Tilable tilable) : this(tilable.position) { this.tilable = tilable; }
public TargetList(Tilable tilable) { this.Enqueue(tilable); this.Next(); }
public void Enqueue(Tilable tilable) { Loki.map[tilable.position].reserved = true; this.targets.Enqueue(new Target(tilable)); }
public void AddTilable(Tilable tilable) { this.GetBucketAt(tilable.position).AddTilable(tilable); }
/// Build meshes for the ground public override void BuildMeshes() { List <int> neighboursGraphicsList = new List <int>(); int[] neighboursGraphics = new int[8]; Color[] colors = new Color[9]; foreach (Tilable ground in this.bucket.tilables) { if (ground.hidden) { continue; } neighboursGraphicsList.Clear(); MeshData currentMesh = this.GetMesh(ground.mainGraphic.uid, false, (MeshFlags.Base | MeshFlags.Color)); int vIndex = currentMesh.vertices.Count; float z = ground.mainGraphic.priority; currentMesh.vertices.Add(new Vector3(ground.position.x, ground.position.y, z)); currentMesh.vertices.Add(new Vector3(ground.position.x, ground.position.y + 1, z)); currentMesh.vertices.Add(new Vector3(ground.position.x + 1, ground.position.y + 1, z)); currentMesh.vertices.Add(new Vector3(ground.position.x + 1, ground.position.y, z)); currentMesh.colors.Add(Color.white); currentMesh.colors.Add(Color.white); currentMesh.colors.Add(Color.white); currentMesh.colors.Add(Color.white); currentMesh.AddTriangle(vIndex, 0, 1, 2); currentMesh.AddTriangle(vIndex, 0, 2, 3); for (int i = 0; i < DirectionUtils.neighbours.Length; i++) { // Do this better! Tilable neighbourGround = Loki.map.GetTilableAt(ground.position + DirectionUtils.neighbours[i], Layer.Ground); if (neighbourGround != null) { neighboursGraphics[i] = neighbourGround.mainGraphic.uid; if ( !neighboursGraphicsList.Contains(neighbourGround.mainGraphic.uid) && neighbourGround.mainGraphic.uid != ground.mainGraphic.uid && neighbourGround.def.groundDef.maxHeight >= ground.def.groundDef.maxHeight ) { neighboursGraphicsList.Add(neighbourGround.mainGraphic.uid); } } else { neighboursGraphics[i] = ground.mainGraphic.uid; } } foreach (int graphicUID in neighboursGraphicsList) { currentMesh = this.GetMesh(graphicUID, false, (MeshFlags.Base | MeshFlags.Color)); vIndex = currentMesh.vertices.Count; z = GraphicInstance.instances[graphicUID].priority; currentMesh.vertices.Add(new Vector3(ground.position.x + .5f, ground.position.y, z)); // 0 currentMesh.vertices.Add(new Vector3(ground.position.x, ground.position.y, z)); // 1 currentMesh.vertices.Add(new Vector3(ground.position.x, ground.position.y + .5f, z)); // 2 currentMesh.vertices.Add(new Vector3(ground.position.x, ground.position.y + 1, z)); // 3 currentMesh.vertices.Add(new Vector3(ground.position.x + .5f, ground.position.y + 1, z)); // 4 currentMesh.vertices.Add(new Vector3(ground.position.x + 1, ground.position.y + 1, z)); // 5 currentMesh.vertices.Add(new Vector3(ground.position.x + 1, ground.position.y + .5f, z)); // 6 currentMesh.vertices.Add(new Vector3(ground.position.x + 1, ground.position.y, z)); // 7 currentMesh.vertices.Add(new Vector3(ground.position.x + .5f, ground.position.y + .5f, z)); // 8 for (int i = 0; i < colors.Length; i++) { colors[i] = Color.clear; } for (int i = 0; i < 8; i++) { if (i % 2 != 0) // if it's odd { if (graphicUID == neighboursGraphics[i]) { colors[i] = Color.white; } } else { if (graphicUID == neighboursGraphics[i]) { switch (i) { case 0: // South colors[1] = Color.white; colors[0] = Color.white; colors[7] = Color.white; break; case 2: // West colors[1] = Color.white; colors[2] = Color.white; colors[3] = Color.white; break; case 4: // North colors[3] = Color.white; colors[4] = Color.white; colors[5] = Color.white; break; case 6: // East colors[5] = Color.white; colors[6] = Color.white; colors[7] = Color.white; break; } } } } currentMesh.colors.AddRange(colors); currentMesh.AddTriangle(vIndex, 0, 8, 6); currentMesh.AddTriangle(vIndex, 0, 6, 7); currentMesh.AddTriangle(vIndex, 1, 8, 0); currentMesh.AddTriangle(vIndex, 1, 2, 8); currentMesh.AddTriangle(vIndex, 2, 4, 8); currentMesh.AddTriangle(vIndex, 2, 3, 4); currentMesh.AddTriangle(vIndex, 8, 5, 6); currentMesh.AddTriangle(vIndex, 8, 4, 5); } } foreach (MeshData meshData in this.meshes.Values) { meshData.Build(); } }