Example #1
0
	// Called when user left clicks on a block while holding a tool
	public void HitBlockWithTool(Item ItemInUse) {
		Chunk MyHitChunk = MouseHit.collider.gameObject.GetComponent<Chunk> ();
		if (MyHitChunk) {
			Vector3 MouseHitPoint = MouseHit.point - MouseHit.normal * 0.5f;
			GameObject ItemDrop = GetManager.GetCharacterManager ().BlockItemDrop;
			Vector3 ItemDropPosition = new Vector3 (Mathf.RoundToInt (MouseHitPoint.x) / 1, Mathf.RoundToInt (MouseHitPoint.y) / 1, Mathf.RoundToInt (MouseHitPoint.z) / 1);
			//ItemDropPosition += new Vector3(0.5f, 0.5f, 0.5f);
		
		
			//Debug.Log ("Using Pickaxe at " + Time.time);
			BlockBase MyBlockBase = Terrain.GetBlock (MouseHit, false);
		
			World MyHitWorld = MyHitChunk.world;
			int HitPositionX = Mathf.RoundToInt (MouseHitPoint.x);
			int HitPositionY = Mathf.RoundToInt (MouseHitPoint.y);
			int HitPositionZ = Mathf.RoundToInt (MouseHitPoint.z);
			Vector3 HitPosition = new Vector3 (HitPositionX, HitPositionY, HitPositionZ);
			// atm this sends the signal to everything
			// first it should check if it can be removed - on client side - then send the signal
			// blocks should be damaged as well - and an animation should be created

			// Get the block index
			//This doesn't work sometimes?
			Block MyBlock = null;
			int BlockIndex = 0;
			BlockDamaged MyBlockDamaged2;
			try {
				MyBlock = (Block)MyBlockBase;
				BlockIndex = MyBlock.GetBlockIndex ();
			} catch {
				//Debug.LogError (gameObject.name + "'s Problem with: " + HitPosition.ToString());
				try {
					MyBlockDamaged2 = (BlockDamaged)MyBlockBase;
					BlockIndex = MyBlockDamaged2.GetBlockIndex ();
				} catch { 

				}
			}

		
			BlockData MyBlockData = GetManager.GetDataManager ().GetBlockData (BlockIndex);
			if (MyBlockData.CanBeRemoved) {
				BlockDamaged MyBlockDamaged;
				try {
					MyBlockDamaged = (BlockDamaged)MyBlockBase;
				} catch {
					MyBlockDamaged = CreateDamagedBlock (MyBlock, MyHitChunk, HitPosition, MyBlockData);
				}
				if (MyBlockDamaged.MyBlockDamage) {
					float DamageDone = MyBlockDamaged.MyBlockDamage.ApplyDamage (ItemInUse.Damage);

					if (DamageDone != 0 && MyBlockDamaged.MyBlockDamage) {
						//CreateDamagePopup (gameObject, MyBlockDamaged.MyBlockDamage.gameObject, DamageDone, 40);
					}

					if (MyBlockDamaged.MyBlockDamage.IsDead ()) {
						Destroy (MyBlockDamaged.MyBlockDamage.gameObject);	// remove the block damage from the scene!
						RemoveBlock (MyBlockData, HitPosition, MyHitWorld);
						///bool IsSuccess = true;
						//if (IsSuccess) 
						{
							//Debug.LogError ("Destroying " + BlockIndex +" Block at time: " + Time.time);
							ItemInUse.DecreaseDurability (BlockIndex);

							//BlockData MyBlockData =  GetManager.GetMyDataManager().GetBlockData(BlockIndex);
							if (MyBlockData != null) {
								int ItemDropType = MyBlockData.DropIndex;
								ItemDrop = GetManager.GetTerrainManager ().BlockPrefab;
								if (ItemDrop != null) {// && ItemDropType != 0) {
									Item DropItem = null;
									for (int i = 0; i < GetManager.GetDataManager().ItemsList.Count; i++) {
										Item NewItem = GetManager.GetDataManager().ItemsList[i];
										if (NewItem.Name == MyBlockData.Name) {
											DropItem = new Item(NewItem);
											break;
										}
									}
									if (DropItem != null) {
										GameObject ItemDropObject = (GameObject)Instantiate (ItemDrop, ItemDropPosition, Quaternion.identity);
										ItemDropObject.GetComponent<ItemPickup> ().DropItem = DropItem;
										ItemDropObject.GetComponent<ItemPickup> ().IsAddEffect = false;
										ItemDropObject.GetComponent<ItemPickup> ().IsStatIncrease = false;
										ItemDropObject.GetComponent<ItemPickup> ().DropType = MyBlockData.DropType;
										ItemDropObject.GetComponent<MeshRenderer> ().material.mainTexture = GetManager.GetDataManager ().BlocksTextureManager.BlockTextures [BlockIndex];
									}
								}
							}
						}
					}
				}
			}
		}
	}