diff --git a/WikiJam/Level/Level.tscn b/WikiJam/Level/Level.tscn index 19bd56d..d11eacb 100644 --- a/WikiJam/Level/Level.tscn +++ b/WikiJam/Level/Level.tscn @@ -1,3 +1,32 @@ -[gd_scene format=2] +[gd_scene load_steps=5 format=2] -[node name="Spatial" type="Spatial"] +[ext_resource path="res://Player/Player.tscn" type="PackedScene" id=1] + +[sub_resource type="NavigationMesh" id=3] + +[sub_resource type="BoxShape" id=2] +extents = Vector3( 10, 1, 10 ) + +[sub_resource type="PlaneMesh" id=4] +size = Vector2( 20, 20 ) + +[node name="Level" type="Spatial"] + +[node name="Spatial" parent="." instance=ExtResource( 1 )] + +[node name="Navigation" type="Navigation" parent="."] +editor/display_folded = true + +[node name="NavigationMeshInstance" type="NavigationMeshInstance" parent="Navigation"] +navmesh = SubResource( 3 ) + +[node name="StaticBody" type="StaticBody" parent="Navigation/NavigationMeshInstance"] + +[node name="CollisionShape" type="CollisionShape" parent="Navigation/NavigationMeshInstance/StaticBody"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 ) +shape = SubResource( 2 ) + +[node name="MeshInstance" type="MeshInstance" parent="Navigation/NavigationMeshInstance"] +mesh = SubResource( 4 ) +skeleton = NodePath("") +material/0 = null diff --git a/WikiJam/Player/Player.gd b/WikiJam/Player/Player.gd new file mode 100644 index 0000000..5bb24cb --- /dev/null +++ b/WikiJam/Player/Player.gd @@ -0,0 +1,108 @@ +extends KinematicBody + +# export variables +export(NodePath) var body_nodepath +#export(NodePath) var lookingAt_nodepath +export(NodePath) var animation_nodepath +export(NodePath) var ui_nodepath +export(NodePath) var camera_nodepath + +# const +const GRAVITY = -24.8 +const JUMP_SPEED = 8 +const MOVE_SPEED = 6 +const SPRINT_SPEED = 10 +const ACCEL = 15.0 +const MAX_SLOPE_ANGLE = 40 +const MOUSE_SENSITIVITY = 0.05 +const INTERACT_DISTANCE = 4 + +# private members +var _body: Spatial +var _camera: Camera +#var _lookCast: RayCast +var _animation: AnimationPlayer +var _interface: Control +var _dir = Vector3(); +var _vel = Vector3(); +var _is_sprinting; + + +func _ready(): + # Assign child node variables + _body = get_node(body_nodepath) as Spatial + assert(null != _body) + + _camera = get_node(camera_nodepath) as Camera + assert(null != _camera) + + _animation = get_node(animation_nodepath) as AnimationPlayer + assert(null != _animation) + + # Setup mouse look + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + #_lookCast = get_node(lookingAt_nodepath) as RayCast + #_lookCast.cast_to = Vector3(0, 0, INTERACT_DISTANCE) + + _interface = get_node(ui_nodepath) as Control + assert(null != _interface) + + +func _physics_process(delta): + process_input() + process_movement(delta) + +func _process(_delta): + process_animations() + +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") + + +func process_movement(delta): + _vel.y += delta * GRAVITY + + # set movement speed + var target = _dir * (SPRINT_SPEED if _is_sprinting else 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 process_animations(): + _animation.playback_speed = _vel.length() / MOVE_SPEED + + +func _input(event): + # capture mouse movement + if event is InputEventMouseMotion: + _camera.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1)) + self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1)) + + # Prevent player from doing a purzelbaum + _camera.rotation_degrees.x = clamp(_camera.rotation_degrees.x, -70, 70) diff --git a/WikiJam/Player/Player.tscn b/WikiJam/Player/Player.tscn index 19bd56d..3b73066 100644 --- a/WikiJam/Player/Player.tscn +++ b/WikiJam/Player/Player.tscn @@ -1,3 +1,27 @@ -[gd_scene format=2] +[gd_scene load_steps=3 format=2] -[node name="Spatial" type="Spatial"] +[ext_resource path="res://Player/Player.gd" type="Script" id=1] + +[sub_resource type="CylinderShape" id=1] + +[node name="Spatial" type="KinematicBody"] +script = ExtResource( 1 ) +body_nodepath = NodePath("Body") +animation_nodepath = NodePath("WalkAnimationPlayer") +ui_nodepath = NodePath("HUD") +camera_nodepath = NodePath("Camera") + +[node name="Body" type="Spatial" parent="."] + +[node name="Camera" type="Camera" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 ) + +[node name="Collider" type="CollisionShape" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 ) +shape = SubResource( 1 ) + +[node name="WalkAnimationPlayer" type="AnimationPlayer" parent="."] + +[node name="HUD" type="Control" parent="."] +margin_right = 40.0 +margin_bottom = 40.0 diff --git a/WikiJam/project.godot b/WikiJam/project.godot index 2109ee8..1104847 100644 --- a/WikiJam/project.godot +++ b/WikiJam/project.godot @@ -57,11 +57,45 @@ _global_script_class_icons={ [application] config/name="WikiJam" +run/main_scene="res://Level/Level.tscn" config/icon="res://icon.png" [autoload] -logger="*res://Util/gs_logger/logger.gd" +Logger="*res://Util/gs_logger/logger.gd" + +[input] + +move_fwrd={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null) + ] +} +move_back={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null) + ] +} +move_left={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null) + ] +} +move_right={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null) + ] +} +move_jump={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null) + ] +} +move_sprint={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777237,"unicode":0,"echo":false,"script":null) + ] +} [rendering]