Add inventory system
This commit is contained in:
128
scripts/Entities/OverworldEntity.gd
Normal file
128
scripts/Entities/OverworldEntity.gd
Normal file
@@ -0,0 +1,128 @@
|
||||
extends CharacterBody3D
|
||||
class_name OverworldEntity
|
||||
|
||||
enum Direction {
|
||||
NORTH,
|
||||
SOUTH,
|
||||
WEST,
|
||||
EAST
|
||||
}
|
||||
|
||||
var speed:float = 150;
|
||||
var friction:float = 8.5;
|
||||
var gravity:float = 30;
|
||||
|
||||
var direction = Direction.SOUTH;
|
||||
var meshInstance:MeshInstance3D;
|
||||
var underFootTile:int = -1;
|
||||
var underFootPosition:Vector3;
|
||||
|
||||
var withinMapBounds:MapBounds;
|
||||
var withinBoundsLastFrame:bool = true;
|
||||
|
||||
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);
|
||||
|
||||
# Virtual Methods
|
||||
func updateMovement(delta) -> void:
|
||||
pass
|
||||
|
||||
func updateOverworldLogic(delta) -> void:
|
||||
pass
|
||||
|
||||
# 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 = 768;
|
||||
var h = w;
|
||||
var tw = 48;
|
||||
var th = tw;
|
||||
var column = int(roundf(min.x * w)) / 48;
|
||||
var row = int(roundf(min.y * h)) / 48;
|
||||
var columns = 768 / 48;
|
||||
underFootPosition = result.position;
|
||||
underFootTile = column % columns + row * columns;
|
||||
|
||||
# Events
|
||||
func _ready() -> void:
|
||||
meshInstance = get_node("MeshInstance3D")
|
||||
_updateTileData();
|
||||
pass
|
||||
|
||||
func _process(delta:float) -> void:
|
||||
# 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)
|
||||
|
||||
# Set shader direction.
|
||||
var material:ShaderMaterial = meshInstance.get_surface_override_material(0)
|
||||
material.set_shader_parameter("direction", direction)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# 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();
|
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, 4) + look;
|
||||
look_at_from_position(
|
||||
Vector3(position.x, position.y + z, position.z),
|
||||
look
|
||||
);
|
||||
|
||||
pass
|
48
scripts/Entities/RosaController.gd
Normal file
48
scripts/Entities/RosaController.gd
Normal file
@@ -0,0 +1,48 @@
|
||||
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)
|
||||
|
||||
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
|
11
scripts/Entities/TestNPCController.gd
Normal file
11
scripts/Entities/TestNPCController.gd
Normal file
@@ -0,0 +1,11 @@
|
||||
class_name TestNPCController extends "res://scripts/Entities/OverworldEntity.gd"
|
||||
|
||||
func interact(interactor) -> void:
|
||||
var systems = getSystems();
|
||||
systems.ITEM.addItem(systems.ITEM.ITEM_POTION, 1);
|
||||
#var itemSystem = (get_node("Systems") as Systems).ITEM;
|
||||
#itemSystem.addItem(itemSystem.ITEM_POTION, 1);
|
||||
#pass
|
||||
|
||||
func updateMovement(delta) -> void:
|
||||
pass
|
Reference in New Issue
Block a user