Browse Source

Creating item database and loading block data from these resources

pull/1/head
Ryan Reed 1 month ago
parent
commit
cb3bf3522d
13 changed files with 96 additions and 25 deletions
  1. +6
    -0
      data_structure/block.gd
  2. +1
    -0
      data_structure/block.gd.uid
  3. +19
    -0
      data_structure/db_items.gd
  4. +1
    -0
      data_structure/db_items.gd.uid
  5. +6
    -0
      data_structure/db_items.tscn
  6. +7
    -0
      data_structure/item.gd
  7. +1
    -0
      data_structure/item.gd.uid
  8. +1
    -1
      project.godot
  9. +10
    -0
      resources/blocks/001_dirt.tres
  10. +12
    -0
      resources/blocks/002_stone.tres
  11. +10
    -0
      root.tscn
  12. +18
    -16
      scenes/blocks/block.gd
  13. +4
    -8
      scenes/world/world.gd

+ 6
- 0
data_structure/block.gd View File

@ -0,0 +1,6 @@
class_name BlockResource
extends ItemResource
# TODO: Replace with "missing texture" resource
@export var material: StandardMaterial3D = preload("res://assets/materials/dirt.tres")

+ 1
- 0
data_structure/block.gd.uid View File

@ -0,0 +1 @@
uid://dwrmy4mx0mw18

+ 19
- 0
data_structure/db_items.gd View File

@ -0,0 +1,19 @@
class_name DBItems
extends Node
#region Singleton
static var ref: DBItems
func _init() -> void:
if not ref:
ref = self
else:
queue_free()
#endregion
@onready var data: Dictionary = {
"001": preload("res://resources/blocks/001_dirt.tres"),
"002": preload("res://resources/blocks/002_stone.tres"),
}

+ 1
- 0
data_structure/db_items.gd.uid View File

@ -0,0 +1 @@
uid://bm3oi0opccjif

+ 6
- 0
data_structure/db_items.tscn View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://c0epfh4sqjcjq"]
[ext_resource type="Script" uid="uid://bm3oi0opccjif" path="res://data_structure/db_items.gd" id="1_jp5j0"]
[node name="DBItems" type="Node"]
script = ExtResource("1_jp5j0")

+ 7
- 0
data_structure/item.gd View File

@ -0,0 +1,7 @@
class_name ItemResource
extends Resource
@export var id: String = "000"
@export var name: String = "Item Name"
@export var description: String = "Item Description"

+ 1
- 0
data_structure/item.gd.uid View File

@ -0,0 +1 @@
uid://bdx4q355l5ugl

+ 1
- 1
project.godot View File

@ -11,7 +11,7 @@ config_version=5
[application]
config/name="Skyblock"
run/main_scene="uid://mkfitwqnerku"
run/main_scene="uid://cgx0nawwjjj7g"
config/features=PackedStringArray("4.4", "Forward Plus")
config/icon="res://icon.svg"


+ 10
- 0
resources/blocks/001_dirt.tres View File

@ -0,0 +1,10 @@
[gd_resource type="Resource" script_class="BlockResource" load_steps=2 format=3 uid="uid://bu0yoee1rs1se"]
[ext_resource type="Script" uid="uid://dwrmy4mx0mw18" path="res://data_structure/block.gd" id="1_ljghb"]
[resource]
script = ExtResource("1_ljghb")
id = "001"
name = "Dirt"
description = "Block of dirt"
metadata/_custom_type_script = "uid://dwrmy4mx0mw18"

+ 12
- 0
resources/blocks/002_stone.tres View File

@ -0,0 +1,12 @@
[gd_resource type="Resource" script_class="BlockResource" load_steps=3 format=3 uid="uid://cpddnknmxpohc"]
[ext_resource type="Material" uid="uid://cx7m27qa4ds4s" path="res://assets/materials/stone.tres" id="1_6chm7"]
[ext_resource type="Script" uid="uid://dwrmy4mx0mw18" path="res://data_structure/block.gd" id="1_63t5s"]
[resource]
script = ExtResource("1_63t5s")
material = ExtResource("1_6chm7")
id = "002"
name = "Stone"
description = "Block of stone"
metadata/_custom_type_script = "uid://dwrmy4mx0mw18"

+ 10
- 0
root.tscn View File

@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=3 uid="uid://cgx0nawwjjj7g"]
[ext_resource type="PackedScene" uid="uid://c0epfh4sqjcjq" path="res://data_structure/db_items.tscn" id="1_pyidc"]
[ext_resource type="PackedScene" uid="uid://mkfitwqnerku" path="res://scenes/world/world.tscn" id="2_vvh5c"]
[node name="Root" type="Node"]
[node name="DBItems" parent="." instance=ExtResource("1_pyidc")]
[node name="World" parent="." instance=ExtResource("2_vvh5c")]

+ 18
- 16
scenes/blocks/block.gd View File

@ -2,29 +2,31 @@ class_name Block
extends StaticBody3D
enum Types {
DIRT,
STONE,
}
var materials_map: Dictionary[Types, StandardMaterial3D] = {
Types.DIRT: preload("res://assets/materials/dirt.tres"),
Types.STONE: preload("res://assets/materials/stone.tres"),
}
@export var material: StandardMaterial3D
@export var type: Types
@onready var block_faces: Node3D = $BlockFaces
var id: String
var resource: BlockResource
func _ready() -> void:
_apply_materials()
func apply_material() -> void:
material = materials_map[type]
func set_id(block_id: String) -> void:
id = block_id
_get_resource_data()
_apply_materials()
func _apply_materials() -> void:
if block_faces == null: return
for face: MeshInstance3D in block_faces.get_children():
face.set_surface_override_material(0, material)
face.set_surface_override_material(0, resource.material)
func _get_resource_data() -> void:
if not id:
printerr("Block ID was expected but was not received")
return
resource = DBItems.ref.data[id]

+ 4
- 8
scenes/world/world.gd View File

@ -8,23 +8,19 @@ const BLOCK_PREFAB: PackedScene = preload("res://scenes/blocks/block.tscn")
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
_initialize_ground()
#create_block(Block.Types.DIRT, Vector3.ZERO)
#create_block(Block.Types.STONE, Vector3.ONE)
func create_block(block_type: Block.Types, block_position: Vector3) -> void:
func create_block(id: String, block_position: Vector3) -> void:
var block: Block = BLOCK_PREFAB.instantiate()
block.position = block_position
block.name = "%s" % [block_position]
block.type = block_type
block.apply_material()
block.set_id(id)
add_child(block)
func _initialize_ground() -> void:
for x: int in range(-10, 11):
for z: int in range(-10, 11):
var random_type: int = randi_range(0 ,1)
var ground_position: Vector3 = Vector3(x, 0, z)
create_block(random_type, ground_position)
#create_block(Block.Types.STONE, ground_position)
create_block("001", ground_position)

Loading…
Cancel
Save