public override bool PlantOn(IntVector3 blockPos) { Tuple <IntVector3, byte>[] treeBlocks = new Tuple <IntVector3, byte> [48]; for (int i = 0; i < 48; i++) { treeBlocks[i] = Tuple.Create(blockPos + treeBlockVectors[i].Item1, treeBlockVectors[i].Item2); } if (!CanGrowOn(blockPos)) { return(false); } terrain.SetBlocks(treeBlocks); plantBlocks.Add(blockPos); Tuple <int, int> gridPos = Tuple.Create(blockPos.x / GRID_SIZE, blockPos.z / GRID_SIZE); if (!grid.ContainsKey(gridPos)) { grid[gridPos] = new List <IntVector3>(); } grid[gridPos].Add(blockPos); CollisionShape collisionShape = new CollisionShape(); BoxShape b = new BoxShape(); b.SetExtents(new Vector3(Block.SIZE * 3.0f, 4.0f * Block.SIZE, Block.SIZE * 3.0f)); //trees are 8ish blocks tall collisionShape.SetShape(b); PhysicsBody physicsBody = new StaticBody(); physicsBody.AddToGroup("plants"); physicsBody.AddToGroup("alive"); //Don't collide with player physicsBody.SetCollisionLayerBit(0, false); physicsBody.SetCollisionMaskBit(0, false); //But still collide with giants physicsBody.SetCollisionLayerBit(2, true); //And still get picked up by the giant areas physicsBody.SetCollisionMaskBit(31, true); Vector3 position = (blockPos + new Vector3(0, 5, 0)) * Block.SIZE + new Vector3(0, 5, 0); //the plus 5 to make it actually seen physicsBody.SetTranslation(position); physicsBody.AddChild(collisionShape); physicsBodies[blockPos] = physicsBody; plants.AddChild(physicsBody); return(true); }
public bool PlaceBlock(byte blockId) { var hitInfo = GetHitInfo(); if (hitInfo != null) { Vector3 pos = (Vector3)hitInfo["position"] + (Vector3)hitInfo["normal"] * 0.5f * Block.SIZE; IntVector3 blockPos = new IntVector3((int)Mathf.Round(pos.x / Block.SIZE), (int)Mathf.Round(pos.y / Block.SIZE), (int)Mathf.Round(pos.z / Block.SIZE)); Vector3 blockCollisionPos = new Vector3(blockPos.x, blockPos.y, blockPos.z) * Block.SIZE; BoxShape bs = new BoxShape(); bs.SetExtents(new Vector3(Block.SIZE, Block.SIZE, Block.SIZE) / 2); PhysicsShapeQueryParameters psqp = new PhysicsShapeQueryParameters(); psqp.SetShape(bs); Transform t = new Transform(new Vector3(1.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, 0.0f, 0.0f)).Translated(blockCollisionPos); psqp.SetTransform(t); object[] res = spaceState.IntersectShape(psqp); if (res.Length > 0) { for (int i = 0; i < res.Length; i++) { Dictionary <object, object> info = (Dictionary <object, object>)spaceState.IntersectShape(psqp)[i]; if (info["collider"] is KinematicBody) { // A moving body (player, animal etc.) is in the way return(false); } } } terrain.SetBlock(blockPos, blockId); return(true); } return(false); }