public GridObject AddGridObject(SpriteData sprite, int x, int y) { if (!CanAddGridObject(sprite, x, y)) { return(null); } // Instantiate object GridObject clone = Instantiate(gridObjectPrefab, sprite.Functional ? gridObjectParentFunctional : gridObjectParentDecorative); clone.SetSprite(sprite); clone.SetPosition(x, y); gridObjects.Add(clone); // Add references to object in grid for (int i = x; i < x + sprite.Width; i++) { for (int j = y; j < y + sprite.Height; j++) { if (sprite.Functional) { gridFunctional[i, j] = clone; } else { gridDecorative[i, j] = clone; } } } return(clone); }
public void RemoveGridObject(bool functional, int x, int y) { if (!ContainsGridObject(functional, x, y)) { return; } // Remove the grid object GridObject gridObject = (functional ? gridFunctional[x, y] : gridDecorative[x, y]); gridObjects.Remove(gridObject); // - Automatically removes all references in grid Destroy(gridObject.gameObject); }
public void RemoveGridObject(bool functional, int x, int y) { if (!ContainsGridObject(functional, x, y)) { return; } // Remove the grid object GridObject gridObject = (functional ? gridFunctional[x, y] : gridDecorative[x, y]); LogHandler.Instance.WriteLine("Remove " + gridObject.Data.Name + " at " + x + ", " + y + ": time = " + Time.time); gridObjects.Remove(gridObject); // - Automatically removes all references in grid Destroy(gridObject.gameObject); }
public GridObject AddGridObject(SpriteData sprite, int x, int y, bool writeLog) { if (!CanAddGridObject(sprite, x, y)) { return(null); } if (ContainsGridObject(sprite.Functional, x, y)) { return(null); } if (writeLog) { LogHandler.Instance.WriteLine("Added " + sprite.Name + " at " + x + ", " + y + ": time = " + Time.time); } // Instantiate object GridObject clone = Instantiate(gridObjectPrefab, sprite.Functional ? gridObjectParentFunctional : gridObjectParentDecorative); clone.SetSprite(sprite); clone.SetPosition(x, y); gridObjects.Add(clone); // Add references to object in grid for (int i = x; i < x + sprite.Width; i++) { for (int j = y; j < y + sprite.Height; j++) { if (sprite.Functional) { gridFunctional[i, j] = clone; } else { gridDecorative[i, j] = clone; } } } return(clone); }
private IEnumerator EndTurnCoroutine(Process process) { yield return(new WaitUntil(() => process.HasExited)); // Close prompt dialogueMenu.CloseDialogue(); // Read new additions from file string[] lines = File.ReadAllLines(Application.dataPath + "/StreamingAssets/Model/additions.csv"); float rate = totalDuration / lines.Length; // Was weird to have this change depending on # of additions if (lines.Length < 8) { rate = 0.5f; } foreach (string line in lines) { // - Parse values string[] values = line.Split(','); SpriteData sprite = SpriteManager.Instance.GetSprite(values[0]); int spriteX = int.Parse(values[1]); int spriteY = int.Parse(values[2]); if (GridManager.Instance.CanAddGridObject(sprite, spriteX, spriteY)) { // - Scroll window to addition location windowScroll.ScrollOverTime(spriteX + sprite.Width / 2); float time = 0; while (time < rate * 0.75f) { yield return(null); if (!IsLocked(dialogueMenu)) { time += Time.deltaTime; } } // - Fade addition in GridObject addition = GridManager.Instance.AddGridObject(sprite, spriteX, spriteY); addition.SetAlpha(0); while (time < rate) { yield return(null); if (!IsLocked(dialogueMenu)) { time += Time.deltaTime; addition.SetAlpha((time - rate * 0.75f) / (rate * 0.25f)); } } addition.SetAlpha(1); } } // Unblock input windowScroll.StopScrolling(); gridPlacement.RemoveLock(this); RemoveLock(this); }
private IEnumerator EndTurnCoroutine() { yield return(new WaitForSeconds(0.5f)); /** * int numAttempts = 0; * while (!System.IO.File.Exists(Constants.directory+ "/StreamingAssets/Model/additions.csv") && numAttempts<10){ * numAttempts+=1; * UnityEngine.Debug.Log (numAttempts); * yield return new WaitForSeconds(0.5f); * * } */ //process.Close(); string receivedFromServer = ""; try { Byte[] bytes = new Byte[8112]; bool listening = true; while (listening) { // Get a stream object for reading using (NetworkStream stream = socketConnection.GetStream()) { int length; // Read incomming stream into byte arrary. while ((length = stream.Read(bytes, 0, bytes.Length)) != 0) { var incommingData = new byte[length]; Array.Copy(bytes, 0, incommingData, 0, length); // Convert byte array to string message. string serverMessage = Encoding.ASCII.GetString(incommingData); UnityEngine.Debug.Log("server message received as: " + serverMessage); receivedFromServer += serverMessage; } listening = false; } } } catch (SocketException socketException) { UnityEngine.Debug.Log("Socket exception: " + socketException); } string[] eachAddition = receivedFromServer.Split(new char[] { '*' }); socketConnection.Close(); socketConnection = null; float rate = 2.0f / eachAddition.Length;//Was weird to have this change depending on # of additions if (eachAddition.Length < 8) { rate = 0.3f; } dialogueMenu.CloseDialogue(); foreach (string value in eachAddition) { string[] additionSplitFormat = value.Split(new char[] { ',' }); if (additionSplitFormat.Length == 4) { SpriteData sprite = SpriteManager.Instance.GetSprite(actions[int.Parse(additionSplitFormat[2])]); int spriteX = int.Parse(additionSplitFormat[0]) + startX; int spriteY = int.Parse(additionSplitFormat[1]); // - Scroll window to addition location windowScroll.ScrollOverTime(spriteX + sprite.Width / 2); float time = 0; while (time < rate * 0.75f) { yield return(null); if (!IsLocked) { time += Time.deltaTime; } } // - Fade addition in GridObject addition = GridManager.Instance.AddGridObject(sprite, spriteX, spriteY, true); if (addition != null) { gridObjects.Add(addition); addition.SetAlpha(0); time = 0; while (time < rate) { yield return(null); if (!IsLocked) { time += Time.deltaTime; addition.SetAlpha((time - rate * 0.25f) / (rate * 0.25f)); } } } else { yield return(new WaitForSeconds(rate - time)); } } } // Unblock input windowScroll.StopScrolling(); gridPlacement.RemoveLock(this); LogHandler.Instance.WriteLine("Player Turn: time = " + Time.time); }
public string FormatToJava() { StringBuilder builder = new StringBuilder(); builder.AppendLine("package dk.itu.mario.level;"); builder.AppendLine(""); builder.AppendLine("import java.util.Random;"); builder.AppendLine(""); builder.AppendLine("import dk.itu.mario.MarioInterface.Constraints;"); builder.AppendLine("import dk.itu.mario.MarioInterface.GamePlay;"); builder.AppendLine("import dk.itu.mario.MarioInterface.LevelInterface;"); builder.AppendLine("import dk.itu.mario.engine.sprites.SpriteTemplate;"); builder.AppendLine("import dk.itu.mario.engine.sprites.Enemy;"); builder.AppendLine(""); builder.AppendLine("public class MyLevel extends Level"); builder.AppendLine("{"); builder.AppendLine(" private static long lastSeed;"); builder.AppendLine(" private static Random levelSeedRandom;"); builder.AppendLine(""); builder.AppendLine(" private int difficulty;"); builder.AppendLine(" private int type;"); builder.AppendLine(""); builder.AppendLine(" public MyLevel(int width, int height)"); builder.AppendLine(" {"); builder.AppendLine(" super(width, height);"); builder.AppendLine(" }"); builder.AppendLine(""); builder.AppendLine(" public MyLevel(int width, int height, long seed, int difficulty, int type, GamePlay playerMetrics)"); builder.AppendLine(" {"); builder.AppendLine(" this(width, height);"); builder.AppendLine(" this.difficulty = difficulty;"); builder.AppendLine(" this.type = type;"); builder.AppendLine(" lastSeed = seed;"); builder.AppendLine(" levelSeedRandom = new Random(seed);"); builder.AppendLine(""); // Add objects and calculate exit location int[] exit = new int[] { 0, 0 }; foreach (GridObject gridObject in gridObjects) { string mapping = null; bool enemy = false; int width = gridObject.Data.Width; int height = gridObject.Data.Height; switch (gridObject.Data.Mapping) { case SimulatorObject.Ground: mapping = "GROUND"; if (gridObject.X > exit[0] || (gridObject.X == exit[0] && gridObject.Y > exit[1])) { exit = new int[] { gridObject.X + gridObject.Data.Width - 2, gridObject.Y + gridObject.Data.Height } } ; break; case SimulatorObject.Block: mapping = "BLOCK_EMPTY"; if (gridObject.X > exit[0] || (gridObject.X == exit[0] && gridObject.Y > exit[1])) { exit = new int[] { gridObject.X + gridObject.Data.Width - 2, gridObject.Y + gridObject.Data.Height } } ; break; case SimulatorObject.Coin: mapping = "COIN"; break; case SimulatorObject.Goomba: mapping = "ENEMY_GOOMBA"; enemy = true; width = 1; height = 1; break; case SimulatorObject.Koopa: mapping = "ENEMY_GREEN_KOOPA"; enemy = true; width = 1; height = 1; break; } if (mapping != null) { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { StringBuilder subBuilder = new StringBuilder(); subBuilder.Append(" "); subBuilder.Append(enemy ? "setSpriteTemplate(" : "setBlock("); subBuilder.Append((gridObject.X + i) + ", " + (GridHeight - gridObject.Y - j - 1) + ", "); subBuilder.Append(enemy ? "new SpriteTemplate(Enemy." : ""); subBuilder.Append(mapping); subBuilder.Append(enemy ? ", false)" : ""); subBuilder.Append(");"); builder.AppendLine(subBuilder.ToString()); } } } } // Add hill tops for (int i = 0; i < GridWidth; i++) { bool prevGround = false; for (int j = GridHeight - 1; j >= 0; j--) { GridObject gridObject = gridFunctional[i, j]; if (gridObject != null && gridObject.Data.Mapping == SimulatorObject.Ground) { if (!prevGround) { StringBuilder subBuilder = new StringBuilder(); subBuilder.Append(" setBlock("); subBuilder.Append(i + ", " + (GridHeight - j - 1) + ", "); subBuilder.Append("HILL_TOP);"); builder.AppendLine(subBuilder.ToString()); prevGround = true; } } else { prevGround = false; } } } // Add exit location builder.AppendLine(""); builder.AppendLine(" xExit = " + exit[0] + ";"); builder.AppendLine(" yExit = " + (GridHeight - exit[1]) + ";"); builder.AppendLine(" }"); builder.AppendLine("}"); return(builder.ToString()); }