Have mouse wheel cycle through hotbar

master
Marc Gilleron 2020-07-29 21:27:50 +01:00
parent 45950e40af
commit 0bd06d5639
4 changed files with 97 additions and 2 deletions

View File

@ -20,6 +20,7 @@ func _ready():
func select_slot(i: int):
if _inventory_index == i:
return
assert(i >= 0 and i < len(_inventory))
_inventory_index = i
var block_id = _inventory[_inventory_index]
@ -42,3 +43,19 @@ func try_select_slot_by_block_id(block_id: int):
if id == block_id:
select_slot(i)
break
func select_next_slot():
var i = _inventory_index + 1
if i >= len(_inventory):
i = 0
select_slot(i)
func select_previous_slot():
var i = _inventory_index - 1
if i < 0:
i = len(_inventory) - 1
select_slot(i)

View File

@ -0,0 +1,74 @@
extends Spatial
export var sensitivity = 0.4
export var min_angle = -90
export var max_angle = 90
export var capture_mouse = true
export var distance = 0.0
var _yaw = 0
var _pitch = 0
var _offset = Vector3()
func _ready():
_offset = get_translation()
if capture_mouse:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event):
if event is InputEventMouseButton:
if event.pressed and Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
if capture_mouse:
# Capture the mouse
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
# The game uses the wheel already, put that "debug" adjustment behind a modifier
if event.control:
if event.button_index == BUTTON_WHEEL_UP:
distance = max(distance - 1 - distance * 0.1, 0)
update_rotations()
elif event.button_index == BUTTON_WHEEL_DOWN:
distance = max(distance + 1 + distance * 0.1, 0)
update_rotations()
elif event is InputEventMouseMotion:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED || not capture_mouse:
# Get mouse delta
var motion = event.relative
# Add to rotations
_yaw -= motion.x * sensitivity
_pitch += motion.y * sensitivity
# Clamp pitch
var e = 0.001
if _pitch > max_angle-e:
_pitch = max_angle-e
elif _pitch < min_angle+e:
_pitch = min_angle+e
# Apply rotations
update_rotations()
elif event is InputEventKey:
if event.pressed:
if event.scancode == KEY_ESCAPE:
# Get the mouse back
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
elif event.scancode == KEY_I:
var pos = get_translation()
var fw = -transform.basis.z
print("Position: ", pos, ", Forward: ", fw)
func update_rotations():
set_translation(Vector3())
set_rotation(Vector3(0, deg2rad(_yaw), 0))
rotate(get_transform().basis.x.normalized(), -deg2rad(_pitch))
set_translation(get_transform().basis.z * distance + _offset)

View File

@ -111,6 +111,10 @@ func _unhandled_input(event):
_action_place = true
BUTTON_MIDDLE:
_action_pick = true
BUTTON_WHEEL_DOWN:
_hotbar.select_next_slot()
BUTTON_WHEEL_UP:
_hotbar.select_previous_slot()
elif event is InputEventKey:
if event.pressed:

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=8 format=2]
[ext_resource path="res://blocky_game/player/character_controller.gd" type="Script" id=1]
[ext_resource path="res://common/mouse_look.gd" type="Script" id=2]
[ext_resource path="res://blocky_game/player/avatar_camera.gd" type="Script" id=2]
[ext_resource path="res://blocky_game/player/avatar_interaction.gd" type="Script" id=3]
[ext_resource path="res://blocky_terrain/crosshair.png" type="Texture" id=4]
[ext_resource path="res://blocky_terrain/center.gd" type="Script" id=5]
@ -23,7 +23,7 @@ near = 0.1
far = 500.0
script = ExtResource( 2 )
sensitivity = 0.3
distance = 4.0
distance = 0.0
[node name="Interaction" type="Node" parent="."]
script = ExtResource( 3 )