diff --git a/buoyancy/Player.gd b/buoyancy/Player.gd index 749c777..767994e 100644 --- a/buoyancy/Player.gd +++ b/buoyancy/Player.gd @@ -27,8 +27,6 @@ var bound_offset = Vector2() var state = STATE.WALKING -var _oldtf = Transform() - # Called when the node enters the scene tree for the first time. func _ready(): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) @@ -38,7 +36,7 @@ func _input(event): if not paused: if event is InputEventMouseMotion: camera_base.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity)) - + var x_delta = event.relative.y * mouse_sensitivity if camera_x_rotation + x_delta > -90 and camera_x_rotation + x_delta < 90: camera.rotate_x(deg2rad(-x_delta)) @@ -50,24 +48,24 @@ func grounded(): func _integrate_forces(physics_state): # State machine state = swap_state() - + if not paused: var acc = Vector3() var direction = Vector3() var camera_base_basis = camera.get_global_transform().basis var mod = SPRINT_MOD if Input.is_action_pressed("sprint") else 1 - + if Input.is_action_pressed("forward"): direction -= camera_base_basis.z #forward is negative in Godot if Input.is_action_pressed("backward"): direction += camera_base_basis.z - + # Strafe if Input.is_action_pressed("left"): direction -= camera_base_basis.x if Input.is_action_pressed("right"): direction += camera_base_basis.x - + match state: STATE.WALKING: if Input.is_action_just_pressed("jump") and grounded(): @@ -83,13 +81,13 @@ func _integrate_forces(physics_state): acc = direction * SPEED * mod STATE.FLOATING: physics_state.add_central_force(Vector3.DOWN * 9.8) - + var wave = ocean.get_wave(translation.x, translation.z) var wave_height = wave.y var height = translation.y - + acc = direction * SPEED * mod - + if height < wave_height: var buoyancy = clamp((wave_height - height) / 0.001, 0, 1) * 5 physics_state.add_central_force(Vector3(0, 9.8 * buoyancy, 0)) @@ -98,14 +96,12 @@ func _integrate_forces(physics_state): STATE.SAILING: self.transform = bound_object.transform self.translate(bound_offset) - + physics_state.add_central_force(acc * mass) - + # emit signal - playerinfo_updated - var tf = $Head/Camera.get_global_transform_interpolated() - if tf != _oldtf: - Events.emit_signal("player_transform_updated", $Head/Camera.get_global_translation(), $Head.global_rotation.y) - _oldtf = tf + Events.emit_signal("player_transform_updated", $Head/Camera.get_global_translation(), $Head.global_rotation.y) + func swap_state(): @@ -128,7 +124,7 @@ func swap_state(): new_state = STATE.WALKING if translation.y < wave_height - 0.1: new_state = STATE.DIVING - + return new_state func _process(delta):