// Select any cubes that are adjacent to the current one in 3 directions (excluding the previous one) // Uses recursion to add each cube to the global group public void SelectAdj(Vector3 dir) { if (Physics.Raycast(transform.position, dir, out hit, size)) { if (hit.transform.tag == "Cube") { CubeProperties prop = hit.transform.GetComponent <CubeProperties>(); if (hit.transform.GetComponent <CubeProperties>().colour == colour && !prop.fall) { if (!hit.transform.GetComponent <CubeProperties>().selected) { hit.transform.GetComponent <CubeProperties>().selected = true; Globals.group.Add(hit.transform.gameObject); if (dir != Vector3.left) { prop.SelectAdj(Vector3.right); } if (dir != Vector3.right) { prop.SelectAdj(Vector3.left); } if (dir != Vector3.up) { prop.SelectAdj(Vector3.down); } if (dir != Vector3.down) { prop.SelectAdj(Vector3.up); } } } } } }
public bool AddItemToInventory(CubeProperties.itemIDs itemID , int number , SlotProperties slot) { if(CubeProperties.cubeProperties.itemDict[itemID].stackable) { SlotProperties temp = CheckIfIsInInvenory( itemID); if( temp != null) { int remainder = temp.UpdateNumberOfItem(number); if(remainder == 0) { slot.UpdateSlot(0,0); return true; } else { slot.UpdateSlot(itemID , remainder); return AddItemToInventory(itemID ,remainder ,slot ); } } } SlotProperties temp1 = GetNextAvailableSlot (itemID); if(temp1 != null) { temp1.UpdateSlot(itemID , number); return true; } return false; }
public void CreateCube(int x , int y, int z, CubeProperties.itemIDs cubeType) { int index = -1; if(x >= world.chunkLength) index = world.FindChunk(GetChunkCoords(new Vector2(1,0))); else if(z >= world.chunkLength) index = world.FindChunk(GetChunkCoords(new Vector2(0,1))); else if(x < 0) index = world.FindChunk(GetChunkCoords(new Vector2(-1,0))); else if(z < 0) index = world.FindChunk(GetChunkCoords(new Vector2(0,-1))); if (x >= world.chunkLength) x -= world.chunkLength; else if (x < 0) x += world.chunkLength; else if (z >= world.chunkLength) z -= world.chunkLength; else if (z < 0) z += world.chunkLength; if(index != -1) { if(world.chunks [index].GetComponent<ChunkGenerator> ().GetCube (x , y , z) == 0 ) { world.chunks [index].GetComponent<ChunkGenerator> ().CreateCube (x , y , z , cubeType); } } else { if (GetCube (x, y, z) == 0) { cubes [x, y, z] = (byte)cubeType; CreateVisualMesh (); } } }
void DestroyBlock() { if (CheckForCube()) { Collider _collider = GetHitCollider(); CubeProperties _cubeProperties = _collider.GetComponent <CubeProperties>(); _cubeProperties.DestroyBlock(); } }
/// <inheritdoc/> public void Duplicate(ObjectProperties objectProperties) { CubeProperties cubeProperties = (CubeProperties)objectProperties; this.color = cubeProperties.color; CubeUI cui = FindObjectOfType(typeof(CubeUI)) as CubeUI; cui.Setup(gameObject, this.color); }
void BuildBlock() { if (CheckForCube()) { Collider _collider = GetHitCollider(); CubeProperties _cubePropertiesScript = _collider.GetComponent <CubeProperties>(); Vector3 locationToBuild = _cubePropertiesScript.GetLocationToBuildBlock(hit); Instantiate(cubeBuild, locationToBuild, new Quaternion(0, 0, 0, 0)); Debug.Log("Check For Cube Done"); } }
public void ChangeCubeProperties() { if (activeCubeProp == 1) { activeCubeProp = 2; cubeProp = redCube; } else if (activeCubeProp == 2) { activeCubeProp = 1; cubeProp = blueCube; } }
public void onChange(Dropdown dp) { GameObject obj = Camera.main.GetComponent <Inspector>().selectedItem; int current = dp.value; switch (current) { case 0: obj.transform.GetChild(0).gameObject.GetComponent <Renderer>().material = red; break; case 1: obj.transform.GetChild(0).gameObject.GetComponent <Renderer>().material = green; break; case 2: obj.transform.GetChild(0).gameObject.GetComponent <Renderer>().material = blue; break; case 3: obj.transform.GetChild(0).gameObject.GetComponent <Renderer>().material = purple; break; case 4: obj.transform.GetChild(0).gameObject.GetComponent <Renderer>().material = white; break; case 5: obj.transform.GetChild(0).gameObject.GetComponent <Renderer>().material = black; break; case 6: obj.transform.GetChild(0).gameObject.GetComponent <Renderer>().material = orange; break; case 7: obj.transform.GetChild(0).gameObject.GetComponent <Renderer>().material = yellow; break; } CubeProperties cp = obj.GetComponent <CubeProperties>(); cp.color = current; }
public bool AddItemToInventory(CubeProperties.itemIDs itemID) { if(CubeProperties.cubeProperties.itemDict[itemID].stackable) { SlotProperties temp = CheckIfIsInInvenory( itemID); if( temp != null) { temp.UpdateNumberOfItem(1); return true; } } SlotProperties temp1 = GetNextAvailableSlot (itemID); if(temp1 != null) { temp1.UpdateSlot(itemID); return true; } return false; }
// Use this for initialization void Start() { cubes = new GameObject[(int)numRow, (int)heightTunnel]; int halfHeight = heightTunnel / 2; int halfRow = numRow / 2; float incrementAngle = 90.0f / halfRow; float angle = -90.0f; for (int h = 0; h < heightTunnel; h++) { angle = -90.0f; for (int i = 0; i < numRow; i++) { cubes[i, h] = GameObject.CreatePrimitive(PrimitiveType.Cube); cubes[i, h].transform.parent = transform; //cube.transform.localPosition = new Vector3(i, h, 20); cubes[i, h].transform.localPosition = new Vector3(disFromView * Mathf.Sin(Mathf.Deg2Rad * angle), (h - halfHeight), disFromView * Mathf.Cos(Mathf.Deg2Rad * angle)); //cube.transform.localPosition = new Vector3(i*20*Mathf.Cos(angle), h, 20*Mathf.Cos(angle)); cubes[i, h].transform.rotation = Quaternion.AngleAxis(angle, new Vector3(0, 1, 0)); //cube.transform.localScale = new Vector3(2,2,2); CubeProperties prop = cubes[i, h].AddComponent <CubeProperties>(); prop.SetXY(i, h); float c = h + i; angle += incrementAngle; //Debug.Log(angle); //cubes[i,h].GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value); } } InvokeRepeating("PokeRandomCube", 0f, pokeFrequency); }
void Start() { cubeProperties = this.transform.parent.GetComponent <CubeProperties>(); }
void Awake() { uvSize = new Vector2(.04166666666666f, 0.1428571f); InitializeStructs(); cubeProperties = this; }
public SlotProperties GetNextAvailableSlot(CubeProperties.itemIDs itemID) { foreach (SlotProperties temp in activeInventorySlots) { if(temp.itemID == 0) return temp; } foreach( SlotProperties temp in mainInventorySlots) { if(temp.itemID == 0) return temp; } return null; }
public SlotProperties CheckIfIsInInvenory(CubeProperties.itemIDs itemID) { foreach (SlotProperties temp in activeInventorySlots) { if(temp.itemID == itemID && !temp.atMaxStacks) return temp; } foreach( SlotProperties temp in mainInventorySlots) { if(temp.itemID == itemID && !temp.atMaxStacks) return temp; } return null; }
// Update is called once per frame void Update() { if (Input.touchCount < 1) { dragging = false; return; } //Touch[] touch = Input.touches; Touch touch = Input.GetTouch(0); if (!Globals.paused && !Globals.gameOver) { if (touch.phase == TouchPhase.Began) { Debug.Log("touch"); Ray ray = Camera.main.ScreenPointToRay(touch.position); if (Physics.Raycast(ray, out hit) && hit.transform.tag == "Cube") { target = hit.transform; dragging = true; cube = hit.transform.GetComponent <CubeProperties>(); cube.selected = true; cube.allowDragX = false; cube.allowDragY = false; cube.prevPos = target.position; // Clear previous selections and select this cube Globals.ClearGroup(); Globals.group.Add(target.gameObject); // Select all connected cubes if this one isn't falling if (!cube.fall) { cube.SelectAdj(Vector3.left); cube.SelectAdj(Vector3.right); cube.SelectAdj(Vector3.up); cube.SelectAdj(Vector3.down); } cube.distance_to_screen = Camera.main.WorldToScreenPoint(target.position).z; Vector3 temp_pos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cube.distance_to_screen)); cube.offsetX = target.position.x - temp_pos.x; } } if (cube.colour != new Color(0.5f, 0.5f, 0.5f)) { if (dragging && touch.phase == TouchPhase.Moved) { Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cube.distance_to_screen)); Vector3 cube_pos = target.position; pos_move.x = pos_move.x + cube.offsetX; // Make sure object is dragged and not moved slightly if (Mathf.Abs(pos_move.x - cube.prevPos.x) > 0.3f) { cube.allowDragX = true; } if (cube.allowDragX) { // Check if an object is blocking this cube to the left // The +/- 0.01f is necessary to prevent collision detection when a cube is touching from above/below Vector3 originU = new Vector3(cube_pos.x, cube_pos.y + Globals.cubeSize - 0.01f, cube_pos.z); Vector3 originD = new Vector3(cube_pos.x, cube_pos.y - Globals.cubeSize + 0.01f, cube_pos.z); if (Physics.Raycast(originU, Vector3.left, Globals.cubeSize) || Physics.Raycast(originD, Vector3.left, Globals.cubeSize)) { cube.dragLeft = false; } else { cube.dragLeft = true; } // Check if an object is blocking this cube to the right if (Physics.Raycast(originU, Vector3.right, Globals.cubeSize) || Physics.Raycast(originD, Vector3.right, Globals.cubeSize)) { cube.dragRight = false; } else { cube.dragRight = true; } // Raycast to see where objects are to the left and right // Prevent fast dragging from ignoring collision if (Physics.Raycast(originU, Vector3.left, out hit) || Physics.Raycast(originD, Vector3.left, out hit)) { if (pos_move.x < hit.transform.position.x) { pos_move.x = hit.transform.position.x + 1; } } if (Physics.Raycast(originU, Vector3.right, out hit) || Physics.Raycast(originD, Vector3.right, out hit)) { if (pos_move.x > hit.transform.position.x) { pos_move.x = hit.transform.position.x - 1; } } // Prevent dragging left or right if an object is blocking in that direction if ((!cube.dragLeft && pos_move.x <= cube_pos.x) || (!cube.dragRight && pos_move.x >= cube_pos.x)) { pos_move.x = cube_pos.x; } // Clamp maximum left drag if (cube_pos.x >= pos_move.x) { if (cube_pos.x - pos_move.x > maxDragX) { pos_move.x = cube_pos.x - maxDragX; } } if (pos_move.x <= 0) { pos_move.x = 0; } // Clamp maximum right drag if (cube_pos.x <= pos_move.x) { if (pos_move.x - cube_pos.x > maxDragX) { pos_move.x = cube_pos.x + maxDragX; } } if (pos_move.x >= 11) { pos_move.x = 11; } } else { pos_move.x = cube_pos.x; } if (Mathf.Abs(pos_move.y - cube.prevPos.y) > 0.5) { cube.allowDragY = true; } cube.prevPos.y = cube_pos.y; bool checkDown = Physics.Raycast(cube_pos, Vector3.down, Globals.cubeSize * 2); if (checkDown) { //print(allowDragY); cube.allowDragY = false; } if (cube.allowDragY) { // Calculate y position movement if (pos_move.y > cube_pos.y) { pos_move.y = cube_pos.y; } if (cube_pos.y - pos_move.y > maxDragY) { pos_move.y = cube_pos.y - maxDragY; } Globals.decreaseTimer += Time.fixedDeltaTime; } else { pos_move.y = cube_pos.y; } // Update to new adjusted position cube_pos = new Vector3(pos_move.x, pos_move.y, pos_move.z); target.transform.position = cube_pos; } } if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) { Debug.Log("release"); if (target.position != cube.prevPos) { Globals.ClearGroup(); } Ray ray = Camera.main.ScreenPointToRay(touch.position); if (Physics.Raycast(ray, out hit, 10)) { if (hit.transform.tag == "Cube") { if (hit.transform.GetComponent <CubeProperties>().selected) { if (Globals.group.Count >= 3) { scoreInc = -2; // Begin at -2 so once 3 are counted the points start at 1 foreach (GameObject o in Globals.group) { Destroy(o); scoreInc++; } // 3 points for 3 cubes and 3 additional points per extra cube Globals.score += (scoreInc * 3); } cube.selected = false; } } } Globals.ClearGroup(); target.position = new Vector3(Mathf.Round(target.position.x), target.position.y, target.position.z); dragging = false; } } /* * // Detect touch and assign target to object * if (touch.phase == TouchPhase.Began) * { * RaycastHit hit; * Ray ray = Camera.main.ScreenPointToRay(touch.position); * if(Physics.Raycast(ray, out hit) && hit.transform.tag == "Cube") { * if (hit.collider.gameObject.GetComponent<CubeProperties>().draggable == true) { * Debug.Log("here"); * target = hit.transform; * prop = hit.transform.GetComponent<CubeProperties>(); * distance = target.position.z - Camera.main.transform.position.z; * update = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, target.position.y, distance)); * offset = target.position - update; * dragging = true; * } * } * } * * // Control drag movement * if(dragging && touch.phase == TouchPhase.Moved) * { * // Set update position to cursor position * update = new Vector3(Input.mousePosition.x, target.position.y, distance); * update = Camera.main.ScreenToWorldPoint(update); * * // Clamp maximum left drag * if (update.x < transform.position.x) * if (transform.position.x - update.x > maxDistance) * update.x = transform.position.x - maxDistance; * * // Clamp maximum right drag * if (update.x > transform.position.x) * if (update.x - transform.position.x > maxDistance) * update.x = transform.position.x + maxDistance; * * // Clamp the x position if an object is adjacent to it * if ((!prop.dragLeft && update.x <= target.position.x) || (!prop.dragRight && update.x >= target.position.x)) * update.x = target.position.x; * * // Update y value * if (prop.fall) * update.y = target.position.y - prop.fallSpeed * Time.fixedDeltaTime; * * // Update to new position * target.position = update + offset; * * } * * // End dragging * if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) * { * dragging = false; * transform.position = new Vector3(Mathf.Round(transform.position.x), transform.position.y, transform.position.z); * }*/ }
void InitializeVariables() { world = World.currentWorld; cubes = new byte[world.chunkLength, world.chunkHeight, world.chunkLength]; meshRender = GetComponent<MeshRenderer> (); meshCollider = GetComponent<MeshCollider> (); meshFilter = GetComponent<MeshFilter> (); chunkProp = GetComponent<ChunkProperties> (); cubeType = CubeProperties.cubeProperties; }