Skip to content

Commit

Permalink
feat(game): Scrolling clouds in the background (#110)
Browse files Browse the repository at this point in the history
Closes #108
  • Loading branch information
elementbound authored Nov 13, 2023
1 parent 4614d21 commit 35300c9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
26 changes: 23 additions & 3 deletions examples/forest-brawl/maps/three-peaks.tscn
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[gd_scene load_steps=9 format=3 uid="uid://d1544gxqaoptc"]
[gd_scene load_steps=11 format=3 uid="uid://d1544gxqaoptc"]

[ext_resource type="MeshLibrary" uid="uid://do0y7f0sy12jg" path="res://examples/forest-brawl/models/forest-blocks.meshlib" id="1_124j6"]
[ext_resource type="PackedScene" uid="uid://ijqx6kjl3qf4" path="res://examples/forest-brawl/scenes/powerup.tscn" id="2_4e2nk"]
[ext_resource type="PackedScene" uid="uid://c12hb5btenkcy" path="res://examples/forest-brawl/scenes/doodads/clouds-1.tscn" id="3_ucvrj"]
[ext_resource type="PackedScene" uid="uid://c5l8x303sjqhs" path="res://examples/forest-brawl/scenes/doodads/clouds-2.tscn" id="4_dfl7h"]
[ext_resource type="PackedScene" uid="uid://dq14vitb32x5p" path="res://examples/forest-brawl/scenes/doodads/clouds-3.tscn" id="5_doyli"]
[ext_resource type="Script" path="res://examples/forest-brawl/scripts/cloud-area3d.gd" id="6_ymimw"]

[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_xjsj7"]
sky_top_color = Color(0.384314, 0.454902, 0.54902, 1)
ground_bottom_color = Color(0.3136, 0.420373, 0.56, 1)

[sub_resource type="Sky" id="Sky_g0uhm"]
sky_material = SubResource("ProceduralSkyMaterial_xjsj7")
Expand All @@ -16,6 +19,9 @@ background_mode = 2
sky = SubResource("Sky_g0uhm")
tonemap_mode = 2

[sub_resource type="BoxShape3D" id="BoxShape3D_4u8tk"]
size = Vector3(240, 48, 120)

[node name="Three Peaks Map" type="Node3D"]

[node name="GridMap" type="GridMap" parent="."]
Expand All @@ -28,6 +34,9 @@ data = {
}
metadata/_editor_floor_ = Vector3(0, 0, 0)

[node name="Mountain Clouds" parent="." instance=ExtResource("3_ucvrj")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 17.8236, -4.03231, 11.082)

[node name="Powerups" type="Node" parent="."]

[node name="Powerup" parent="Powerups" instance=ExtResource("2_4e2nk")]
Expand Down Expand Up @@ -73,6 +82,7 @@ environment = SubResource("Environment_c7ydc")

[node name="Clouds" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -20, 0)
visible = false

[node name="Clouds 1" parent="Clouds" instance=ExtResource("3_ucvrj")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -20.6817, 0, -3.66238)
Expand Down Expand Up @@ -122,5 +132,15 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 79.7221, 0, 4.68955)
[node name="Clouds 16" parent="Clouds" instance=ExtResource("5_doyli")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -71.8085, 0, 21.5426)

[node name="Clouds 17" parent="Clouds" instance=ExtResource("3_ucvrj")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 17.8236, 15.8221, 11.082)
[node name="Area3D" type="Area3D" parent="."]
monitoring = false
monitorable = false
script = ExtResource("6_ymimw")
clouds = Array[PackedScene]([ExtResource("3_ucvrj"), ExtResource("4_dfl7h"), ExtResource("5_doyli")])
count = 24
float_speed_min = 0.25
float_speed_max = 2.0

[node name="CollisionShape3D" type="CollisionShape3D" parent="Area3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -34, 0)
shape = SubResource("BoxShape3D_4u8tk")
Binary file modified examples/forest-brawl/models/forest-blocks.meshlib
Binary file not shown.
65 changes: 65 additions & 0 deletions examples/forest-brawl/scripts/cloud-area3d.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
extends Area3D

@export var clouds: Array[PackedScene] = []
@export var count: int = 16

@export var float_direction: Vector3 = Vector3.RIGHT
@export var float_speed_min: float = 4.0
@export var float_speed_max: float = 8.0

var _clouds: Array[Node3D] = []
var _speeds: Array[float] = []
var _aabb: AABB

func _ready():
_aabb = _find_aabb()
if not _aabb.has_volume():
push_error("CloudArea required a box shape!")
queue_free()
return

for i in range(count):
var cloud = _spawn_cloud()
cloud.position = _aabb.position + _aabb.size * Vector3(randf(), randf(), randf())

_clouds.push_back(cloud)
_speeds.push_back(randf_range(float_speed_min, float_speed_max))

func _process(delta):
for i in range(count):
var cloud = _clouds[i]
var speed = _speeds[i]

cloud.position += float_direction * speed * delta

if not _aabb.has_point(cloud.position):
cloud.queue_free()

cloud = _spawn_cloud()
cloud.position = _aabb.position + _aabb.size * \
Vector3(randf(), randf(), randf()) * \
(Vector3.ONE * 0.5 - float_direction * 0.5)

_clouds[i] = cloud
_speeds[i] = randf_range(float_speed_min, float_speed_max)

func _spawn_cloud() -> Node3D:
var cloud_template = clouds.pick_random() as PackedScene
var cloud = cloud_template.instantiate() as Node3D
add_child(cloud)
cloud.owner = self

return cloud

func _find_aabb() -> AABB:
var shape_owners = get_shape_owners()
for shape_owner in shape_owners:
for i in range(shape_owner_get_shape_count(shape_owner)):
var shape = shape_owner_get_shape(shape_owner, i)

if shape is BoxShape3D:
var pos = shape_owner_get_transform(shape_owner).origin
var size = shape.size
return AABB(pos - size / 2, size)

return AABB(Vector3.ZERO, Vector3.ZERO)

0 comments on commit 35300c9

Please sign in to comment.