public bool CanPlacePiece(Vector2Int coords, LevelBoard.Directions orientation, Piece piece) { // Get Piece's Footprint var footprint = piece.GetFootprintAt(coords, orientation); // Foreach Space the Piece would occupy foreach (var partCoords in footprint) { // If the Space is Out Of Bounds return false if (this.OutOfBounds(partCoords)) { return(false); } // Get the Piece presently at that Space (if any) var pieceInSpace = this.GetPiece(partCoords); // If the Space is occupied and not by this Piece if (pieceInSpace != null && pieceInSpace != piece) { return(false); } } return(true); }
public override bool CommunicateInfo(Vector2Int crossingCoords, LevelBoard.Directions dirIn, LevelBoard.Directions dirOut) { base.CommunicateInfo(crossingCoords, dirIn, dirOut); var connected = false; // If the Slime has come from a Crossing if (this.CrossingLog.Count > 0) { var last = this.CrossingLog.Peek(); // Get last visited Crossing // If the last visited Crossing is not the present Crossing if (last.Key != crossingCoords) { // Add this Crossing as the last one's child connected = this.RegisterCrossingConnection(last.Key, last.Value, crossingCoords, dirIn); } else if (this.DeadEnds && !this.PathBlocked) { this.RegisterDeadEnd(crossingCoords, LevelBoard.InvertDirection(dirIn)); connected = true; } this.PathBlocked = false; } // Save this Crossing as its most recently visited, and Direction from which it left this.CrossingLog.Push(new KeyValuePair <Vector2Int, LevelBoard.Directions>(crossingCoords, dirOut)); return(connected); }
public void RemoveConnection(LevelBoard.Directions dir) { if (this.Connections.ContainsKey(dir)) { this.Connections.Remove(dir); } }
public override void RollBackInfo(Vector2Int crossingCoords, LevelBoard.Directions dirIn, LevelBoard.Directions dirOut, bool connected) { // Pop last Log entry this.CrossingLog.Pop(); // If the Slime had come from a Crossing and a connection was made if (this.CrossingLog.Count > 0 && connected) { var last = this.CrossingLog.Peek(); // Get last visited Crossing // If the last visited Crossing is not the present Crossing if (last.Key != crossingCoords) { // Disconnect the Crossings this.UnregisterCrossingConnection(last.Key, last.Value, crossingCoords, dirIn); } else if (this.DeadEnds) { this.UnregisterDeadEnd(crossingCoords, LevelBoard.InvertDirection(dirIn)); } } base.RollBackInfo(crossingCoords, dirIn, dirOut, connected); }
public void RegisterExplored(Vector2Int crossingCoords, LevelBoard.Directions exploredPath) { this.Crossings.TryGetValue(crossingCoords, out var crossing); // Should never happen if (crossing == null) { return; } crossing.MarkExplored(exploredPath); /*Debug.Log("Register Explored - " + crossingCoords + " -> " + exploredPath); * * var utilities = crossing.GetUtilities(); * * var utils = "{"; * * foreach (var pair in utilities) * { * utils += " (" + pair.Key + ", " + pair.Value + ")"; * } * * Debug.Log(crossingCoords + " Utils: " + utils);*/ }
public override bool Confirm(Agent agent) { // If the Agent can be moved to the new Coords if (agent.CanMove(this.MoveCoords)) { this.OrigOrientation = agent.Orientation; // Save current Orientation // Rotate the Agent in the Board if (agent.RotateInBoard(this.Direction)) { // Move the Agent in the Board if (agent.MoveInBoard(this.MoveCoords)) { return(true); } // If it fails, Undo the rotation else { agent.RotateInBoard(this.OrigOrientation); } } } return(false); }
public static Piece CreatePiece(Puzzle puzzle, string prefabName, Vector2Int coords, LevelBoard.Directions ori = LevelBoard.Directions.South, int turn = 0) { GameObject obj = Instantiate(puzzle, prefabName, coords); obj.name = prefabName; var piece = obj.GetComponent <Piece>(); piece.Rotate(ori); // Agent Init if (piece is Agent agent) { // Slime Init if (agent is Pieces.Slimes.Slime slime) { slime.Initialize(puzzle, coords, new Characteristics(prefabName), ori, turn); } // Component Init else if (agent is Pieces.Components.CircuitComponent component) { component.Initialize(puzzle, coords, new Characteristics(prefabName), ori, turn); } } // Piece Init else { piece.Initialize(puzzle, coords, new Characteristics(prefabName)); } return(piece); }
public void UnregisterDeadEnd(Vector2Int crossingCoords, LevelBoard.Directions deadEnd) { this.Crossings.TryGetValue(crossingCoords, out var crossing); // Should never happen if (crossing == null) { return; } crossing.UnmarkDeadEnd(deadEnd); /*Debug.Log("Unregister Dead End - " + crossingCoords + " -> " + deadEnd); * * var utilities = crossing.GetUtilities(); * * var utils = "{"; * * foreach (var pair in utilities) * { * utils += " (" + pair.Key + ", " + pair.Value + ")"; * } * * Debug.Log(crossingCoords + " Utils: " + utils);*/ }
public virtual void RollBackInfo(Vector2Int crossingCoords, LevelBoard.Directions dirIn, LevelBoard.Directions dirOut, bool connected) { // Unregister path by which the present Crossing was reached this.UnregisterExploredPath(crossingCoords, LevelBoard.InvertDirection(dirIn)); // Unregister path chosen to exit present Crossing this.UnregisterExploredPath(crossingCoords, dirOut); }
// Init Method public override void Initialize(Puzzle puzzle, Vector2Int coords, Characteristics characterization, LevelBoard.Directions ori = 0, int turn = 0) { base.Initialize(puzzle, coords, characterization, ori, turn); this.Charges = new List <ElectricSlime>(); this.Connections = new Dictionary <LevelBoard.Directions, Vector2Int>(); }
public void UnmarkExplored(LevelBoard.Directions dir) { if (!this.Explorations.ContainsKey(dir)) { this.Explorations.Add(dir, 1); } this.Explorations[dir] = this.Explorations[dir] - 1; }
public bool AddConnection(LevelBoard.Directions dir, Crossing other) { if (this.Explorations.ContainsKey(dir) && !this.Connections.ContainsKey(dir)) { this.Connections.Add(dir, other); return(true); } return(false); }
public virtual bool CommunicateInfo(Vector2Int crossingCoords, LevelBoard.Directions dirIn, LevelBoard.Directions dirOut) { // Register path chosen to exit present Crossing this.RegisterExploredPath(crossingCoords, dirOut); // Register path by which the present Crossing was reached this.RegisterExploredPath(crossingCoords, LevelBoard.InvertDirection(dirIn)); return(true); }
public void MarkDeadEnd(LevelBoard.Directions dir) { if (!this.Explorations.ContainsKey(dir)) { this.Explorations.Add(dir, 0); } if (!this.DeadEnds.Contains(dir)) { this.DeadEnds.Add(dir); } }
public SeekTarget(Vector2Int moveCoords, LevelBoard.Directions direction, Piece target) { this.Direction = direction; this.Target = target; this.TargetCoords = target.Coords; this.TargetCharacteristics = new Piece.Characteristics(target.Characterization.ToString()); this.MoveCoords = moveCoords; }
//Calculate footprint in world coords private Vector2Int[] CalculateFootprint(Vector2Int coords, LevelBoard.Directions orientation) { //SINGLE TILE if (this.Footprint.Length == 1) { return new Vector2Int[] { coords } } ; //MULTI-TILE var footprint = new Vector2Int[this.Footprint.Length]; for (var i = 0; i < footprint.Length; i++) { //rotate footprint to match orientation var xx = this.Footprint[i].x; var yy = this.Footprint[i].y; switch (orientation) { // 0 degree rotation case LevelBoard.Directions.East: default: footprint[i].x = xx; footprint[i].y = yy; break; // +90 degree rotation case LevelBoard.Directions.North: footprint[i].x = yy; footprint[i].y = -xx; break; // +180 degree rotation case LevelBoard.Directions.West: footprint[i].x = -xx; footprint[i].y = -yy; break; // -90 degree rotation case LevelBoard.Directions.South: footprint[i].x = -yy; footprint[i].y = xx; break; } //apply footprint to world coord point footprint[i] = coords + footprint[i]; } return(footprint); }
public void InitializeMovingItem(Transform selected) { Option.InitiliazeSprite(this.MovingSprite, selected.name); this.MovigStartPos = this.Puzzle.Discretize(selected.position); this.MovingOption.Initialize(this, selected.name, this.MovingText, this.MovingDrag, this.Mode.AbleToEditOptions()); this.MovingDrag.InitializeDrag(); Piece piece = selected.GetComponentInChildren <Piece>(); if (piece != null) { this.direction = piece.Orientation; } }
private void SaveSmartInfo(Agent agent) { // If this is a Crossing if (this.Crossing && agent is SmartElectricSlime smart) { // Save the Direction the Slime reached the Crossing from this.OrigOrientation = smart.Orientation; // Save Direction Slime entered component by/left crossing from this.WentTo = LevelBoard.GetDirection(smart.Coords, this.ComponentCoords); this.RegisteredConnection = smart.CommunicateInfo(this.ChargeCoords, this.OrigOrientation, this.WentTo); } }
//rotate piece public virtual bool Rotate(LevelBoard.Directions targetDir) { float targetAngle = 360 - ((float)targetDir) * 45f; if (targetAngle == 360) { targetAngle = 0; } this.transform.eulerAngles = new Vector3(this.transform.eulerAngles.x, targetAngle, this.transform.eulerAngles.z); this.Orientation = targetDir; return(true); }
public void RemoveCrossingConnection(Vector2Int startCoords, LevelBoard.Directions dirOut, Vector2Int endCoords, LevelBoard.Directions dirIn) { if (this.Crossings.TryGetValue(startCoords, out var start) && this.Crossings.TryGetValue(endCoords, out var end)) { start.RemoveConnection(dirOut); var invDir = LevelBoard.InvertDirection(dirIn); end.RemoveConnection(invDir); Debug.Log("Removed connection between " + startCoords + " through " + dirOut + " to " + endCoords); Debug.Log("Removed connection between " + endCoords + " through " + invDir + " to " + startCoords); } }
private void InitializeMovingItem() { Object prefab = UnityEngine.Resources.Load(FileHelper.OPTION_PATH); GameObject item = Option.CreateOption(this, null, prefab, this.Controller.transform.Find("Canvas"), "", false); item.name = "MovingItem"; Transform resource = item.transform.Find("Resource"); this.MovingSprite = resource.Find("Sprite"); this.MovingOption = item.GetComponent <Option>(); this.MovingDrag = this.MovingSprite.GetComponent <Draggable>(); this.MovingText = resource.Find("Amount").GetComponent <Text>(); this.direction = LevelBoard.Directions.South; }
// Init Method virtual public void Initialize(Puzzle puzzle, Vector2Int coords, Characteristics characterization, LevelBoard.Directions ori = 0, int turn = 0) { base.Initialize(puzzle, coords, characterization); this.State = States.Idle; this.KnownActions = new List <Action>(); this.ActionLog = new Dictionary <int, Action>(); this.Orientation = ori; this.StartTurn = turn; this.Turn = turn; }
public Vector2Int RouteEnergy(LevelBoard.Directions entryDir) { int dirId = ((int)entryDir + 6) % 8; for (int i = 0; i < 4; i++) { LevelBoard.Directions checkDir = (LevelBoard.Directions)dirId; if (this.Connections.ContainsKey(checkDir) && this.IsFree(this.Connections[checkDir])) { return(this.Connections[checkDir]); } dirId += 2; dirId %= 8; } return(new Vector2Int(-1, -1)); }
public override bool Confirm(Agent agent) { // If Component is still in place if (agent.PieceAt(this.ComponentCoords) == this.Component && this.Component.Stats.Food < this.Component.Stats.MaxFood) { this.ChargeCoords = agent.Coords; this.OrigOrientation = agent.Orientation; this.SaveSmartInfo(agent); this.Component.Stats.Food++; return(true); } return(false); }
override public Action Available(Agent agent) { // If the Agent is a Component if (agent is CircuitComponent component) { // If the Component is connected and has surplus charge if (component.Connections.Count > 0 && component.Stats.Food > 0) { ElectricSlime charge = null; // If there are Slimes stored if (component.Charges.Count > 0) { charge = component.Charges[component.Charges.Count - 1]; // Get the next Slime to be discharged // Find the approapriate connection to discharge it to var componentExitCoords = component.Coords + component.Footprint[component.Footprint.Length - 1]; LevelBoard.Directions inDir = LevelBoard.GetDirection(charge.Coords, component.Coords); var coords = component.RouteEnergy(inDir); // If an approapriate connection was found if (coords.x != -1) { return(new Discharge(charge, coords)); // Return the potential Action } } // If there are no Slimes stored else { var coords = component.RouteEnergy(LevelBoard.Directions.North); if (coords.x != -1) { return(new Discharge(charge, coords)); } } } } return(null); }
public bool AddCrossingConnection(Vector2Int startCoords, LevelBoard.Directions dirOut, Vector2Int endCoords, LevelBoard.Directions dirIn) { if (this.Crossings.TryGetValue(startCoords, out var start) && this.Crossings.TryGetValue(endCoords, out var end)) { var first = start.AddConnection(dirOut, end); var invDir = LevelBoard.InvertDirection(dirIn); var second = end.AddConnection(invDir, start); //if(first) Debug.Log("Added connection between " + startCoords + " through " + dirOut + " to " + endCoords); //if(second) Debug.Log("Added connection between " + endCoords + " through " + invDir + " to " + startCoords); return(first || second); } return(false); }
public Piece CreatePiece(Piece.Characteristics characterization, Vector2Int coords, LevelBoard.Directions ori = LevelBoard.Directions.South, int turn = 0) { var piece = Piece.CreatePiece(this, characterization, coords, ori, turn); if (!this.Board.PlacePiece(piece, coords)) { Destroy(piece.gameObject); return(null); } this.Pieces.Add(piece); if (piece is Agent agent) { this.Agents.Add(agent); } return(piece); }
public int GetUtility(LevelBoard.Directions dir) { var utility = int.MinValue; if (!this.Explorations.ContainsKey(dir)) { return(utility); } if (this.DeadEnds.Contains(dir)) { return(int.MinValue); } utility = 1 - this.Explorations[dir]; // How many have gone in that direction if (this.Connections.ContainsKey(dir)) { utility += this.Connections[dir].GetTotalUnexplored(this.Coords); // Paths unexplored } return(utility); }
public override bool Confirm(Agent agent) { if (agent is CircuitComponent component) { if (component.IsFree(this.TargetCoords)) { if (component.Charges.Count == 0) { var componentFootprint = component.GetFootprint(); LevelBoard.Directions ori = LevelBoard.GetDirection(componentFootprint[componentFootprint.Length - 1], this.TargetCoords); var piece = component.CreatePiece(new Piece.Characteristics(component.GetChargeType()), this.TargetCoords, ori, component.Turn + 1); this.Charge = (ElectricSlime)piece; this.NewCharge = true; } else { component.ReleaseCharge(this.Charge, this.TargetCoords); var outDir = LevelBoard.GetDirection(agent.Coords, this.TargetCoords); this.Charge.RotateInBoard(outDir); this.Charge.Rotate(outDir, 1f); } this.Charge.Hide(); return(true); } } return(false); }
// World virtual public bool Rotate(LevelBoard.Directions targetDir, float percentage = 0.33f) { float currentAngle = this.transform.eulerAngles.y; float targetAngle = 360 - ((float)targetDir) * 45f; if (targetAngle == 360) { targetAngle = 0; } var rate = Time.deltaTime / EXPECTED_DELTA; if (percentage == 1f) { rate = 1f; } currentAngle = Mathf.LerpAngle(currentAngle, targetAngle, (float)(percentage * 1f)); if (Mathf.Abs((currentAngle % 360) - targetAngle) < 0.1f) { currentAngle = targetAngle; } this.transform.eulerAngles = new Vector3(this.transform.eulerAngles.x, currentAngle, this.transform.eulerAngles.z); if (currentAngle == targetAngle || currentAngle - targetAngle == 360) { // Update their Orientation this.Orientation = targetDir; return(true); } return(false); }