Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Squash the Creeps 3D C# demo for Godot mono 4.2 #1001

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mono/squash_the_creeps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.import
50 changes: 50 additions & 0 deletions mono/squash_the_creeps/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Godot;

public partial class Main : Node
{
[Export]
public PackedScene MobScene { get; set; }

public override void _Ready()
{
GetNode<Control>("UserInterface/Retry").Hide();
}

// We also specified this function name in PascalCase in the editor's connection window
private void OnMobTimerTimeout()
{
// Create a new instance of the Mob scene.
Mob mob = MobScene.Instantiate<Mob>();

// Choose a random location on the SpawnPath.
// We store the reference to the SpawnLocation node.
PathFollow3D mobSpawnLocation = GetNode<PathFollow3D>("SpawnPath/SpawnLocation");
// And give it a random offset.
mobSpawnLocation.ProgressRatio = GD.Randf();

Vector3 playerPosition = GetNode<Player>("Player").Position;
mob.Initialize(mobSpawnLocation.Position, playerPosition);

// Spawn the mob by adding it to the Main scene.
AddChild(mob);

// We connect the mob to the score label to update the score upon squashing one.
mob.Squashed += GetNode<ScoreLabel>("UserInterface/ScoreLabel").OnMobSquashed;
}

// We also specified this function name in PascalCase in the editor's connection window
private void OnPlayerHit()
{
GetNode<Timer>("MobTimer").Stop();
GetNode<Control>("UserInterface/Retry").Show();
}

public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed("ui_accept") && GetNode<Control>("UserInterface/Retry").Visible)
{
// This restarts the current scene.
GetTree().ReloadCurrentScene();
}
}
}
155 changes: 155 additions & 0 deletions mono/squash_the_creeps/Main.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
[gd_scene load_steps=15 format=3 uid="uid://bbwavbqpv0ge7"]

[ext_resource type="Script" path="res://Main.cs" id="1_ecxn0"]
[ext_resource type="PackedScene" uid="uid://bdqntdek3u7aw" path="res://Mob.tscn" id="2_i3afp"]
[ext_resource type="PackedScene" uid="uid://bnorrme8hfbsx" path="res://Player.tscn" id="3_g20lh"]
[ext_resource type="FontFile" uid="uid://755mnwjmjuro" path="res://fonts/Montserrat-Medium.ttf" id="4_c7ism"]
[ext_resource type="Script" path="res://ScoreLabel.cs" id="5_61u28"]

[sub_resource type="BoxShape3D" id="BoxShape3D_fo0kv"]
size = Vector3(60, 2, 60)

[sub_resource type="BoxMesh" id="BoxMesh_vxe8p"]
size = Vector3(60, 2, 60)

[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_2qp7p"]
sky_horizon_color = Color(0.647059, 0.654902, 0.670588, 1)

[sub_resource type="Sky" id="Sky_j5vrg"]
sky_material = SubResource("ProceduralSkyMaterial_2qp7p")

[sub_resource type="Environment" id="Environment_t6dwr"]
background_mode = 2
sky = SubResource("Sky_j5vrg")
tonemap_mode = 2

[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_po4ef"]
albedo_color = Color(0.97, 0.549667, 0, 1)

[sub_resource type="CylinderMesh" id="CylinderMesh_hgo3b"]
material = SubResource("StandardMaterial3D_po4ef")

[sub_resource type="Curve3D" id="Curve3D_kyctn"]
_data = {
"points": PackedVector3Array(0, 0, 0, 0, 0, 0, -16.5959, 0, -17.8175, 0, 0, 0, 0, 0, 0, 16.5117, 0, -17.8175, 0, 0, 0, 0, 0, 0, 16.5959, 0, 18.1544, 0, 0, 0, 0, 0, 0, -16.7644, 0, 18.1544, 0, 0, 0, 0, 0, 0, -16.5959, 0, -17.8175),
"tilts": PackedFloat32Array(0, 0, 0, 0, 0)
}
point_count = 5

[sub_resource type="Theme" id="Theme_uubu5"]
default_font = ExtResource("4_c7ism")
default_font_size = 22

[node name="Main" type="Node"]
script = ExtResource("1_ecxn0")
MobScene = ExtResource("2_i3afp")

[node name="Ground" type="StaticBody3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0)
collision_layer = 4
collision_mask = 0

[node name="CollisionShape3D" type="CollisionShape3D" parent="Ground"]
shape = SubResource("BoxShape3D_fo0kv")

[node name="MeshInstance3D" type="MeshInstance3D" parent="Ground"]
mesh = SubResource("BoxMesh_vxe8p")

[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(0.5, -0.777049, 0.382355, 0, 0.441506, 0.897258, -0.866025, -0.448629, 0.220753, 0, 12.5592, 14.7757)
shadow_enabled = true
shadow_bias = 0.04
shadow_blur = 1.5
directional_shadow_mode = 0
directional_shadow_fade_start = 1.0
directional_shadow_max_distance = 40.0

[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_t6dwr")

[node name="Player" parent="." instance=ExtResource("3_g20lh")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.66792, 0)

[node name="CameraPivot" type="Marker3D" parent="."]
transform = Transform3D(1, -5.95416e-16, 2.4663e-16, 2.4663e-16, 0.707107, 0.707107, -5.95416e-16, -0.707107, 0.707107, 0, 0, 0)

[node name="Camera3D" type="Camera3D" parent="CameraPivot"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.08165e-12, 2.08165e-12, 19)
projection = 1
current = true
size = 24.0
far = 40.0

[node name="Cylinders" type="Node3D" parent="."]

[node name="MeshInstance3D" type="MeshInstance3D" parent="Cylinders"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -16.6028, 0, -17.8799)
mesh = SubResource("CylinderMesh_hgo3b")

[node name="MeshInstance3D2" type="MeshInstance3D" parent="Cylinders"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 16.5115, 0, -17.7887)
mesh = SubResource("CylinderMesh_hgo3b")

[node name="MeshInstance3D3" type="MeshInstance3D" parent="Cylinders"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 16.5115, 0, 18.2448)
mesh = SubResource("CylinderMesh_hgo3b")

[node name="MeshInstance3D4" type="MeshInstance3D" parent="Cylinders"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -16.7074, 0, 18.077)
mesh = SubResource("CylinderMesh_hgo3b")

[node name="SpawnPath" type="Path3D" parent="."]
curve = SubResource("Curve3D_kyctn")

[node name="SpawnLocation" type="PathFollow3D" parent="SpawnPath"]
transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, -16.5959, 0, -17.8175)

[node name="MobTimer" type="Timer" parent="."]
wait_time = 0.5
autostart = true

[node name="UserInterface" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme = SubResource("Theme_uubu5")

[node name="ScoreLabel" type="Label" parent="UserInterface"]
layout_mode = 0
offset_left = 16.0
offset_top = 16.0
offset_right = 144.0
offset_bottom = 56.0
theme_override_colors/font_color = Color(0, 0, 0, 1)
text = "Score: 0"
script = ExtResource("5_61u28")

[node name="Retry" type="ColorRect" parent="UserInterface"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0, 0, 0, 0.376471)

[node name="Label" type="Label" parent="UserInterface/Retry"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -106.5
offset_top = -14.0
offset_right = 106.5
offset_bottom = 14.0
grow_horizontal = 2
grow_vertical = 2
text = "Press Enter to retry"

[connection signal="Hit" from="Player" to="." method="OnPlayerHit"]
[connection signal="timeout" from="MobTimer" to="." method="OnMobTimerTimeout"]
52 changes: 52 additions & 0 deletions mono/squash_the_creeps/Mob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Godot;

public partial class Mob : CharacterBody3D
{
// Emitted when the player jumped on the mob.
[Signal]
public delegate void SquashedEventHandler();
// Minimum speed of the mob in meters per second
[Export]
public int MinSpeed { get; set; } = 10;
// Maximum speed of the mob in meters per second
[Export]
public int MaxSpeed { get; set; } = 18;

public override void _PhysicsProcess(double delta)
{
MoveAndSlide();
}

// This function will be called from the Main scene.
public void Initialize(Vector3 startPosition, Vector3 playerPosition)
{
// We position the mob by placing it at startPosition
// and rotate it towards playerPosition, so it looks at the player.
LookAtFromPosition(startPosition, playerPosition, Vector3.Up);
// Rotate this mob randomly within range of -45 and +45 degrees,
// so that it doesn't move directly towards the player.
RotateY((float)GD.RandRange(-Mathf.Pi / 4.0, Mathf.Pi / 4.0));

// We calculate a random speed (integer).
int randomSpeed = GD.RandRange(MinSpeed, MaxSpeed);
// We calculate a forward velocity that represents the speed.
Velocity = Vector3.Forward * randomSpeed;
// We then rotate the velocity vector based on the mob's Y rotation
// in order to move in the direction the mob is looking.
Velocity = Velocity.Rotated(Vector3.Up, Rotation.Y);

GetNode<AnimationPlayer>("AnimationPlayer").SpeedScale = randomSpeed / MinSpeed;
}

// We also specified this function name in PascalCase in the editor's connection window
private void OnVisibleOnScreenNotifierScreenExited()
{
QueueFree();
}

public void Squash()
{
EmitSignal(SignalName.Squashed);
QueueFree();
}
}
68 changes: 68 additions & 0 deletions mono/squash_the_creeps/Mob.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[gd_scene load_steps=6 format=3 uid="uid://bdqntdek3u7aw"]

[ext_resource type="Script" path="res://Mob.cs" id="1_g64pt"]
[ext_resource type="PackedScene" uid="uid://b1kcqfya734dq" path="res://art/mob.glb" id="2_e23op"]

[sub_resource type="BoxShape3D" id="BoxShape3D_svwdt"]
size = Vector3(1.358, 1.088, 2.201)

[sub_resource type="Animation" id="Animation_t3png"]
resource_name = "float"
length = 1.2
loop_mode = 1
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Pivot/Character:position")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0.3, 0.7, 1.2),
"transitions": PackedFloat32Array(0.420448, 2.07053, 1),
"update": 0,
"values": [Vector3(2.08165e-12, 0.33, 2.08165e-12), Vector3(2.08165e-12, 0.65, 2.08165e-12), Vector3(2.08165e-12, 0.35, 2.08165e-12)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Pivot/Character:rotation")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0.1, 0.5, 1.2),
"transitions": PackedFloat32Array(0.420448, 0.450625, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(0.139626, 3.48787e-16, 3.48787e-16), Vector3(-0.15708, 3.48787e-16, 3.48787e-16)]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_sw56c"]
_data = {
"float": SubResource("Animation_t3png")
}

[node name="Mob" type="CharacterBody3D" groups=["mob"]]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.08165e-12, 0.011, 2.08165e-12)
collision_layer = 2
collision_mask = 0
script = ExtResource("1_g64pt")

[node name="Pivot" type="Node3D" parent="."]

[node name="Character" parent="Pivot" instance=ExtResource("2_e23op")]
transform = Transform3D(1, -4.03349e-16, 3.44493e-16, 3.44493e-16, 0.987688, 0.156434, -4.03349e-16, -0.156434, 0.987688, 2.08165e-12, 0.35, 2.08165e-12)

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.08165e-12, 0.528, 2.08165e-12)
shape = SubResource("BoxShape3D_svwdt")

[node name="VisibleOnScreenNotifier3D" type="VisibleOnScreenNotifier3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.08165e-12, 0.062, 0.332)
aabb = AABB(-1.2, 0.251, -1.571, 2.41, 1.093, 3.172)

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_sw56c")
}
autoplay = "float"

[connection signal="screen_exited" from="VisibleOnScreenNotifier3D" to="." method="OnVisibleOnScreenNotifierScreenExited"]
7 changes: 7 additions & 0 deletions mono/squash_the_creeps/MusicPlayer.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://ml76m4c5tsyn"]

[ext_resource type="AudioStream" uid="uid://fx12pc5y5syy" path="res://art/House In a Forest Loop.ogg" id="1_x2nla"]

[node name="MusicPlayer" type="AudioStreamPlayer"]
stream = ExtResource("1_x2nla")
autoplay = true
Loading