55
scripts/Entities/BasicNPCEntity.gd
Normal file
55
scripts/Entities/BasicNPCEntity.gd
Normal file
@@ -0,0 +1,55 @@
|
||||
class_name BasicNPCEntity extends "res://scripts/Entities/OverworldEntity.gd"
|
||||
const Cutscene = preload("res://scripts/Cutscene/Cutscene.gd");
|
||||
const OverworldConversationEvent = preload("res://scripts/Cutscene/Scene/OverworldConversationEvent.gd");
|
||||
|
||||
enum BasicNPCInteractType {
|
||||
NONE,
|
||||
CUTSCENE,
|
||||
TEXTS
|
||||
};
|
||||
|
||||
enum BasicNPCMoveType {
|
||||
STILL,
|
||||
RANDOM_LOOK
|
||||
};
|
||||
|
||||
@export var interactType:BasicNPCInteractType = BasicNPCInteractType.NONE;
|
||||
@export var interactCutscene:GDScript;
|
||||
@export var interactTexts:Array[String];
|
||||
|
||||
@export var moveType:BasicNPCMoveType = BasicNPCMoveType.STILL;
|
||||
|
||||
@export var randomLookMinTime:float = 1.5;
|
||||
@export var randomLookMaxTime:float = 4.0;
|
||||
var randomLookTimer:float = 0.0;
|
||||
|
||||
func interact(interactor:OverworldEntity) -> void:
|
||||
if interactType == BasicNPCInteractType.NONE:
|
||||
return
|
||||
|
||||
if interactType == BasicNPCInteractType.CUTSCENE:
|
||||
# Cutscene in this manner must take two entities
|
||||
# (self, speaker, and interactor, player)
|
||||
var cs:Cutscene = interactCutscene.new(self, interactor);
|
||||
getSystems().CUTSCENE.setCurrentCutscene(cs);
|
||||
return
|
||||
|
||||
if interactType == BasicNPCInteractType.TEXTS:
|
||||
var cs:Cutscene = OverworldConversationEvent.new(self, interactor, interactTexts);
|
||||
getSystems().CUTSCENE.setCurrentCutscene(cs);
|
||||
return
|
||||
|
||||
pass
|
||||
|
||||
func updateMovement(delta:float) -> void:
|
||||
if moveType == BasicNPCMoveType.STILL:
|
||||
return
|
||||
|
||||
if moveType == BasicNPCMoveType.RANDOM_LOOK:
|
||||
randomLookTimer -= delta;
|
||||
if randomLookTimer <= 0:
|
||||
randomLookTimer = randf_range(randomLookMinTime, randomLookMaxTime);
|
||||
self.direction = randi_range(0, 3);
|
||||
return
|
||||
|
||||
pass
|
1
scripts/Entities/BasicNPCEntity.gd.uid
Normal file
1
scripts/Entities/BasicNPCEntity.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://d23qg1ovkbxst
|
176
scripts/Entities/OverworldEntity.gd
Normal file
176
scripts/Entities/OverworldEntity.gd
Normal file
@@ -0,0 +1,176 @@
|
||||
class_name OverworldEntity extends CharacterBody3D
|
||||
|
||||
const PauseSystem = preload("res://scripts/System/PauseSystem.gd")
|
||||
|
||||
enum Direction {
|
||||
NORTH,
|
||||
EAST,
|
||||
SOUTH,
|
||||
WEST,
|
||||
}
|
||||
|
||||
var speed:float = 150;
|
||||
var friction:float = 8.5;
|
||||
var gravity:float = 30;
|
||||
|
||||
@export var direction = Direction.SOUTH:
|
||||
set(newDirection):
|
||||
direction = newDirection;
|
||||
_updateMaterial();
|
||||
|
||||
var meshInstance:MeshInstance3D;
|
||||
var underFootTile:int = -1;
|
||||
var underFootPosition:Vector3;
|
||||
|
||||
var withinMapBounds:MapBounds;
|
||||
var withinBoundsLastFrame:bool = true;
|
||||
|
||||
func _updateMaterial():
|
||||
if !meshInstance:
|
||||
return
|
||||
var material:ShaderMaterial = meshInstance.get_surface_override_material(0)
|
||||
if !material:
|
||||
return
|
||||
material.set_shader_parameter("direction", direction)
|
||||
|
||||
func getSystems() -> Systems:
|
||||
return get_tree().current_scene.get_node("Systems") as Systems;
|
||||
|
||||
func getDirectionVector() -> Vector3:
|
||||
match direction:
|
||||
Direction.NORTH:
|
||||
return Vector3(0, 0, -1);
|
||||
Direction.SOUTH:
|
||||
return Vector3(0, 0, 1);
|
||||
Direction.WEST:
|
||||
return Vector3(-1, 0, 0);
|
||||
Direction.EAST:
|
||||
return Vector3(1, 0, 0);
|
||||
return Vector3(0, 0, 0);
|
||||
|
||||
func getDirectionToFace(position:Vector3) -> Direction:
|
||||
var diff = position - self.position;
|
||||
if abs(diff.x) > abs(diff.z):
|
||||
if diff.x > 0:
|
||||
return Direction.EAST;
|
||||
else:
|
||||
return Direction.WEST;
|
||||
else:
|
||||
if diff.z > 0:
|
||||
return Direction.SOUTH;
|
||||
else:
|
||||
return Direction.NORTH;
|
||||
return Direction.SOUTH;
|
||||
|
||||
# Virtual Methods
|
||||
func updateMovement(delta) -> void:
|
||||
pass
|
||||
|
||||
func updateOverworldLogic(delta) -> void:
|
||||
pass
|
||||
|
||||
func isPaused() -> bool:
|
||||
var pause = getSystems().PAUSE;
|
||||
var ps = pause.getPauseState();
|
||||
|
||||
if ps == PauseSystem.PauseType.NOT_PAUSED:
|
||||
return false;
|
||||
elif ps == PauseSystem.PauseType.FULLY_PAUSED:
|
||||
return true;
|
||||
elif ps == PauseSystem.PauseType.ENTITY_PAUSED:
|
||||
if pause.entities.find(self) != -1:
|
||||
return true;
|
||||
return false
|
||||
elif ps == PauseSystem.PauseType.CUTSCENE_PAUSED:
|
||||
if pause.entities.find(self) != -1:
|
||||
return false;
|
||||
return true;
|
||||
return false;
|
||||
|
||||
# Private methods
|
||||
func _updateTileData() -> void:
|
||||
# ray cast down
|
||||
var offset = Vector3(0, 0, 0.426);
|
||||
var query = PhysicsRayQueryParameters3D.create(
|
||||
position + offset,
|
||||
position + Vector3(0, -1, 0) + offset
|
||||
)
|
||||
query.collide_with_areas = true
|
||||
query.exclude = [self]
|
||||
|
||||
var result = get_world_3d().direct_space_state.intersect_ray(query)
|
||||
if !result or !result.collider:
|
||||
return;
|
||||
|
||||
var collider = result.collider;
|
||||
var colliderMesh = collider.get_node("../");
|
||||
if !colliderMesh or !(colliderMesh is ArrayMesh) or !colliderMesh.mesh or colliderMesh.mesh.get_surface_count() == 0:
|
||||
return;
|
||||
|
||||
# Get the face index (triangle)
|
||||
var arrays = colliderMesh.mesh.surface_get_arrays(0);
|
||||
var indiceIdx = result.face_index * 3;
|
||||
|
||||
# Get each indice of the triangle
|
||||
var index0 = arrays[Mesh.ARRAY_INDEX][indiceIdx+0];
|
||||
var index1 = arrays[Mesh.ARRAY_INDEX][indiceIdx+1];
|
||||
var index2 = arrays[Mesh.ARRAY_INDEX][indiceIdx+2];
|
||||
|
||||
# Get each uv of each indice
|
||||
var uv0:Vector2 = arrays[Mesh.ARRAY_TEX_UV][index0];
|
||||
var uv1:Vector2 = arrays[Mesh.ARRAY_TEX_UV][index1];
|
||||
var uv2:Vector2 = arrays[Mesh.ARRAY_TEX_UV][index2];
|
||||
|
||||
# Determine the lowest texture coordinate
|
||||
var min = Vector2(min(uv0.x, uv1.x, uv2.x), min(uv0.y, uv1.y, uv2.y));
|
||||
|
||||
# Convert to column/row
|
||||
var w = 256;
|
||||
var h = w;
|
||||
var tw = 48;
|
||||
var th = tw;
|
||||
var column = int(roundf(min.x * w)) / tw;
|
||||
var row = int(roundf(min.y * h)) / th;
|
||||
var columns = 768 / tw;
|
||||
underFootPosition = result.position;
|
||||
underFootTile = column % columns + row * columns;
|
||||
|
||||
# Events
|
||||
func _ready() -> void:
|
||||
meshInstance = get_node("MeshInstance3D")
|
||||
_updateTileData();
|
||||
_updateMaterial();
|
||||
pass
|
||||
|
||||
func _process(delta:float) -> void:
|
||||
if isPaused():
|
||||
return;
|
||||
|
||||
# Handle entity leaving map bounds
|
||||
if !withinMapBounds:
|
||||
if !withinBoundsLastFrame:
|
||||
print("Entity ", self.name, " was out of map bounds for two frames");
|
||||
withinBoundsLastFrame = false;
|
||||
else:
|
||||
withinBoundsLastFrame = true;
|
||||
|
||||
# Update logic
|
||||
updateOverworldLogic(delta)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if isPaused():
|
||||
return;
|
||||
|
||||
# Update movement
|
||||
updateMovement(delta);
|
||||
|
||||
# Gravity and friction
|
||||
if !is_on_floor():
|
||||
velocity.y -= gravity * delta;
|
||||
else:
|
||||
velocity += -(velocity * friction * delta);
|
||||
if velocity.length() != 0:
|
||||
_updateTileData();
|
||||
|
||||
# Update character controller.
|
||||
move_and_slide();
|
1
scripts/Entities/OverworldEntity.gd.uid
Normal file
1
scripts/Entities/OverworldEntity.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bpwbaptqu4bm5
|
24
scripts/Entities/RosaCamera.gd
Normal file
24
scripts/Entities/RosaCamera.gd
Normal file
@@ -0,0 +1,24 @@
|
||||
extends Camera3D
|
||||
|
||||
const PIXEL_SCALE:float = 4.0;
|
||||
const WORLD_UNITS:float = 32.0;
|
||||
|
||||
func _ready() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
var z:float = (
|
||||
tan((deg_to_rad(180) - deg_to_rad(fov)) / 2.0) *
|
||||
(get_viewport().size.y / 2.0)
|
||||
) / PIXEL_SCALE / WORLD_UNITS;
|
||||
|
||||
var rosa = get_node("..");
|
||||
var look = rosa.position;
|
||||
var position = Vector3(0, 0, 2) + look;
|
||||
look_at_from_position(
|
||||
Vector3(position.x, position.y + z, position.z),
|
||||
look
|
||||
);
|
||||
|
||||
pass
|
1
scripts/Entities/RosaCamera.gd.uid
Normal file
1
scripts/Entities/RosaCamera.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://jd50n00bo05y
|
51
scripts/Entities/RosaController.gd
Normal file
51
scripts/Entities/RosaController.gd
Normal file
@@ -0,0 +1,51 @@
|
||||
class_name RosaController extends "res://scripts/Entities/OverworldEntity.gd"
|
||||
|
||||
var interactRange = 0.7;
|
||||
|
||||
func updateOverworldLogic(delta) -> void:
|
||||
# Check if interact button is pressed
|
||||
if(Input.is_action_just_pressed("interact")):
|
||||
var rayDirection = getDirectionVector();
|
||||
# cast ray
|
||||
|
||||
var query = PhysicsRayQueryParameters3D.create(
|
||||
position,
|
||||
position + (rayDirection * interactRange)
|
||||
)
|
||||
query.collide_with_areas = true
|
||||
query.exclude = [self]
|
||||
|
||||
var result = get_world_3d().direct_space_state.intersect_ray(query)
|
||||
if result and result.collider:
|
||||
var collider = result.collider
|
||||
if(collider.has_method("interact")):
|
||||
collider.interact(self)
|
||||
|
||||
if Input.is_action_just_pressed("pause"):
|
||||
getSystems().PAUSE.playerPauseToggle();
|
||||
|
||||
func updateMovement(delta) -> void:
|
||||
# User movement
|
||||
var dir:Vector2 = Input.get_vector("left", "right", "up", "down");
|
||||
if(dir.x != 0 or dir.y != 0):
|
||||
velocity.x = dir.x * speed * delta;
|
||||
velocity.z = dir.y * speed * delta;
|
||||
|
||||
# Update direction
|
||||
if(dir.x >= abs(dir.y) and(
|
||||
dir.y == 0 or
|
||||
(dir.y > 0 and direction != Direction.SOUTH) or
|
||||
(dir.y < 0 and direction != Direction.NORTH)
|
||||
)):
|
||||
direction = Direction.EAST;
|
||||
elif (dir.x <= -abs(dir.y) and (
|
||||
dir.y == 0 or
|
||||
(dir.y > 0 and direction != Direction.SOUTH) or
|
||||
(dir.y < 0 and direction != Direction.NORTH)
|
||||
)):
|
||||
direction = Direction.WEST;
|
||||
elif (dir.y > 0):
|
||||
direction = Direction.SOUTH;
|
||||
elif (dir.y < 0):
|
||||
direction = Direction.NORTH;
|
||||
pass
|
1
scripts/Entities/RosaController.gd.uid
Normal file
1
scripts/Entities/RosaController.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ixwikdguyhf0
|
Reference in New Issue
Block a user