Lots of stuff I forgot to commit because Holidays

- D&D dice engine (see README)
- Markdown support
- Phantom camera
This commit is contained in:
Tony Bark 2023-12-24 20:39:57 -05:00
parent 9589acd877
commit 2b41f84b05
125 changed files with 13170 additions and 23 deletions

View file

@ -0,0 +1,16 @@
extends Area2D
@export var area_pcam: PhantomCamera2D
func _ready() -> void:
connect("area_entered", _entered_area)
connect("area_exited", _exited_area)
func _entered_area(area_2D: Area2D) -> void:
if area_2D.get_parent() is CharacterBody2D:
area_pcam.set_priority(20)
func _exited_area(area_2D: Area2D) -> void:
if area_2D.get_parent() is CharacterBody2D:
area_pcam.set_priority(0)

View file

@ -0,0 +1,170 @@
extends CharacterBody2D
@onready var player_area2D = %PlayerArea2D
@onready var player_sprite: Sprite2D = %PlayerSprite
@onready var interaction_prompt: Panel = %InteractionPrompt
@onready var ui_sign:Control = %UISign
@onready var item_pcam2D: PhantomCamera2D = %ItemFocusPhantomCamera2D
@onready var inventory_pcam2D: PhantomCamera2D = %InventoryPhantomCamera2D
@onready var dark_overlay: ColorRect = %DarkOverlay
const KEY_STRINGNAME: StringName = "Key"
const ACTION_STRINGNAME: StringName = "Action"
const INPUT_MOVE_LEFT_STRINGNAME: StringName = "move_left"
const INPUT_MOVE_RIGHT_STRINGNAME: StringName = "move_right"
const SPEED = 350.0
const JUMP_VELOCITY = -750.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: int = 2400
var _is_interactive: bool
var _can_open_inventory: bool
var _movement_disabled: bool
var tween: Tween
var _interactive_UI: Control
var _active_pcam: PhantomCamera2D
enum InteractiveType {
NONE = 0,
ITEM = 1,
INVENTORY = 2,
}
var _interactive_object: InteractiveType = InteractiveType.NONE
var InputMovementDic: Dictionary = {
INPUT_MOVE_LEFT_STRINGNAME: {
KEY_STRINGNAME: KEY_A,
ACTION_STRINGNAME: INPUT_MOVE_LEFT_STRINGNAME
},
INPUT_MOVE_RIGHT_STRINGNAME: {
KEY_STRINGNAME: KEY_D,
ACTION_STRINGNAME: INPUT_MOVE_RIGHT_STRINGNAME
},
}
func _ready() -> void:
player_area2D.connect("body_shape_entered", _show_prompt)
player_area2D.connect("body_shape_exited", _hide_prompt)
for input in InputMovementDic:
var key_val = InputMovementDic[input].get(KEY_STRINGNAME)
var action_val = InputMovementDic[input].get(ACTION_STRINGNAME)
var movement_input = InputEventKey.new()
movement_input.physical_keycode = key_val
InputMap.add_action(action_val)
InputMap.action_add_event(action_val, movement_input)
func _unhandled_input(event: InputEvent) -> void:
if _is_interactive:
if Input.is_physical_key_pressed(KEY_F):
if tween:
tween.kill()
if not _movement_disabled:
tween = get_tree().create_tween()
_movement_disabled = true
_active_pcam.set_priority(10)
_show_interactive_node(_interactive_UI)
_interactive_node_logic()
else:
_hide_interactive_node(_interactive_UI)
_interactive_node_logic()
if Input.is_physical_key_pressed(KEY_ESCAPE) and _movement_disabled:
_hide_interactive_node(_interactive_UI)
_interactive_node_logic()
func _show_interactive_node(UI: Control) -> void:
UI.modulate.a = 0
UI.visible = true
tween.tween_property(UI, "modulate", Color.WHITE, 1).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_CIRC)
func _hide_interactive_node(UI: Control) -> void:
_movement_disabled = false
_active_pcam.set_priority(0)
UI.visible = false
func _interactive_node_logic() -> void:
match _interactive_object:
2:
if _movement_disabled:
dark_overlay.set_visible(true)
else:
dark_overlay.set_visible(false)
func _physics_process(delta: float) -> void:
if _movement_disabled: return
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
var input_dir: = Input.get_axis(
INPUT_MOVE_LEFT_STRINGNAME,
INPUT_MOVE_RIGHT_STRINGNAME
)
if input_dir:
velocity.x = input_dir * SPEED
if input_dir > 0:
player_sprite.set_flip_h(false)
elif input_dir < 0:
player_sprite.set_flip_h(true)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func _show_prompt(body_rid: RID, body: Node2D, body_shape_index: int, local_shape: int) -> void:
if body is TileMap:
var tile_map: TileMap = body
var tile_coords: Vector2i = tile_map.get_coords_for_body_rid(body_rid)
var cell_data: TileData = tile_map.get_cell_tile_data(1, tile_coords)
if cell_data:
var cell_data_type: StringName = cell_data.get_custom_data("Type")
# var cell_global_pos: Vector2 = tile_map.to_global(tile_map.map_to_local(tile_coords))
_is_interactive = true
interaction_prompt.set_visible(true)
match cell_data_type:
"Sign":
_interactive_UI = %UISign
_active_pcam = item_pcam2D
_interactive_object = InteractiveType.ITEM
"Inventory":
_interactive_UI = %UIInventory
_interactive_object = InteractiveType.INVENTORY
_active_pcam = inventory_pcam2D
func _hide_prompt(body_rid: RID, body: Node2D, body_shape_index: int, local_shape: int) -> void:
if body is TileMap:
var tile_map: TileMap = body
var tile_coords: Vector2i = tile_map.get_coords_for_body_rid(body_rid)
var cell_data: TileData = tile_map.get_cell_tile_data(1, tile_coords)
if cell_data:
interaction_prompt.set_visible(false)
_is_interactive = false
_interactive_UI = null
_interactive_object = InteractiveType.NONE
_active_pcam = null

View file

@ -0,0 +1,26 @@
extends Area3D
@export var area_pcam: PhantomCamera3D
var initial_camera_position: Vector3
var initial_camera_rotation: Vector3
var tween: Tween
var tween_duration: float = 0.9
func _ready() -> void:
connect("area_entered", _entered_area)
connect("area_exited", _exited_area)
func _entered_area(area_3D: Area3D) -> void:
if area_3D.get_parent() is CharacterBody3D:
area_pcam.set_priority(20)
func _exited_area(area_3D: Area3D) -> void:
if area_3D.get_parent() is CharacterBody3D:
area_pcam.set_priority(0)

View file

@ -0,0 +1,66 @@
extends Node3D
@onready var npc_pcam: PhantomCamera3D = %NPCPhantomCamera3D
@onready var dialogueArea: Area3D = %NPCInteractionArea3D
@onready var dialogueLabel3D: Label3D = %NPCDialogueExampleLabel
@onready var player: CharacterBody3D = %PlayerCharacterBody3D
@onready var move_to_location: Vector3 = %MoveToLocation.get_global_position()
var dialogue_label_initial_position: Vector3
var dialogue_label_initial_rotation: Vector3
var tween: Tween
var tween_duration: float = 0.9
var tween_transition: Tween.TransitionType = Tween.TRANS_QUAD
var interactable: bool
var is_interacting: bool
func _ready() -> void:
dialogueArea.connect("area_entered", _interactable)
dialogueArea.connect("area_exited", _not_interactable)
dialogueLabel3D.set_visible(false)
dialogue_label_initial_position = dialogueLabel3D.get_global_position()
dialogue_label_initial_rotation = dialogueLabel3D.get_global_rotation()
func _interactable(area_3D: Area3D) -> void:
if area_3D.get_parent() is CharacterBody3D:
dialogueLabel3D.set_visible(true)
interactable = true
var tween: Tween = get_tree().create_tween().set_trans(tween_transition).set_ease(Tween.EASE_IN_OUT).set_loops()
tween.tween_property(dialogueLabel3D, "position", dialogue_label_initial_position - Vector3(0, -0.2, 0), tween_duration)
tween.tween_property(dialogueLabel3D, "position", dialogue_label_initial_position, tween_duration)
func _not_interactable(area_3D: Area3D) -> void:
if area_3D.get_parent() is CharacterBody3D:
dialogueLabel3D.set_visible(false)
interactable = false
func _input(event) -> void:
if not interactable: return
if event is InputEventKey and event.pressed:
if event.keycode == KEY_F:
var tween: Tween = get_tree().create_tween() \
.set_parallel(true) \
.set_trans(Tween.TRANS_QUART) \
.set_ease(Tween.EASE_IN_OUT)
if not is_interacting:
npc_pcam.set_priority(20)
player.set_physics_process(false)
tween.tween_property(player, "position", move_to_location, 0.6).set_trans(tween_transition)
tween.tween_property(dialogueLabel3D, "rotation", Vector3(deg_to_rad(-20), deg_to_rad(53), 0), 0.6).set_trans(tween_transition)
else:
npc_pcam.set_priority(0)
player.set_physics_process(true)
tween.tween_property(dialogueLabel3D, "rotation", dialogue_label_initial_rotation, 0.9)
is_interacting = !is_interacting

View file

@ -0,0 +1,18 @@
extends Node
@export var path_pcam: PhantomCamera3D
func _ready() -> void:
connect("area_entered", _entered_area)
connect("area_exited", _exited_area)
func _entered_area(area_3D: Area3D) -> void:
if area_3D.get_parent() is CharacterBody3D:
path_pcam.set_priority(20)
func _exited_area(area_3D: Area3D) -> void:
if area_3D.get_parent() is CharacterBody3D:
path_pcam.set_priority(0)

View file

@ -0,0 +1,81 @@
extends CharacterBody3D
@export var SPEED: float = 5.0
@export var JUMP_VELOCITY: float = 4.5
@export var enable_gravity = true
@onready var _camera: Camera3D = %MainCamera3D
@onready var _player_pcam: PhantomCamera3D = %PlayerPhantomCamera3D
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: float = 9.8
const KEY_STRINGNAME: StringName = "Key"
const ACTION_STRINGNAME: StringName = "Action"
const INPUT_MOVE_UP_STRINGNAME: StringName = "move_up"
const INPUT_MOVE_DOWM_STRINGNAME: StringName = "move_down"
const INPUT_MOVE_LEFT_STRINGNAME: StringName = "move_left"
const INPUT_MOVE_RIGHT_STRINGNAME: StringName = "move_right"
var InputMovementDic: Dictionary = {
INPUT_MOVE_UP_STRINGNAME: {
KEY_STRINGNAME: KEY_W,
ACTION_STRINGNAME: INPUT_MOVE_UP_STRINGNAME
},
INPUT_MOVE_DOWM_STRINGNAME: {
KEY_STRINGNAME: KEY_S,
ACTION_STRINGNAME: INPUT_MOVE_DOWM_STRINGNAME
},
INPUT_MOVE_LEFT_STRINGNAME: {
KEY_STRINGNAME: KEY_A,
ACTION_STRINGNAME: INPUT_MOVE_LEFT_STRINGNAME
},
INPUT_MOVE_RIGHT_STRINGNAME: {
KEY_STRINGNAME: KEY_D,
ACTION_STRINGNAME: INPUT_MOVE_RIGHT_STRINGNAME
},
}
func _ready() -> void:
for input in InputMovementDic:
var key_val = InputMovementDic[input].get(KEY_STRINGNAME)
var action_val = InputMovementDic[input].get(ACTION_STRINGNAME)
var movement_input = InputEventKey.new()
movement_input.physical_keycode = key_val
InputMap.add_action(action_val)
InputMap.action_add_event(action_val, movement_input)
func _physics_process(delta: float) -> void:
# Add the gravity.
if enable_gravity and not is_on_floor():
velocity.y -= gravity * delta
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir: Vector2 = Input.get_vector(
INPUT_MOVE_LEFT_STRINGNAME,
INPUT_MOVE_RIGHT_STRINGNAME,
INPUT_MOVE_UP_STRINGNAME,
INPUT_MOVE_DOWM_STRINGNAME
)
var cam_dir: Vector3 = -_camera.global_transform.basis.z
var direction: Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
var move_dir: Vector3 = Vector3.ZERO
move_dir.x = direction.x
move_dir.z = direction.z
move_dir = move_dir.rotated(Vector3.UP, _camera.rotation.y).normalized()
velocity.x = move_dir.x * SPEED
velocity.z = move_dir.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()

View file

@ -0,0 +1,83 @@
extends "player_controller.gd"
@onready var _aim_pcam: PhantomCamera3D = %PlayerAimPhantomCamera3D
@onready var _model: Node3D = $PlayerModel
@onready var _ceiling_pcam: PhantomCamera3D = %CeilingPhantomCamera3D
@export var mouse_sensitivity: float = 0.05
@export var min_yaw: float = -89.9
@export var max_yaw: float = 50
@export var min_pitch: float = 0
@export var max_pitch: float = 360
func _ready() -> void:
super()
if _player_pcam.get_follow_mode() == _player_pcam.Constants.FollowMode.THIRD_PERSON:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta: float) -> void:
super(delta)
if velocity.length() > 0.2:
var look_direction: Vector2 = Vector2(velocity.z, velocity.x)
_model.rotation.y = look_direction.angle()
func _unhandled_input(event: InputEvent) -> void:
if _player_pcam.get_follow_mode() == _player_pcam.Constants.FollowMode.THIRD_PERSON:
var active_pcam: PhantomCamera3D
if is_instance_valid(_aim_pcam):
_set_pcam_rotation(_player_pcam, event)
_set_pcam_rotation(_aim_pcam, event)
if _player_pcam.get_priority() > _aim_pcam.get_priority():
_toggle_aim_pcam(event)
else:
_toggle_aim_pcam(event)
if event is InputEventKey and event.pressed:
if event.keycode == KEY_SPACE:
if _ceiling_pcam.get_priority() < 30 and _player_pcam.is_active():
_ceiling_pcam.set_priority(30)
else:
_ceiling_pcam.set_priority(1)
func _set_pcam_rotation(pcam: PhantomCamera3D, event: InputEvent) -> void:
if event is InputEventMouseMotion:
var pcam_rotation_degrees: Vector3
# Assigns the current 3D rotation of the SpringArm3D node - so it starts off where it is in the editor
pcam_rotation_degrees = pcam.get_third_person_rotation_degrees()
# Change the X rotation
pcam_rotation_degrees.x -= event.relative.y * mouse_sensitivity
# Clamp the rotation in the X axis so it go over or under the target
pcam_rotation_degrees.x = clampf(pcam_rotation_degrees.x, min_yaw, max_yaw)
# Change the Y rotation value
pcam_rotation_degrees.y -= event.relative.x * mouse_sensitivity
# Sets the rotation to fully loop around its target, but witout going below or exceeding 0 and 360 degrees respectively
pcam_rotation_degrees.y = wrapf(pcam_rotation_degrees.y, min_pitch, max_pitch)
# Change the SpringArm3D node's rotation and rotate around its target
pcam.set_third_person_rotation_degrees(pcam_rotation_degrees)
func _toggle_aim_pcam(event: InputEvent) -> void:
if event is InputEventMouseButton \
and event.is_pressed() \
and event.button_index == 2 \
and (_player_pcam.is_active() or _aim_pcam.is_active()):
if _player_pcam.get_priority() > _aim_pcam.get_priority():
_aim_pcam.set_priority(30)
else:
_aim_pcam.set_priority(0)