Added picking with MMB

master
Marc Gilleron 2020-07-28 22:36:40 +01:00
parent 39f48b106c
commit 366e3e1d5f
3 changed files with 33 additions and 3 deletions

View File

@ -93,6 +93,13 @@ func get_block(id: int) -> Block:
return _blocks[id]
func get_raw_mapping(raw_id: int) -> RawMapping:
assert(raw_id >= 0)
var rm = _raw_mappings[raw_id]
assert(rm != null)
return rm
func get_block_count() -> int:
return len(_blocks)
@ -104,6 +111,9 @@ func _create_block(params: Dictionary):
"backface_culling": true,
"directory": params.name
})
var block = Block.new()
block.id = len(_blocks)
for i in len(params.voxels):
var vname = params.voxels[i]
@ -112,9 +122,13 @@ func _create_block(params: Dictionary):
push_error("Could not find voxel named {0}".format([vname]))
assert(id != -1)
params.voxels[i] = id
var rm = RawMapping.new()
rm.block_id = block.id
rm.variant_index = i
if id >= len(_raw_mappings):
_raw_mappings.resize(id + 1)
_raw_mappings[id] = rm
var block = Block.new()
block.id = len(_blocks)
block.name = params.name
block.directory = params.directory
block.rotation_type = params.rotation_type

View File

@ -35,3 +35,10 @@ func select_slot(i: int):
func get_selected_block_type() -> int:
return _inventory[_inventory_index]
func try_select_slot_by_block_id(block_id: int):
for i in len(_inventory):
var id = _inventory[i]
if id == block_id:
select_slot(i)
break

View File

@ -29,6 +29,7 @@ var _terrain_tool = null
var _cursor = null
var _action_place = false
var _action_remove = false
var _action_pick = false
func _ready():
@ -72,7 +73,8 @@ func _physics_process(delta):
# These inputs have to be in _fixed_process because they rely on collision queries
if hit != null:
var has_cube = _terrain_tool.get_voxel(hit.position) != 0
var hit_raw_id = _terrain_tool.get_voxel(hit.position)
var has_cube = hit_raw_id != 0
if _action_remove and has_cube:
var pos = hit.position
@ -89,9 +91,14 @@ func _physics_process(delta):
print("Place voxel at ", pos)
else:
print("Can't place here!")
elif _action_pick:
var rm := Blocks.get_raw_mapping(hit_raw_id)
_hotbar.try_select_slot_by_block_id(rm.block_id)
_action_place = false
_action_remove = false
_action_pick = false
func _unhandled_input(event):
@ -102,6 +109,8 @@ func _unhandled_input(event):
_action_remove = true
BUTTON_RIGHT:
_action_place = true
BUTTON_MIDDLE:
_action_pick = true
elif event is InputEventKey:
if event.pressed: