void BoardHolderInit() { Transform board_holder = holders[(int)(holderID.BOARD)]; if (board_holder == null) { board_holder = new GameObject(holderNames[(int)(holderID.BOARD)]).transform; board_holder.localScale += new Vector3(1.0f / SQUARESIZE_PER_UNIT - 1.0f, 1.0f / SQUARESIZE_PER_UNIT - 1.0f, 0.0f); board_holder.Translate(new Vector3( (-rows / 2.0f + 0.5f) * SQUARESIZE_PER_UNIT, (-columns / 2.0f + 0.5f) * SQUARESIZE_PER_UNIT, 0 )); holders[(int)(holderID.BOARD)] = board_holder; } for (int i = (int)holderID.START; i <= (int)holderID.END; i++) { // do not add new board holder into the board holder if (i == (int)holderID.BOARD) continue; if (holders[i] == null) { holders[i] = new GameObject(holderNames[i]).transform; holders[i].SetParent(board_holder); } } }
void generateMapAndPlayer(Hashtable mapInfo, PlayerSpawnDir spawnDir) { // get map information from the hash table string mapString = (string)mapInfo["map"]; rows = (int)mapInfo["row"]; columns = (int)mapInfo["column"]; currentStageHash = (string)mapInfo["hash"]; int HPStatus, APStatus; if (player != null) { HPStatus = player.m_HP; APStatus = player.m_AP; } else { HPStatus = (int)mapInfo["hp"]; APStatus = (int)mapInfo["ap"]; } string[] arrayMap = mapString.Split(','); // get item information from the hash table int[] itemCounts = new int[(int)(itemID.END + 1)]; int[] itemValues = new int[(int)(itemID.END + 1)]; for (int i = 0; i <= (int)(itemID.END); i++) { if (mapInfo.ContainsKey(i.ToString())) { Hashtable itemTable = (Hashtable)mapInfo[i.ToString()]; itemCounts[i] = (int)itemTable["items"]; itemValues[i] = (int)itemTable["value"]; } else { itemCounts[i] = 0; } } // find the gates int northGate = -111, southGate = -111, eastGate = -111, westGate = -111; for (int i = 0; i < columns; i++) { if (arrayMap[i] == "N") { northGate = i; break; } } for (int i = 0; i < columns; i++) { if (arrayMap[columns*(rows-1) + i] == "S") { southGate = i; break; } } for (int i = 0; i < rows; i++) { if (arrayMap[columns - 1 + columns * (rows - 1 - i)] == "E") { eastGate = i; break; } } for (int i = 0; i < rows; i++) { if (arrayMap[columns * (rows - 1 - i)] == "W") { westGate = i; break; } } // floor generation for (int x = -2; x < columns + 2; x++) { for (int y = -3; y < rows + 3; y++) { int tileIndex = 0; if (x<=0 || y<=0 || x>=columns-1 || y>=rows-1) { tileIndex = 9; } else if (x == 1) { if (y == 1) tileIndex = 6; else if (y == rows - 2) tileIndex = 0; else tileIndex = 3; } else if (x == columns - 2) { if (y == 1) tileIndex = 8; else if (y == rows - 2) tileIndex = 2; else tileIndex = 5; } else { if (y == 1) tileIndex = 7; else if (y == rows - 2) tileIndex = 1; else tileIndex = 4; } instantiateAndAdd(floorTiles[tileIndex], x, y, holders[(int)(holderID.FLOOR)]); } } // cliff generation List<Vector3> rockTilePositions = new List<Vector3>(); List<Vector3> rockPositions = new List<Vector3>(); // cliff, SW side of the map instantiateAndAdd(cliffTiles[3], -1, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) for (int x = -2; x <= 0; x++) rockTilePositions.Add(new Vector3(x, y, 0)); for (int y = -1; y <= 0; y++) rockTilePositions.Add(new Vector3(-2, y, 0)); // cliff, SE side of the map instantiateAndAdd(cliffTiles[15], columns, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) for (int x = columns-1; x<=columns+1; x++) rockTilePositions.Add(new Vector3(x, y, 0)); for (int y = -1; y <= 0; y++) rockTilePositions.Add(new Vector3((columns + 1), y, 0)); // cliff, NW side of the map instantiateAndAdd(cliffTiles[7], -1, rows + 1, holders[(int)(holderID.CLIFF)]); for (int x = -2; x <= 0; x++) rockTilePositions.Add(new Vector3(x, rows + 2, 0)); for (int y = rows-1; y <= rows + 1; y++) rockTilePositions.Add(new Vector3(-2, y, 0)); // cliff, NE side of the map instantiateAndAdd(cliffTiles[11], columns, rows + 1, holders[(int)(holderID.CLIFF)]); for (int x = columns - 1; x <= columns + 1; x++) rockTilePositions.Add(new Vector3(x, rows + 2, 0)); for (int y = rows-1; y <= rows + 1; y++) rockTilePositions.Add(new Vector3((columns + 1), y, 0)); // cliff, south side for (int x = 1; x < columns - 1; x++) { if (x == southGate - 2) { instantiateAndAdd(cliffTiles[2], x, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) instantiateAndAdd(cliffTiles[4 + Random.Range(0, 2)], x, y, holders[(int)(holderID.CLIFF)]); } else if (x == southGate + 2) { instantiateAndAdd(cliffTiles[14], x, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) instantiateAndAdd(cliffTiles[12 + Random.Range(0, 2)], x, y, holders[(int)(holderID.CLIFF)]); } else if (Mathf.Abs(x - southGate) >= 3) { instantiateAndAdd(cliffTiles[Random.Range(0, 2)], x, -1, holders[(int)(holderID.CLIFF)]); for (int y = -3; y <= -2; y++) rockTilePositions.Add(new Vector3(x, y, 0)); } } // cliff, north side for (int x = 1; x < columns - 1; x++) { if (x == northGate - 2) { instantiateAndAdd(cliffTiles[6], x, rows + 1, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[4 + Random.Range(0, 2)], x, rows + 2, holders[(int)(holderID.CLIFF)]); } else if (x == northGate + 2) { instantiateAndAdd(cliffTiles[10], x, rows + 1, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[12 + Random.Range(0, 2)], x, rows + 2, holders[(int)(holderID.CLIFF)]); } else if (Mathf.Abs(x - northGate) >= 3) { instantiateAndAdd(cliffTiles[8 + Random.Range(0, 2)], x, rows + 1, holders[(int)(holderID.CLIFF)]); rockTilePositions.Add(new Vector3(x, rows + 2, 0)); } } // cliff, west side for (int y = 1; y <= rows - 1; y++) { if (y == westGate + 3) { instantiateAndAdd(cliffTiles[6], -1, y, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[8 + Random.Range(0, 2)], -2, y, holders[(int)(holderID.CLIFF)]); } else if (y == westGate - 1) { instantiateAndAdd(cliffTiles[2], -1, y, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[Random.Range(0, 2)], -2, y, holders[(int)(holderID.CLIFF)]); } else if (Mathf.Abs(y - (westGate + 1)) >= 3) { instantiateAndAdd(cliffTiles[4 + Random.Range(0, 2)], -1, y, holders[(int)(holderID.CLIFF)]); rockTilePositions.Add(new Vector3(-2, y, 0)); } } // cliff, east side for (int y = 1; y <= rows - 1; y++) { if (y == eastGate + 3) { instantiateAndAdd(cliffTiles[10], columns, y, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[8 + Random.Range(0, 2)], columns + 1, y, holders[(int)(holderID.CLIFF)]); } else if (y == eastGate - 1) { instantiateAndAdd(cliffTiles[14], columns, y, holders[(int)(holderID.CLIFF)]); instantiateAndAdd(cliffTiles[Random.Range(0, 2)], columns + 1, y, holders[(int)(holderID.CLIFF)]); } else if (Mathf.Abs(y - (eastGate + 1)) >= 3) { instantiateAndAdd(cliffTiles[12 + Random.Range(0, 2)], columns, y, holders[(int)(holderID.CLIFF)]); rockTilePositions.Add(new Vector3(columns + 1, y, 0)); } } // random generation of rock tiles foreach (Vector3 pos in rockTilePositions) { instantiateAndAdd( rockFloorTiles[Random.Range(0, rockFloorTiles.Length)], Mathf.FloorToInt(pos.x), Mathf.FloorToInt(pos.y), holders[(int)(holderID.CLIFF)] ); } // box colliders for the borders for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++) { if (arrayMap[x + (rows - 1 - y) * columns] == "#") { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(x, y, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent<BoxCollider2D>(); } } } if (northGate >= 1 && northGate < columns - 1) { for (int x = northGate-1; x <= northGate+1; x += 2) { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(x, rows + 1f, 0); border.localScale += new Vector3(0, 2.0f, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent<BoxCollider2D>(); } } if (southGate >= 1 && southGate < columns - 1) { for (int x = southGate - 1; x <= southGate + 1; x += 2) { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(x, -2f, 0); border.localScale += new Vector3(0, 2.0f, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent<BoxCollider2D>(); } } if (eastGate >= 1 && eastGate < rows - 1) { for (int y = eastGate - 1; y <= eastGate + 1; y += 2) { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(columns + 0.5f, y, 0); border.localScale += new Vector3(1.0f, 0, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent<BoxCollider2D>(); } } if (westGate >= 1 && westGate < rows - 1) { for (int y = westGate - 1; y <= westGate + 1; y += 2) { Transform border = new GameObject("collider").transform; border.SetParent(holders[(int)(holderID.BOARD_COLLIDER)]); border.Translate(-1.5f, y, 0); border.localScale += new Vector3(1.0f, 0, 0); border.gameObject.layer = LayerMask.NameToLayer("Object"); border.gameObject.AddComponent<BoxCollider2D>(); } } // block generation for (int y = rows - 1; y >= 0; y--) { for (int x = 0; x < columns; x++) { if (arrayMap[x + (rows - 1 - y) * columns] == "b") { instantiateAndAdd(blockTiles, x, y, 0, holders[(int)(holderID.BLOCK)]); } } } // item generation for (int y = rows - 1; y >= 0; y--) { for (int x = 0; x < columns; x++) { for (int itemIdx = (int)(itemID.START); itemIdx <= (int)(itemID.END); itemIdx++) { if (arrayMap[x + (rows - 1 - y) * columns] == itemIdx.ToString() && itemCounts[itemIdx] > 0) { switch ((itemID)itemIdx) { case itemID.POTION1: Potion potion1 = ObjectFactory.createPotion(x, y, itemValues[itemIdx], (int)(itemID.POTION1)); potion1.transform.SetParent(holders[(int)(holderID.POTION)]); break; case itemID.POTION2: Potion potion2 = ObjectFactory.createPotion(x, y, itemValues[itemIdx], (int)(itemID.POTION2)); potion2.transform.SetParent(holders[(int)(holderID.POTION)]); break; case itemID.WEAPON: Weapon weapon = ObjectFactory.createWeapon(x, y, itemValues[itemIdx], (int)(itemID.WEAPON)); weapon.transform.SetParent(holders[(int)(holderID.WEAPON)]); break; } } } } } // player generation int playerX = 0; int playerY = 0; switch (spawnDir) { case PlayerSpawnDir.SPAWN_EAST: playerX = columns - 2; playerY = eastGate; break; case PlayerSpawnDir.SPAWN_NORTH: playerX = northGate; playerY = rows - 2; break; case PlayerSpawnDir.SPAWN_WEST: playerX = 1; playerY = westGate; break; case PlayerSpawnDir.SPAWN_SOUTH: playerX = southGate; playerY = 1; break; case PlayerSpawnDir.SPAWN_NONE: bool playerPlaced = false; bool hasEmptySpace = false; for (int y = rows - 1; y >= 0; y--) { for (int x = 0; x < columns; x++) { if (arrayMap[x + (rows - 1 - y) * columns] == "u") { if (!playerPlaced) { playerPlaced = true; playerX = x; playerY = y; } } else if (arrayMap[x + (rows - 1 - y) * columns] == "f") { hasEmptySpace = true; } } } while (!playerPlaced && hasEmptySpace) { playerX = Random.Range(1, columns - 1); playerY = Random.Range(1, columns - 1); if (arrayMap[playerX + (rows - 1 - playerY) * columns] == "f") { playerPlaced = true; } } break; } if (player == null) { player = ObjectFactory.createPlayer(playerX, playerY, HPStatus, APStatus); player.transform.SetParent(holders[(int)(holderID.PLAYER)]); } else { player.Initialize(playerX, playerY, HPStatus, APStatus); } // get peer's ip addresses who are in this area! peerIPList.Clear(); ArrayList listIPs = (ArrayList)mapInfo["ips"]; foreach (string ip in listIPs) { if (ip != getMyIP()) peerIPList.Add(ip); } // peer player initialization peerUDPClients.Clear(); foreach (string ip in peerIPList) { GameObject peer = new GameObject("peer UDP client"); UDPClient peerClient = peer.AddComponent<UDPClient>(); peerClient.InitiateSocket(ip, 12346); peerUDPClients.Add(peer); Hashtable data = new Hashtable(); data.Add("action", "myinfo"); data.Add("hash", currentStageHash); data.Add("username", SystemInfo.deviceUniqueIdentifier); data.Add("ip", getMyIP()); data.Add("xpos", playerX); data.Add("ypos", playerY); peerClient.sendJSONObject(data); } }
/// <summary> /// Start everything up and get it configured. /// </summary> public void Start() { try { rpmComp = RasterPropMonitorComputer.Instantiate(internalProp, true); Transform textObjTransform = internalProp.FindModelTransform(transformName); Vector3 localScale = internalProp.transform.localScale; Transform offsetTransform = new GameObject().transform; offsetTransform.gameObject.name = "JSILabel-" + this.internalProp.propID + "-" + this.GetHashCode().ToString(); offsetTransform.gameObject.layer = textObjTransform.gameObject.layer; offsetTransform.SetParent(textObjTransform, false); offsetTransform.Translate(transformOffset.x * localScale.x, transformOffset.y * localScale.y, 0.0f); textObj = offsetTransform.gameObject.AddComponent<JSITextMesh>(); font = JUtil.LoadFont(fontName, fontQuality); textObj.font = font; //textObj.fontSize = fontQuality; // This doesn't work with Unity-embedded fonts textObj.fontSize = font.fontSize; if (!string.IsNullOrEmpty(anchor)) { if (anchor == TextAnchor.LowerCenter.ToString()) { textObj.anchor = TextAnchor.LowerCenter; } else if (anchor == TextAnchor.LowerLeft.ToString()) { textObj.anchor = TextAnchor.LowerLeft; } else if (anchor == TextAnchor.LowerRight.ToString()) { textObj.anchor = TextAnchor.LowerRight; } else if (anchor == TextAnchor.MiddleCenter.ToString()) { textObj.anchor = TextAnchor.MiddleCenter; } else if (anchor == TextAnchor.MiddleLeft.ToString()) { textObj.anchor = TextAnchor.MiddleLeft; } else if (anchor == TextAnchor.MiddleRight.ToString()) { textObj.anchor = TextAnchor.MiddleRight; } else if (anchor == TextAnchor.UpperCenter.ToString()) { textObj.anchor = TextAnchor.UpperCenter; } else if (anchor == TextAnchor.UpperLeft.ToString()) { textObj.anchor = TextAnchor.UpperLeft; } else if (anchor == TextAnchor.UpperRight.ToString()) { textObj.anchor = TextAnchor.UpperRight; } else { JUtil.LogErrorMessage(this, "Unrecognized anchor '{0}' in config for {1} ({2})", anchor, internalProp.propID, internalProp.propName); } } if (!string.IsNullOrEmpty(alignment)) { if (alignment == TextAlignment.Center.ToString()) { textObj.alignment = TextAlignment.Center; } else if (alignment == TextAlignment.Left.ToString()) { textObj.alignment = TextAlignment.Left; } else if (alignment == TextAlignment.Right.ToString()) { textObj.alignment = TextAlignment.Right; } else { JUtil.LogErrorMessage(this, "Unrecognized alignment '{0}' in config for {1} ({2})", alignment, internalProp.propID, internalProp.propName); } } float sizeScalar = 32.0f / (float)font.fontSize; textObj.characterSize = fontSize * 0.00005f * sizeScalar; textObj.lineSpacing = textObj.lineSpacing * lineSpacing; // "Normal" mode if (string.IsNullOrEmpty(switchTransform)) { // Force oneshot if there's no variables: oneshot |= !labelText.Contains("$&$"); string sourceString = labelText.UnMangleConfigText(); if (!string.IsNullOrEmpty(sourceString) && sourceString.Length > 1) { // Alow a " character to escape leading whitespace if (sourceString[0] == '"') { sourceString = sourceString.Substring(1); } } labels.Add(new JSILabelSet(sourceString, rpmComp, oneshot)); if (!oneshot) { rpmComp.UpdateDataRefreshRate(refreshRate); } } else // Switchable mode { SmarterButton.CreateButton(internalProp, switchTransform, Click); audioOutput = JUtil.SetupIVASound(internalProp, switchSound, switchSoundVolume, false); foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("PROP")) { if (node.GetValue("name") == internalProp.propName) { ConfigNode moduleConfig = node.GetNodes("MODULE")[moduleID]; ConfigNode[] variableNodes = moduleConfig.GetNodes("VARIABLESET"); for (int i = 0; i < variableNodes.Length; i++) { try { bool lOneshot = false; if (variableNodes[i].HasValue("oneshot")) { bool.TryParse(variableNodes[i].GetValue("oneshot"), out lOneshot); } if (variableNodes[i].HasValue("labelText")) { string lText = variableNodes[i].GetValue("labelText"); string sourceString = lText.UnMangleConfigText(); lOneshot |= !lText.Contains("$&$"); labels.Add(new JSILabelSet(sourceString, rpmComp, lOneshot)); if (!lOneshot) { rpmComp.UpdateDataRefreshRate(refreshRate); } } } catch (ArgumentException e) { JUtil.LogErrorMessage(this, "Error in building prop number {1} - {0}", e.Message, internalProp.propID); } } break; } } } if (!string.IsNullOrEmpty(zeroColor)) { zeroColorValue = JUtil.ParseColor32(zeroColor, part, ref rpmComp); textObj.color = zeroColorValue; } bool usesMultiColor = false; if (!(string.IsNullOrEmpty(variableName) || string.IsNullOrEmpty(positiveColor) || string.IsNullOrEmpty(negativeColor) || string.IsNullOrEmpty(zeroColor))) { usesMultiColor = true; positiveColorValue = JUtil.ParseColor32(positiveColor, part, ref rpmComp); negativeColorValue = JUtil.ParseColor32(negativeColor, part, ref rpmComp); del = (Action<float>)Delegate.CreateDelegate(typeof(Action<float>), this, "OnCallback"); rpmComp.RegisterVariableCallback(variableName, del); registeredVessel = vessel.id; // Initialize the text color. Actually, callback registration takes care of that. } if (string.IsNullOrEmpty(emissive)) { if (usesMultiColor) { emissiveMode = EmissiveMode.active; } else { emissiveMode = EmissiveMode.always; } } else if (emissive.ToLower() == EmissiveMode.always.ToString()) { emissiveMode = EmissiveMode.always; } else if (emissive.ToLower() == EmissiveMode.never.ToString()) { emissiveMode = EmissiveMode.never; } else if (emissive.ToLower() == EmissiveMode.active.ToString()) { emissiveMode = EmissiveMode.active; } else if (emissive.ToLower() == EmissiveMode.passive.ToString()) { emissiveMode = EmissiveMode.passive; } else if (emissive.ToLower() == EmissiveMode.flash.ToString()) { if (flashRate > 0.0f) { emissiveMode = EmissiveMode.flash; fm = JUtil.InstallFlashModule(part, flashRate); if (fm != null) { fm.flashSubscribers += FlashToggle; } } else { emissiveMode = EmissiveMode.active; } } else { JUtil.LogErrorMessage(this, "Unrecognized emissive mode '{0}' in config for {1} ({2})", emissive, internalProp.propID, internalProp.propName); emissiveMode = EmissiveMode.always; } UpdateShader(); } catch (Exception e) { JUtil.LogErrorMessage(this, "Start failed in prop {1} ({2}) with exception {0}", e, internalProp.propID, internalProp.propName); labels.Add(new JSILabelSet("ERR", rpmComp, true)); } }