// Gets the mouse position is world space (& check for Cube hits) private void GameStateUpdates() { MarkedCube = null; // reset marked Cube UpdateUI(); // update Player UI information // Shoot ray into middle of screen (marked by sight) Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100f, layerMask)) { var cube = hit.transform.GetComponent <GameCube>(); // get Cube var cubeNormal = hit.normal.ToString(); // get side where Cube was hit switch (cubeNormal) // determine position for potential new Cube { case "(1.0, 0.0, 0.0)": NewCube = hit.transform.position + new Vector3(1, 0, 0); break; case "(-1.0, 0.0, 0.0)": NewCube = hit.transform.position + new Vector3(-1, 0, 0); break; case "(0.0, 1.0, 0.0)": NewCube = hit.transform.position + new Vector3(0, 1, 0); break; case "(0.0, 0.0, 1.0)": NewCube = hit.transform.position + new Vector3(0, 0, 1); break; case "(0.0, 0.0, -1.0)": NewCube = hit.transform.position + new Vector3(0, 0, -1); break; default: // no action needed break; } NewCubeSector = GameSectors.FindLast(n => n.SectorCoords == cube.ParentSector); ActionAllowed = true; // allow Build/Destroy actions if (cube.PlayerMade == true && BuildMode == false) // update time needed to destroy Cube { MarkedCube = cube; } } else { ActionAllowed = false; // no hit = disable Build/Destroy actions } }
//------------------------------- // Method for rebuild Player Cube private void RecreatePlayerCube(GameSector sector, PlayerCubesData cubeData) { // Create Player Cube according to loaded data about this var cubePos = cubeData.CubeCoords; GameObject newCubeGO = Instantiate(PlayerCubePfbs[cubeData.Endurance - 1], new Vector3(cubePos.x, cubePos.y, cubePos.z), Quaternion.identity) as GameObject; GameCube newCube = newCubeGO.GetComponent <GameCube>(); newCubeGO.transform.parent = sector.transform; newCube.ParentSector = sector.SectorCoords; sector.Cubes.Add(newCube); }
// Method for update Game World when any Outer Sector is reached public void UpdateSectors(int xDirection, int zDirection) { var xCoord = Player.PlayerSector.x; // get current Player coords (y = z for us) var zCoord = Player.PlayerSector.y; if (xDirection != 0) // change on x-axes -> shift Sectors horizontal { SectorsTmp = GameSectors.FindAll(n => n.SectorCoords.x == xCoord + (3 * xDirection)); for (int i = 0; i < SectorsTmp.Count; i++) // destoy 'left' Sectors & build new in Pl. direction { GameSectors.Remove(SectorsTmp[i]); Destroy(SectorsTmp[i].gameObject); var newCoord = new Vector2((xCoord + (xDirection * -1)), (SectorsTmp[i].SectorCoords.y)); CreateSector(newCoord, false); } SectorsTmp = GameSectors.FindAll(n => n.SectorCoords.x == xCoord + (2 * xDirection) && n.InnerSector == true); for (int i = 0; i < SectorsTmp.Count; i++) // update Inner/Outer Sector statuses { SectorTmp = GameSectors.FindLast(n => n.SectorCoords.x == xCoord && n.SectorCoords.y == SectorsTmp[i].SectorCoords.y); SectorTmp.InnerSector = true; SectorsTmp[i].InnerSector = false; } } else // change on z-ares -> shift Sectors vertical { SectorsTmp = GameSectors.FindAll(n => n.SectorCoords.y == zCoord + (3 * zDirection)); for (int i = 0; i < SectorsTmp.Count; i++) // destoy 'left' Sectors & build new in Pl. direction { GameSectors.Remove(SectorsTmp[i]); Destroy(SectorsTmp[i].gameObject); var newCoord = new Vector2(SectorsTmp[i].SectorCoords.x, zCoord + (zDirection * -1)); CreateSector(newCoord, false); } SectorsTmp = GameSectors.FindAll(n => n.SectorCoords.y == zCoord + (2 * zDirection) && n.InnerSector == true); for (int i = 0; i < SectorsTmp.Count; i++) // update Inner/Outer Sector statuses { SectorTmp = GameSectors.FindLast(n => n.SectorCoords.y == zCoord && n.SectorCoords.x == SectorsTmp[i].SectorCoords.x); SectorTmp.InnerSector = true; SectorsTmp[i].InnerSector = false; } } }
//------------------------------ // Method for Game Cube Creation // (Dynamic batching swithed on for reducing number of batches) private void CreateGameCube(GameSector sector, Vector2 position) { int yCoord = NoiseEffect(position.x, position.y); // create 'terrain' effect GCIndex = 0; // set Cube color according to y-position if (yCoord > 2) { GCIndex = 1; } if (yCoord > 5) { GCIndex = 2; } GameObject newCubeGO = Instantiate(CubePrefabs[GCIndex], new Vector3(position.x, yCoord, position.y), Quaternion.identity) as GameObject; GameCube newCube = newCubeGO.GetComponent <GameCube>(); newCubeGO.transform.parent = sector.transform; newCube.ParentSector = sector.SectorCoords; sector.Cubes.Add(newCube); }
//--------------------------- // Method for Sector Creation private void CreateSector(Vector2 sectorCoords, bool IsInner) { var xCoord = (int)sectorCoords.x * SectorSize; // get x-start point coords var zCoord = (int)sectorCoords.y * SectorSize; // get x-start point coords (y = z for us) GameObject newSectorGO = Instantiate(SectorPrefab, new Vector3(xCoord, 0, zCoord), Quaternion.identity) as GameObject; GameSector newSector = newSectorGO.GetComponent <GameSector>(); newSector.SectorCoords.x = sectorCoords.x; newSector.SectorCoords.y = sectorCoords.y; if (IsInner == true) // mark Sector as Inner Sector { newSector.InnerSector = true; } for (int x = xCoord; x < SectorSize + xCoord; x++) // Generate Game Cubes { for (int z = zCoord; z < SectorSize + zCoord; z++) { var position = new Vector2(x, z); CreateGameCube(newSector, position); // Create Game Cube } } // Create Sector Player Cubes PlayerCube = PlayerMade.Find(n => n.sectorCoord == sectorCoords); if (PlayerCube.CubeData != null) { for (int i = 0; i < PlayerCube.CubeData.Count; i++) { RecreatePlayerCube(newSector, PlayerCube.CubeData[i]); // build prev. created Player Cube } } // Add Sector to lists GameSectors.Add(newSector); newSector.transform.parent = ObjectsParent; }
//------------------------------------- // Method for Player made Cube Creation private void CreatePlayerCube(GameSector sector) { // Create Cube from relevant Prefab GameObject newCubeGO = Instantiate(PlayerCubePfbs[CubeIndex], new Vector3(NewCube.x, NewCube.y, NewCube.z), Quaternion.identity) as GameObject; GameCube newCube = newCubeGO.GetComponent <GameCube>(); newCubeGO.transform.parent = sector.transform; newCube.ParentSector = sector.SectorCoords; sector.Cubes.Add(newCube); // probably not needed SumPlayerCubes += 1; // increase Total // Process Player Made list PlayerCube = PlayerMade.FindLast(n => n.sectorCoord == sector.SectorCoords); if (PlayerCube.CubeData == null) // no Player Cube data for Sector yet { PlayerCube.sectorCoord = sector.SectorCoords; PlayerCube.CubeData = new List <PlayerCubesData>(); PlayerCubesData cubedata = new PlayerCubesData { CubeCoords = NewCube, Endurance = newCube.Endurance }; PlayerCube.CubeData.Add(cubedata); PlayerMade.Add(PlayerCube); } else // only add new Player Cube to list { PlayerCubesData cubedata = new PlayerCubesData { CubeCoords = NewCube, Endurance = newCube.Endurance }; PlayerCube.CubeData.Add(cubedata); } }