karl ff1d85a325 Add Pill singleton with basic behavior
A pill can be taken (key 'E', currently in Player.gd for testing) to
increase the level up to the max level (currently 6).

The drugged level gradually decreases down to the min level
(currently 0).
2019-11-11 21:54:17 +01:00

96 lines
2.4 KiB
GDScript

extends KinematicBody
# export variable
export(NodePath) var body_nodepath
# const
const GRAVITY = -24.8
const JUMP_SPEED = 8
const MOVE_SPEED = 20
const SPRINT_SPEED = 40
const ACCEL = 4.5
const MAX_SLOPE_ANGLE = 40
const MOUSE_SENSITIVITY = 0.05
# private members
var _body: Spatial
var _camera: Camera
var _dir = Vector3();
var _vel = Vector3();
var _is_sprinting;
func _ready():
_body = get_node(body_nodepath) as Spatial # = $Body
assert(null != _body)
_camera = $Body/Camera
assert(null != _camera)
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
process_input()
process_movement(delta)
func process_input():
# Walking
var input_movement_vector = Vector2()
if Input.is_action_pressed("move_fwrd"):
input_movement_vector.y += 1
if Input.is_action_pressed("move_back"):
input_movement_vector.y -= 1
if Input.is_action_pressed("move_left"):
input_movement_vector.x -= 1
if Input.is_action_pressed("move_right"):
input_movement_vector.x += 1
# look around with mouse
_dir = Vector3()
var camera_transform = _camera.get_global_transform()
_dir += -camera_transform.basis.z * input_movement_vector.y
_dir += camera_transform.basis.x * input_movement_vector.x
# jumping
if Input.is_action_just_pressed("move_jump") and is_on_floor():
_vel.y = JUMP_SPEED
# sprinting
_is_sprinting = Input.is_action_pressed("move_sprint")
# Taking a pill
# TODO: Should be moved to a different component which only handles pills - this script should only do movement
# Only here for testing purposes!
if Input.is_action_just_pressed("take_pill"):
Pills.take_pill()
Logger.info("Player took a pill; new level: %s" % [Pills.get_round_level()])
func process_movement(delta):
_vel.y += delta * GRAVITY
# set movement speed
var target = _dir
if _is_sprinting:
target *= SPRINT_SPEED
else:
target *= MOVE_SPEED
var hvel = _vel
hvel = hvel.linear_interpolate(target, ACCEL * delta)
_vel.x = hvel.x
_vel.z = hvel.z
_vel = move_and_slide(_vel, Vector3(0, 1, 0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE))
func _input(event):
# capture mouse movement
if event is InputEventMouseMotion:
_body.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
var camera_rot = _body.rotation_degrees
camera_rot.x = clamp(camera_rot.x, -70, 70)
_body.rotation_degrees = camera_rot