public void InitializeFromMapData(MapData mapData) { //Set all air block without tree as navigable NavPoints = new RVector3Table(); var chunks = mapData.Chunks; this.ForXyz(chunks.GetLength(0), chunks.GetLength(1), chunks.GetLength(2), (cx, cy, cz) => { var chunk = chunks[cx, cy, cz]; var blocks = chunk.Blocks; var chunkOffset = new RVector3( cx * (blocks.GetLength(0)), cy * (blocks.GetLength(1)), cz * (blocks.GetLength(2))); this.ForXyz( blocks.GetLength(0), blocks.GetLength(1), blocks.GetLength(2), (bx, by, bz) => { RecalculateBlockNavigation(bx, by, bz, blocks, chunkOffset); }); }); }
public bool Contains(RVector3 item) { return(store.ContainsKey(item.x) && store[item.x].ContainsKey(item.y) && store[item.x][item.y].ContainsKey(item.z) && store[item.x][item.y][item.z] != null); }
private Vector3 GetCubeWorldPosFromHit(Vector3 hitPos) { var hitPosCube = new RVector3( Mathf.FloorToInt(hitPos.x), Mathf.FloorToInt(hitPos.y), Mathf.FloorToInt(hitPos.z)); //TODO if face x-1 or face z-1 change vx & vy var vx = 0; if (Input.GetKey(KeyCode.LeftControl)) { vx -= (int)BlockSize; } var vy = 0; if (Input.GetKey(KeyCode.LeftShift)) { vy -= (int)BlockSize; } var vz = 0; if (Input.GetKey(KeyCode.LeftAlt)) { vz -= (int)BlockSize; } return(new RVector3(hitPosCube.x + vx, hitPosCube.y + vy, hitPosCube.z + vz)); }
public void UnsetNavigablePoint(RVector3 position) { if (NavPoints.Contains(position)) { NavPoints.Remove(position); } }
public Vector3 realtosim(RVector3 real_pose, int index) { Vector3 new_pose = new Vector3(0, 0, 0); float sim_w = sim_corner_b_right_down.x - sim_corner_a_left_up.x; float sim_h = sim_corner_a_left_up.z - sim_corner_b_right_down.z; float real_w = real_corner_b_right_down.x - real_corner_a_left_up.x; float real_h = real_corner_a_left_up.z - real_corner_b_right_down.z; float new_x = ((real_pose.x - real_corner_a_left_up.x) / real_w) * sim_w + sim_corner_a_left_up.x; float new_z = ((real_pose.z - real_corner_b_right_down.z) / real_h) * sim_h + sim_corner_b_right_down.z; new_pose.x = new_x; if (index == 1) { new_pose.y = 1.5f; } if (index == 2) { new_pose.y = 1.5f; } new_pose.z = new_z; return(new_pose); }
public void RemoveBlock(Block block) { RVector3 pos = block.position; chunkBlocks[pos.x, pos.y, pos.z].empty = true; UpdateChunk(); UpdateCollider(); }
public void SetNavigablePoint(RVector3 position) { if (NavPoints.Contains(position)) { return; } NavPoints.Add(position); }
/// <summary> /// Gets the block closest to the position provided /// </summary> /// <returns>The closest block</returns> /// <param name="position">The position closest to a block</param> public Block GetBlock(Vector3 position) { RVector3 rpos = new RVector3(Mathf.FloorToInt(position.x), Mathf.FloorToInt(position.y), Mathf.FloorToInt(position.z)); rpos.x -= _chunkPosition.x; rpos.z -= _chunkPosition.z; return(chunkBlocks[rpos.x, rpos.y, rpos.z]); }
private Block PlaceBlock(Block blockProto) { BlockFace side; Block block = GetBlockFromLookVector(out side); if (side == BlockFace.All || block == null) { return(null); } RVector3 newPosition = new RVector3(block.position.ToVector3()); switch (side) { case BlockFace.Bottom: newPosition.y -= 1; break; case BlockFace.Top: newPosition.y += 1; break; case BlockFace.Far: newPosition.z += 1; break; case BlockFace.Near: newPosition.z -= 1; break; case BlockFace.Left: newPosition.x -= 1; break; case BlockFace.Right: newPosition.x += 1; break; case BlockFace.All: break; default: throw new ArgumentOutOfRangeException(); } Vector3 center = newPosition.ToVector3(); center.x += 0.5f; center.y += 0.5f; center.z += 0.5f; return(Physics.OverlapBox(center, new Vector3(0.4f, 0.4f, 0.4f)).Length == 0 ? block.chunk.AddBlock(blockProto, newPosition) : null); }
private void Subscriber_cmd_vel_delegateNewData(long sequence, byte[] buffer, uint priority, Net2.Net2HandlerBase sender) { MemoryStream ms = new MemoryStream(buffer); RVector3 cmd = Serializer.Deserialize <RVector3>(ms); speed.x = cmd.x * 15; speed.y = cmd.y * 15; speed.z = cmd.z * 10; print(speed.x + " " + speed.y + " " + speed.z); }
private void Main_timer_Elapsed(object sender, ElapsedEventArgs e) { RVector3 msg = new RVector3(); msg.x = 10; msg.y = 20; msg.theta = 30; local1.sendMessage(msg); print("Cube 1 send done."); }
private void ClickChunck(GameObject chunkGameObject, Vector3 cubeWorldPos, ushort editBlockIndex) { var chunkController = chunkGameObject.GetComponent <ChunkController>(); var chunk = chunkController.ChunkData; var chunkIndex = chunk.MapPosition; var chunkRelativeWorldPos = cubeWorldPos - chunkController.gameObject.transform.position; var cubeChunkIndex = new RVector3(chunkRelativeWorldPos); if (cubeChunkIndex.x < 0) { cubeChunkIndex.x += (int)ChunkSize.x; chunkIndex.x -= 1; chunkController = GetChunkAtPosition(chunkIndex); chunk = chunkController.ChunkData; } if (cubeChunkIndex.z < 0) { cubeChunkIndex.z += (int)ChunkSize.x; chunkIndex.z -= 1; chunkController = GetChunkAtPosition(chunkIndex); chunk = chunkController.ChunkData; } if (cubeChunkIndex.x >= ChunkSize.x) { cubeChunkIndex.x -= (int)ChunkSize.x; chunkIndex.x += 1; chunkController = GetChunkAtPosition(chunkIndex); chunk = chunkController.ChunkData; } if (cubeChunkIndex.z >= ChunkSize.z) { cubeChunkIndex.z -= (int)ChunkSize.x; chunkIndex.z += 1; chunkController = GetChunkAtPosition(chunkIndex); chunk = chunkController.ChunkData; } //Debug.LogFormat("Hit chunk {2} {0} at cube {1}", chunk.MapPosition, cubeChunkIndex, chunkRelativeWorldPos); if (chunkController != null) { chunkController.SetBlock(cubeChunkIndex, editBlockIndex); GetComponent <NavWeb>() .RecalculateBlockNavigation( cubeChunkIndex.x, cubeChunkIndex.y, cubeChunkIndex.z, chunk.Blocks, new RVector3(chunkIndex.x * ChunkSize.x, chunkIndex.y * ChunkSize.y, chunkIndex.z * ChunkSize.z)); } }
public List <RVector3> GetConnexion(RVector3 p) { var con = new List <RVector3>(); this.ForXyz(p.x - 1, p.x + 2, p.y - 1, p.y + 2, p.z - 1, p.z + 2, (x, y, z) => { var cursor = new RVector3(x, y, z); if (cursor != p && NavPoints.Contains(cursor)) { con.Add(cursor); } }); return(con); }
public static float[,] Generate(RVector3 chunkPosition, int xSize, int ySize, int seed, float intensity, int minHeight = 50) { float[,] noise = new float[xSize, ySize]; for (int x = chunkPosition.x; x < xSize + chunkPosition.x; x++) { for (int y = chunkPosition.z; y < ySize + chunkPosition.z; y++) { float xNoise = (float)x / xSize * intensity; float yNoise = (float)y / ySize * intensity; noise[x - chunkPosition.x, y - chunkPosition.z] = Mathf.PerlinNoise(seed + xNoise, seed + yNoise) * intensity + minHeight; } } return(noise); }
public void SetBlock(RVector3 buildPosCube, ushort blocktype) { if (buildPosCube.x < 0 || buildPosCube.y < 0 || buildPosCube.z < 0 || buildPosCube.x >= Size.x || buildPosCube.y >= Size.y || buildPosCube.z >= Size.z) { return; } Send(new UpdateBlockMessage { ChunkPosition = ChunkData.MapPosition, BlockPosition = buildPosCube, BlockType = blocktype }, true); }
public void Update() { if (EventSystem.current.IsPointerOverGameObject()) { return; } var ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 500)) { if (hit.rigidbody != null) { var hitPos = hit.point; var cubeWorldPos = GetCubeWorldPosFromHit(hitPos); if (Highlighter != null) { var hs = BlockSize / 2; Highlighter.transform.position = cubeWorldPos + new Vector3(hs, hs, hs); } if (Input.GetKeyUp(KeyCode.G)) { debugPath = new List <RVector3>(); debugOriginForPath = new RVector3(cubeWorldPos); } if (Input.GetKeyDown(KeyCode.H)) { debugDestinationForPath = new RVector3(cubeWorldPos); debugPath = GetComponent <NavWeb>().FindPath(debugOriginForPath, debugDestinationForPath); } if (Input.GetMouseButtonDown(0)) { ClickChunck(hit.collider.gameObject, cubeWorldPos, EditBlockIndex); } if (Input.GetMouseButtonDown(1)) { ClickChunck(hit.collider.gameObject, cubeWorldPos, 0); } } } }
public void Add(RVector3 item) { if (!store.ContainsKey(item.x)) { store.Add(item.x, new Dictionary <int, Dictionary <int, RVector3> >()); } if (!store[item.x].ContainsKey(item.y)) { store[item.x].Add(item.y, new Dictionary <int, RVector3>()); } if (!store[item.x][item.y].ContainsKey(item.z)) { store[item.x][item.y].Add(item.z, item); } }
public void Remove(RVector3 item) { if (!store.ContainsKey(item.x)) { return; } if (!store[item.x].ContainsKey(item.y)) { return; } if (!store[item.x][item.y].ContainsKey(item.z)) { return; } store[item.x][item.y].Remove(item.z); }
public Block AddBlock(Block blockProto, RVector3 position) { if (_bounds.Contains(position.ToVector3())) { Block block = new Block(blockProto) { position = position, chunk = this }; chunkBlocks[position.x, position.y, position.z] = block; UpdateChunk(); UpdateCollider(); return(block); } Debug.Log("Block not in chunk!"); return(null); }
public List <RVector3> FindPath(RVector3 origin, RVector3 destination) { var path = new List <RVector3> { origin }; var evaluated = new List <RVector3> { origin }; int maxSearch = 100; while (maxSearch > 0) { maxSearch--; var openConnexion = GetConnexion(path.Last()) .Where(c => !evaluated.Contains(c)) .OrderBy(c => Vector3.Distance(c, destination)) .ToList(); var nextStep = openConnexion.FirstOrDefault(); if (nextStep == null) { Debug.LogFormat("Coudn't find a parth from {0} to {1}", origin, destination); return(null); } path.Add(nextStep); evaluated.Add(nextStep); if (nextStep.x == destination.x && nextStep.y == destination.y && nextStep.z == destination.z) { return(path); } } Debug.LogFormat("Coudn't find a parth from {0} to {1} (max search reached)", origin, destination); return(null); }
public void RecalculateBlockNavigation(int bx, int by, int bz, BlockData[,,] blocks, RVector3 chunkOffset) { var block = blocks[bx, by, bz]; if (block.BlockType == 0 && block.ObjectDataData == null) { var position = new RVector3(bx + chunkOffset.x, by + chunkOffset.y, bz + chunkOffset.z); var blockOnBottomIsAir = by == 0 || blocks[bx, by - 1, bz].BlockType == 0 && blocks[bx, by - 1, bz].ObjectDataData == null; var blockOnTopIsAir = by == (blocks.GetLength(2) - 1) || blocks[bx, by + 1, bz].BlockType == 0; if (!blockOnBottomIsAir && blockOnTopIsAir) { SetNavigablePoint(position); } } }
/// <summary> /// Selects the block that the player is looking at /// </summary> /// <returns>The block.</returns> private Block SelectBlock() { Block block = GetBlockFromLookVector(); if (block != null) { if (_selector == null) { if (selectorPrefab == null) { Debug.LogError("Selector prefab does not exist!"); } else { _selector = Instantiate(selectorPrefab); _selector.name = "Selector"; } } RVector3 pos = block.position; pos += block.chunk.Position; pos.y = block.position.y; if (_selector == null) { throw new NullReferenceException("Selector has not been initialized!"); } _selector.SetActive(true); _selector.transform.position = new Vector3(pos.x + 0.5f, pos.y + 0.5f, pos.z + 0.5f); return(block); } if (_selector != null) { _selector.SetActive(false); } return(null); }
private void Start() { _chunkPosition = new RVector3(transform.position); _textureAtlas = transform.GetComponent <MeshRenderer>().material.mainTexture; _atlasSize = new Vector2(_textureAtlas.width / textureBlockSize.x, _textureAtlas.height / textureBlockSize.y); _chunkMesh = GetComponent <MeshFilter>().mesh; _chunkMesh.MarkDynamic(); if (seed < 0) { seed = Random.Range(0, (int)Mathf.Round(Util.maxInt / 500.0f)); } if (Game.hasStarted) { GenerateChunk(); _chunkCollider = GetComponent <MeshCollider>(); _bounds.SetMinMax(transform.position, transform.position + chunkSize); } }
public bool IsNavigable(RVector3 position) { return(NavPoints.Contains(position)); }
public bool CheckSides(RVector3 blockPosition, BlockFace blockFace) { int x, y, z; x = blockPosition.x; y = blockPosition.y; z = blockPosition.z; var chunkBlocks = ChunkData.Blocks; switch (blockFace) { case BlockFace.Top: //Checks top face if (y + 1 < Size.y) { if (chunkBlocks[x, y + 1, z].BlockType != 0) { return(false); } } break; case BlockFace.Bottom: //Checks bottom face if (y - 1 >= 0 && chunkBlocks[x, y - 1, z].BlockType != 0) { return(false); } break; case BlockFace.Right: //Checks right face if (x + 1 < Size.x) { if (chunkBlocks[x + 1, y, z].BlockType != 0) { return(false); } } break; case BlockFace.Left: //Checks Left face if (x - 1 >= 0) { if (chunkBlocks[x - 1, y, z].BlockType != 0) { return(false); } } break; case BlockFace.Far: //Checks Far face if (z + 1 < Size.z) { if (chunkBlocks[x, y, z + 1].BlockType != 0) { return(false); } } break; case BlockFace.Near: //Checks Near face if (z - 1 >= 0) { if (chunkBlocks[x, y, z - 1].BlockType != 0) { return(false); } } break; } return(true); }
// TODO: Clean up public bool CheckSides(RVector3 blockPosition, BlockFace blockFace) { int x = blockPosition.x; int y = blockPosition.y; int z = blockPosition.z; switch (blockFace) { case BlockFace.Top: if (y + 1 <= chunkSize.y) { if (!chunkBlocks[x, y + 1, z].empty && !chunkBlocks[x, y + 1, z].hasTransparency) { return(false); } } break; case BlockFace.Bottom: if (y - 1 >= 0) { if (!chunkBlocks[x, y - 1, z].empty && !chunkBlocks[x, y - 1, z].hasTransparency) { return(false); } } break; case BlockFace.Right: if (x + 1 <= chunkSize.x) { if (!chunkBlocks[x + 1, y, z].empty && !chunkBlocks[x + 1, y, z].hasTransparency) { return(false); } } break; case BlockFace.Left: if (x - 1 >= 0) { if (!chunkBlocks[x - 1, y, z].empty && !chunkBlocks[x - 1, y, z].hasTransparency) { return(false); } } break; case BlockFace.Far: if (z + 1 <= chunkSize.z) { if (!chunkBlocks[x, y, z + 1].empty && !chunkBlocks[x, y, z + 1].hasTransparency) { return(false); } } break; case BlockFace.Near: if (z - 1 >= 0) { if (!chunkBlocks[x, y, z - 1].empty && !chunkBlocks[x, y, z - 1].hasTransparency) { return(false); } } break; case BlockFace.All: break; default: throw new ArgumentOutOfRangeException("blockFace", blockFace, null); } return(true); }
public void UpdateChunk() { verticies = new List <Vector3>(); uvs = new List <Vector2>(); triangles = new List <int>(); var chunkBlocks = ChunkData.Blocks; for (var yi = 0; yi < Size.y; yi++) { for (var xi = 0; xi < Size.x; xi++) { for (var zi = 0; zi < Size.z; zi++) { int blockType = chunkBlocks[xi, yi, zi].BlockType; if (blockType != 0) { var blockIndex = new RVector3(xi, yi, zi); var x = (float)xi * BlockSize; var y = (float)yi * BlockSize; var z = (float)zi * BlockSize; if (CheckSides(blockIndex, BlockFace.Top)) { verticiesIndex = verticies.Count; verticies.Add(new Vector3(x, y + BlockSize, z)); verticies.Add(new Vector3(x, y + BlockSize, z + BlockSize)); verticies.Add(new Vector3(x + BlockSize, y + BlockSize, z + BlockSize)); verticies.Add(new Vector3(x + BlockSize, y + BlockSize, z)); UpdateChunkUV(blockType); } if (CheckSides(blockIndex, BlockFace.Bottom)) { verticiesIndex = verticies.Count; verticies.Add(new Vector3(x, y, z)); verticies.Add(new Vector3(x + BlockSize, y, z)); verticies.Add(new Vector3(x + BlockSize, y, z + BlockSize)); verticies.Add(new Vector3(x, y, z + BlockSize)); UpdateChunkUV(blockType); } if (CheckSides(blockIndex, BlockFace.Right)) { verticiesIndex = verticies.Count; verticies.Add(new Vector3(x + BlockSize, y, z)); verticies.Add(new Vector3(x + BlockSize, y + BlockSize, z)); verticies.Add(new Vector3(x + BlockSize, y + BlockSize, z + BlockSize)); verticies.Add(new Vector3(x + BlockSize, y, z + BlockSize)); UpdateChunkUV(blockType); } if (CheckSides(blockIndex, BlockFace.Left)) { verticiesIndex = verticies.Count; verticies.Add(new Vector3(x, y, z + BlockSize)); verticies.Add(new Vector3(x, y + BlockSize, z + BlockSize)); verticies.Add(new Vector3(x, y + BlockSize, z)); verticies.Add(new Vector3(x, y, z)); UpdateChunkUV(blockType); } if (CheckSides(blockIndex, BlockFace.Far)) { verticiesIndex = verticies.Count; verticies.Add(new Vector3(x, y, z + BlockSize)); verticies.Add(new Vector3(x + BlockSize, y, z + BlockSize)); verticies.Add(new Vector3(x + BlockSize, y + BlockSize, z + BlockSize)); verticies.Add(new Vector3(x, y + BlockSize, z + BlockSize)); UpdateChunkUV(blockType); } if (CheckSides(blockIndex, BlockFace.Near)) { verticiesIndex = verticies.Count; verticies.Add(new Vector3(x, y, z)); verticies.Add(new Vector3(x, y + BlockSize, z)); verticies.Add(new Vector3(x + BlockSize, y + BlockSize, z)); verticies.Add(new Vector3(x + BlockSize, y, z)); UpdateChunkUV(blockType); } } } } } //FinalizeChunk(); meshGenerationCompleted = true; }
private ChunkController GetChunkAtPosition(RVector3 chunkIndex) { return(GetComponentsInChildren <ChunkController>().FirstOrDefault( c => c.ChunkData.MapPosition == chunkIndex )); }
private void Local1_eventDataUpdated() { RVector3 data = local1.get; print("Cube1 get data : " + data.x + " " + data.y + " " + data.theta); }