First commit
This commit is contained in:
Executable
+4
@@ -0,0 +1,4 @@
|
||||
extends Resource
|
||||
class_name assetArray
|
||||
|
||||
@export var structures: Array[Structure] = []
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
uid://bewsnajw4pm8g
|
||||
Executable
+42
@@ -0,0 +1,42 @@
|
||||
extends Node
|
||||
|
||||
# Code adapted from KidsCanCode
|
||||
|
||||
var num_players = 12
|
||||
var bus = "master"
|
||||
|
||||
var available = [] # The available players.
|
||||
var queue = [] # The queue of {path, volume} dictionaries.
|
||||
|
||||
func _ready():
|
||||
for i in num_players:
|
||||
var p = AudioStreamPlayer.new()
|
||||
add_child(p)
|
||||
|
||||
available.append(p)
|
||||
|
||||
p.volume_db = -10
|
||||
p.finished.connect(_on_stream_finished.bind(p))
|
||||
p.bus = bus
|
||||
|
||||
func _on_stream_finished(stream):
|
||||
available.append(stream)
|
||||
|
||||
func play(sound_path: String, volume_db: float = -10.0):
|
||||
# Path (or multiple, separated by commas)
|
||||
var sounds = sound_path.split(",")
|
||||
var chosen = "res://" + sounds[randi() % sounds.size()].strip_edges()
|
||||
queue.append({
|
||||
"path": chosen,
|
||||
"volume": volume_db
|
||||
})
|
||||
|
||||
func _process(_delta):
|
||||
if not queue.is_empty() and not available.is_empty():
|
||||
var item = queue.pop_front()
|
||||
var player = available.pop_front()
|
||||
|
||||
player.stream = load(item["path"])
|
||||
player.volume_db = item["volume"]
|
||||
player.pitch_scale = randf_range(0.9, 1.1)
|
||||
player.play()
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
uid://b6g5iasbsyq1x
|
||||
Executable
+185
@@ -0,0 +1,185 @@
|
||||
extends Node3D
|
||||
|
||||
#@export var structures: Array[Structure] = []
|
||||
var ASSET_RESSOURCES = load("res://structuresAssets/asset-ressources.tres")
|
||||
var structures = ASSET_RESSOURCES.structures
|
||||
|
||||
var map:DataMap
|
||||
var map2:DataMap
|
||||
|
||||
var index:int = 0 # Index of structure being built
|
||||
|
||||
@export var selector:Node3D # The 'cursor'
|
||||
@export var selector_container:Node3D # Node that holds a preview of the structure
|
||||
@export var view_camera:Camera3D # Used for raycasting mouse
|
||||
@export var gridmap:GridMap
|
||||
@export var cash_display:Label
|
||||
|
||||
var plane:Plane # Used for raycasting mouse
|
||||
|
||||
func _ready():
|
||||
|
||||
map = DataMap.new()
|
||||
plane = Plane(Vector3.UP, Vector3.ZERO)
|
||||
|
||||
# Create new MeshLibrary dynamically, can also be done in the editor
|
||||
# See: https://docs.godotengine.org/en/stable/tutorials/3d/using_gridmaps.html
|
||||
|
||||
var mesh_library = MeshLibrary.new()
|
||||
|
||||
for structure in structures:
|
||||
|
||||
var id = mesh_library.get_last_unused_item_id()
|
||||
|
||||
mesh_library.create_item(id)
|
||||
mesh_library.set_item_mesh(id, get_mesh(structure.model))
|
||||
mesh_library.set_item_mesh_transform(id, Transform3D())
|
||||
|
||||
gridmap.mesh_library = mesh_library
|
||||
|
||||
update_structure()
|
||||
update_cash()
|
||||
|
||||
func _process(delta):
|
||||
|
||||
# Controls
|
||||
|
||||
action_rotate() # Rotates selection 90 degrees
|
||||
action_structure_toggle() # Toggles between structures
|
||||
|
||||
action_save() # Saving
|
||||
action_load() # Loading
|
||||
action_load_resources() # Loading from resources
|
||||
|
||||
# Map position based on mouse
|
||||
|
||||
var world_position = plane.intersects_ray(
|
||||
view_camera.project_ray_origin(get_viewport().get_mouse_position()),
|
||||
view_camera.project_ray_normal(get_viewport().get_mouse_position()))
|
||||
|
||||
var gridmap_position = Vector3(round(world_position.x), 0, round(world_position.z))
|
||||
selector.position = lerp(selector.position, gridmap_position, min(delta * 40, 1.0))
|
||||
|
||||
action_build(gridmap_position)
|
||||
action_demolish(gridmap_position)
|
||||
|
||||
# Retrieve the mesh from a PackedScene, used for dynamically creating a MeshLibrary
|
||||
|
||||
func get_mesh(packed_scene):
|
||||
var scene_state:SceneState = packed_scene.get_state()
|
||||
for i in range(scene_state.get_node_count()):
|
||||
if(scene_state.get_node_type(i) == "MeshInstance3D"):
|
||||
for j in scene_state.get_node_property_count(i):
|
||||
var prop_name = scene_state.get_node_property_name(i, j)
|
||||
if prop_name == "mesh":
|
||||
var prop_value = scene_state.get_node_property_value(i, j)
|
||||
|
||||
return prop_value.duplicate()
|
||||
|
||||
# Build (place) a structure
|
||||
|
||||
func action_build(gridmap_position):
|
||||
if Input.is_action_just_pressed("build"):
|
||||
|
||||
var previous_tile = gridmap.get_cell_item(gridmap_position)
|
||||
gridmap.set_cell_item(gridmap_position, index, gridmap.get_orthogonal_index_from_basis(selector.basis))
|
||||
|
||||
if previous_tile != index:
|
||||
map.cash -= structures[index].price
|
||||
update_cash()
|
||||
|
||||
Audio.play("sounds/placement-a.ogg, sounds/placement-b.ogg, sounds/placement-c.ogg, sounds/placement-d.ogg", -20)
|
||||
|
||||
# Demolish (remove) a structure
|
||||
|
||||
func action_demolish(gridmap_position):
|
||||
if Input.is_action_just_pressed("demolish"):
|
||||
if gridmap.get_cell_item(gridmap_position) != -1:
|
||||
gridmap.set_cell_item(gridmap_position, -1)
|
||||
|
||||
Audio.play("sounds/removal-a.ogg, sounds/removal-b.ogg, sounds/removal-c.ogg, sounds/removal-d.ogg", -20)
|
||||
|
||||
# Rotates the 'cursor' 90 degrees
|
||||
|
||||
func action_rotate():
|
||||
if Input.is_action_just_pressed("rotate"):
|
||||
selector.rotate_y(deg_to_rad(90))
|
||||
|
||||
Audio.play("sounds/rotate.ogg", -30)
|
||||
|
||||
# Toggle between structures to build
|
||||
|
||||
func action_structure_toggle():
|
||||
if Input.is_action_just_pressed("structure_next"):
|
||||
index = wrap(index + 1, 0, structures.size())
|
||||
Audio.play("sounds/toggle.ogg", -30)
|
||||
|
||||
if Input.is_action_just_pressed("structure_previous"):
|
||||
index = wrap(index - 1, 0, structures.size())
|
||||
Audio.play("sounds/toggle.ogg", -30)
|
||||
|
||||
update_structure()
|
||||
|
||||
# Update the structure visual in the 'cursor'
|
||||
|
||||
func update_structure():
|
||||
# Clear previous structure preview in selector
|
||||
for n in selector_container.get_children():
|
||||
selector_container.remove_child(n)
|
||||
n.queue_free()
|
||||
|
||||
# Create new structure preview in selector
|
||||
var _model = structures[index].model.instantiate()
|
||||
selector_container.add_child(_model)
|
||||
_model.position.y += 0.25
|
||||
|
||||
func update_cash():
|
||||
cash_display.text = "$" + str(map.cash)
|
||||
|
||||
# Saving/load
|
||||
|
||||
func action_save():
|
||||
if Input.is_action_just_pressed("save"):
|
||||
print("Saving map...")
|
||||
|
||||
map.structures.clear()
|
||||
for cell in gridmap.get_used_cells():
|
||||
|
||||
var data_structure:DataStructure = DataStructure.new()
|
||||
|
||||
data_structure.position = Vector2i(cell.x, cell.z)
|
||||
data_structure.orientation = gridmap.get_cell_item_orientation(cell)
|
||||
data_structure.structure = gridmap.get_cell_item(cell)
|
||||
|
||||
map.structures.append(data_structure)
|
||||
|
||||
ResourceSaver.save(map, "user://map.res")
|
||||
|
||||
func action_load():
|
||||
if Input.is_action_just_pressed("load"):
|
||||
print("Loading map...")
|
||||
|
||||
gridmap.clear()
|
||||
|
||||
map = ResourceLoader.load("user://map.res")
|
||||
if not map:
|
||||
map = DataMap.new()
|
||||
for cell in map.structures:
|
||||
gridmap.set_cell_item(Vector3i(cell.position.x, 0, cell.position.y), cell.structure, cell.orientation)
|
||||
print(cell.position.y)
|
||||
|
||||
update_cash()
|
||||
|
||||
func action_load_resources():
|
||||
if Input.is_action_just_pressed("load_resources"):
|
||||
print("Loading map...")
|
||||
|
||||
gridmap.clear()
|
||||
|
||||
map = ResourceLoader.load("res://sample map/map.res")
|
||||
if not map:
|
||||
map = DataMap.new()
|
||||
for cell in map.structures:
|
||||
gridmap.set_cell_item(Vector3i(cell.position.x, 0, cell.position.y), cell.structure, cell.orientation)
|
||||
|
||||
update_cash()
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
uid://76g8ejmdibal
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
extends Resource
|
||||
class_name DataMap
|
||||
|
||||
@export var cash:int = 10000
|
||||
@export var structures:Array[DataStructure]
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
uid://dvrs714bu830u
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
extends Resource
|
||||
class_name DataStructure
|
||||
|
||||
@export var position:Vector2i
|
||||
@export var orientation:int
|
||||
@export var structure:int
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
uid://c77is823atr3
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
extends Resource
|
||||
class_name Structure
|
||||
@export_subgroup("Model")
|
||||
@export var model:PackedScene # Model of the structure
|
||||
|
||||
@export_subgroup("Gameplay")
|
||||
@export var price:int # Price of the structure when building
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
uid://b4i7dfy6u3e8w
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
extends Node3D
|
||||
|
||||
var camera_position:Vector3
|
||||
var camera_rotation:Vector3
|
||||
|
||||
var zoom:float = 30.0 # 30 = Standard zoom level, in meters
|
||||
|
||||
@onready var camera = $Camera
|
||||
@export var lerpValue = 8
|
||||
|
||||
func _ready():
|
||||
|
||||
camera_rotation = rotation_degrees # Initial rotation
|
||||
|
||||
pass
|
||||
|
||||
func _process(delta):
|
||||
|
||||
# Set position and rotation to targets
|
||||
|
||||
position = position.lerp(camera_position, delta * lerpValue)
|
||||
rotation_degrees = rotation_degrees.lerp(camera_rotation, delta * lerpValue)
|
||||
|
||||
# Smoothly update zoom
|
||||
|
||||
camera.position = camera.position.lerp(Vector3(0, 0, zoom), delta * lerpValue * 2)
|
||||
|
||||
handle_input(delta)
|
||||
|
||||
# Handle input
|
||||
|
||||
func handle_input(_delta):
|
||||
|
||||
# Rotation
|
||||
|
||||
var input := Vector3.ZERO
|
||||
|
||||
input.x = Input.get_axis("camera_left", "camera_right")
|
||||
input.z = Input.get_axis("camera_forward", "camera_back")
|
||||
|
||||
input = input.rotated(Vector3.UP, rotation.y).normalized()
|
||||
|
||||
camera_position += input / 4
|
||||
|
||||
# Zoom in/out
|
||||
|
||||
if Input.is_action_just_released("zoom_in"):
|
||||
zoom = max(15, zoom - 5) # 15 = Minimum zoom level, in meters
|
||||
|
||||
if Input.is_action_just_released("zoom_out"):
|
||||
zoom = min(80, zoom + 5) # 80 = Maximum zoom level, in meters
|
||||
|
||||
# Back to center
|
||||
|
||||
if Input.is_action_pressed("camera_center"):
|
||||
camera_position = Vector3()
|
||||
|
||||
func _input(event):
|
||||
|
||||
# Rotate camera using mouse (hold 'middle' mouse button)
|
||||
|
||||
if event is InputEventMouseMotion:
|
||||
if Input.is_action_pressed("camera_rotate"):
|
||||
camera_rotation += Vector3(0, -event.relative.x / 10, 0)
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
uid://dec71yx4xpg2c
|
||||
Reference in New Issue
Block a user