Code cleanup

pull/23/head
Sascha 2023-10-26 13:07:45 +07:00
parent 9aff213b2d
commit b41cc14695
15 changed files with 253 additions and 208 deletions

@ -1,6 +1,6 @@
<Project Sdk="Godot.NET.Sdk/4.2.0-beta.3">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
</Project>

@ -1,4 +1,4 @@
<Project Sdk="Godot.NET.Sdk/4.2.0-beta.2">
<Project Sdk="Godot.NET.Sdk/4.2.0-beta.3">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -48,10 +48,10 @@ freeze_mode = 1
transform = Transform3D(9, 0, 0, 0, 9, 0, 0, 0, 9, 0, 0, 12.4454)
shape = SubResource("BoxShape3D_bw288")
[node name="SoundManager" type="Node" parent="." node_paths=PackedStringArray("explosionSound", "backgroundMusic", "laserSound")]
[node name="SoundManager" type="Node" parent="." node_paths=PackedStringArray("backgroundMusic", "explosionSound", "laserSound")]
script = ExtResource("8_4ksf0")
explosionSound = NodePath("ExplosionSound")
backgroundMusic = NodePath("BackgroundMusic")
explosionSound = NodePath("ExplosionSound")
laserSound = NodePath("LaserSound")
[node name="ExplosionSound" type="AudioStreamPlayer3D" parent="SoundManager"]
@ -65,15 +65,15 @@ autoplay = true
[node name="LaserSound" type="AudioStreamPlayer3D" parent="SoundManager"]
stream = ExtResource("11_bbedd")
[node name="GameManager" type="Node" parent="." node_paths=PackedStringArray("labelAsteroids", "labelLifes", "labelPoints", "labelLevel", "labelNextLevel", "asteroidsContainer")]
[node name="GameManager" type="Node" parent="." node_paths=PackedStringArray("asteroidsContainer", "labelAsteroids", "labelLevel", "labelLifes", "labelNextLevel", "labelPoints")]
script = ExtResource("9_rsrr5")
asteroids = Array[PackedScene]([ExtResource("5_do6ba"), ExtResource("6_tqoe7"), ExtResource("7_v6ul2")])
asteroidsContainer = NodePath("../Asteroids")
labelAsteroids = NodePath("../Control/LabelAsteroids")
labelLifes = NodePath("../Control/LabelLives")
labelPoints = NodePath("../Control/LabelPoints")
labelLevel = NodePath("../Control/LabelLevel")
labelLifes = NodePath("../Control/LabelLives")
labelNextLevel = NodePath("../Control/LabelNextLevel")
asteroidsContainer = NodePath("../Asteroids")
labelPoints = NodePath("../Control/LabelPoints")
[node name="Asteroids" type="Node3D" parent="."]

@ -1,78 +1,79 @@
using Godot;
namespace Scripts;
[GlobalClass]
public partial class Asteroid : Node3D {
[Export] public RigidBody3D AsteroidRb;
[Export] private MeshInstance3D asteroidMesh;
[Export] private CollisionShape3D asteroidCollisionShape;
[Export] private GpuParticlesCollisionSphere3D particleCollision;
[Export] private PackedScene explosion;
[Export] private CollisionShape3D asteroidCollisionShape;
[Export] private MeshInstance3D asteroidMesh;
[Export] public RigidBody3D AsteroidRb;
[Export] private PackedScene explosion;
[Export] private GpuParticlesCollisionSphere3D particleCollision;
private Vector3 rotation;
private float rotSpeed = 100f;
private float size;
private float speed;
private float speed;
private float rotSpeed = 100f;
private float size;
private Vector3 rotation;
public override void _EnterTree() {
Position = new((float)GD.RandRange(-3.7, 3.7), 0, -6.4f);
public override void _EnterTree() {
Position = new((float)GD.RandRange(-3.7, 3.7), 0, -6.4f);
size = (float)GD.RandRange(0.5f, 1f);
Vector3 scale = new(size, size, size);
asteroidMesh.Scale = scale;
AsteroidRb.Scale = scale;
asteroidCollisionShape.Scale = scale;
size = (float)GD.RandRange(0.5f, 1f);
Vector3 scale = new(size, size,size);
asteroidMesh.Scale = scale;
AsteroidRb.Scale = scale;
asteroidCollisionShape.Scale = scale;
speed = GD.Randf() * GameManager.Instance.Level * 0.1f + 1f;
rotation = new((float)GD.RandRange(-rotSpeed, rotSpeed), (float)GD.RandRange(-rotSpeed, rotSpeed), (float)GD.RandRange(-rotSpeed, rotSpeed));
}
speed = GD.Randf() * GameManager.Instance.Level * 0.1f + 1f;
rotation = new((float)GD.RandRange(-rotSpeed, rotSpeed), (float)GD.RandRange(-rotSpeed, rotSpeed), (float)GD.RandRange(-rotSpeed, rotSpeed));
}
public override void _Ready() { }
public override void _Ready() {
}
public override void _PhysicsProcess(double delta) {
AsteroidRb.Rotate(rotation.Normalized(), (float)delta);
public override void _PhysicsProcess(double delta) {
AsteroidRb.Rotate(rotation.Normalized(), (float)delta);
KinematicCollision3D collision = AsteroidRb.MoveAndCollide(new(0, 0, speed * (float)delta));
if (collision?.GetCollider() is Node3D collider) {
Node3D parent = collider.GetParent<Node3D>();
GD.Print($"{Name} collides with {parent.Name}");
Vector3 collisionPosition = collision.GetPosition();
switch (parent) {
case Asteroid collidedAsteroid:
// collidedAsteroid.Explode(collisionPosition);
// Explode(collisionPosition);
break;
case PlayerShip:
GameManager.Instance.Lives--;
PlayerShip.Instance.Explode(collisionPosition);
Explode(collisionPosition);
break;
case GameArea:
Explode(Vector3.Zero);
break;
case LaserBeam laserBeam:
GameManager.Instance.Points++;
laserBeam.QueueFree();
Explode(collisionPosition);
break;
default:
GD.Print("Asteroid collided with unknown.");
break;
}
}
}
KinematicCollision3D collision = AsteroidRb.MoveAndCollide(new(0, 0, speed * (float)delta));
if (collision?.GetCollider() is Node3D collider) {
Node3D parent = collider.GetParent<Node3D>();
GD.Print($"{Name} collides with {parent.Name}");
Vector3 collisionPosition = collision.GetPosition();
switch (parent) {
case Asteroid collidedAsteroid:
// collidedAsteroid.Explode(collisionPosition);
// Explode(collisionPosition);
break;
case PlayerShip:
GameManager.Instance.Lives--;
PlayerShip.Instance.Explode(collisionPosition);
Explode(collisionPosition);
break;
case GameArea:
Explode(Vector3.Zero);
break;
case LaserBeam laserBeam:
GameManager.Instance.Points++;
laserBeam.QueueFree();
Explode(collisionPosition);
break;
default:
GD.Print($"Asteroid collided with unknown.");
break;
}
}
}
public void Explode(Vector3 exPosition) {
if (exPosition != Vector3.Zero) {
SoundManager.Instance.PlayExplosion(exPosition);
if (explosion?.Instantiate() is GpuParticles3D ex) {
GetParent().AddChild(ex);
ex.Position = exPosition;
ex.Emitting = true;
}
}
public void Explode(Vector3 exPosition) {
if (exPosition != Vector3.Zero) {
SoundManager.Instance.PlayExplosion(exPosition);
if (explosion?.Instantiate() is GpuParticles3D ex) {
GetParent().AddChild(ex);
ex.Position = exPosition;
ex.Emitting = true;
}
}
GameManager.Instance.AsteroidNumber--;
QueueFree();
}
GameManager.Instance.AsteroidNumber--;
QueueFree();
}
}

@ -1,11 +1,13 @@
using Godot;
namespace Scripts;
public partial class Background : Sprite3D {
[Export] private float moveVelocity = -250f;
public override void _Process(double delta) {
Rect2 region = RegionRect;
region.Position += new Vector2(0, moveVelocity * (float)delta);
RegionRect = region;
}
[Export] private float moveVelocity = -250f;
public override void _Process(double delta) {
Rect2 region = RegionRect;
region.Position += new Vector2(0, moveVelocity * (float)delta);
RegionRect = region;
}
}

@ -1,8 +1,7 @@
using Godot;
public partial class Explosion : GpuParticles3D
{
public override void _Process(double delta) {
if (!Emitting) QueueFree();
}
public partial class Explosion : GpuParticles3D {
public override void _Process(double delta) {
if (!Emitting) QueueFree();
}
}

@ -1,7 +1,3 @@
using Godot;
using System;
public partial class GameArea : Node3D
{
}
public partial class GameArea : Node3D { }

@ -4,16 +4,23 @@ using Godot.Collections;
namespace Scripts;
public partial class GameManager : Node {
private int asteroidMaxNumber = 3;
private int asteroidNumber;
[Export] private Array<PackedScene> asteroids;
private float spawnTimer;
[Export] private Node3D asteroidsContainer;
[Export] private RichTextLabel labelAsteroids;
[Export] private RichTextLabel labelLifes;
[Export] private RichTextLabel labelPoints;
[Export] private RichTextLabel labelLevel;
[Export] private RichTextLabel labelLifes;
[Export] private RichTextLabel labelNextLevel;
[Export] private Node3D asteroidsContainer;
[Export] private RichTextLabel labelPoints;
private int level = 1;
private int lives = 10;
private int nextLevelPoints = 11;
private int points;
private float spawnRate = 1f;
private float spawnTimer;
public int AsteroidNumber {
get => asteroidNumber;
@ -65,14 +72,6 @@ public partial class GameManager : Node {
}
}
private int asteroidMaxNumber = 3;
private int lives = 10;
private int points;
private int nextLevelPoints = 11;
private int level = 1;
private int asteroidNumber;
private float spawnRate = 1f;
public static GameManager Instance { get; private set; }
@ -106,7 +105,9 @@ public partial class GameManager : Node {
labelLevel.Text = $"Actual Level: {Level}";
}
private static void GameOver() => GD.Print("GAME OVER!");
private static void GameOver() {
GD.Print("GAME OVER!");
}
private void NextLevel() {
GD.Print("Next Level!");
@ -116,5 +117,7 @@ public partial class GameManager : Node {
spawnRate = 1f - Level * 0.1f;
}
public void OnGameAreaBodyExited(Node body) => GD.Print($"{body.Name} leaved GameArea!");
public void OnGameAreaBodyExited(Node body) {
GD.Print($"{body.Name} leaved GameArea!");
}
}

@ -1,10 +1,11 @@
using Godot;
public partial class LaserBeam : Node3D {
[Export] private RigidBody3D rigidBody;
private float speed = 10f;
public override void _Ready() {
Vector3 movement = new(0f, 0f, -speed);
rigidBody.AddConstantForce(movement);
}
[Export] private RigidBody3D rigidBody;
private float speed = 10f;
public override void _Ready() {
Vector3 movement = new(0f, 0f, -speed);
rigidBody.AddConstantForce(movement);
}
}

@ -1,101 +1,101 @@
using System.Diagnostics;
using Godot;
namespace Scripts;
public partial class PlayerShip : Node3D {
[Export] private float moveVelocity = 10f;
[Export] private float rotVelocity = 15f;
[Export] private Node3D jet;
[Export] public RigidBody3D PlayerRb;
[Export] private PackedScene explosion;
[Export] private GpuParticles3D gpulaser;
[Export] private CpuParticles3D cpulaser;
[Export] private PackedScene laser;
[Export] private Node3D shots;
private const float jetTimerMax = 0.5f;
private const float laserTimerMax = 1f;
public static PlayerShip Instance { get; private set;}
private const string PLAYER_MOVE_FORWARD = "Player_Move_Forward";
private const string PLAYER_MOVE_BACKWARDS = "Player_Move_Backwards";
private const string PLAYER_MOVE_LEFT = "Player_Move_Left";
private const string PLAYER_MOVE_RIGHT = "Player_Move_Right";
private const string PLAYER_FIRE = "Player_Fire";
[Export] private CpuParticles3D cpulaser;
[Export] private PackedScene explosion;
[Export] private GpuParticles3D gpulaser;
[Export] private Node3D jet;
private float jetTimer = jetTimerMax;
[Export] private PackedScene laser;
private float laserTimer = laserTimerMax;
private const float jetTimerMax = 0.5f;
private float jetTimer = jetTimerMax;
private Vector3 moveDir;
[Export] private float moveVelocity = 10f;
[Export] public RigidBody3D PlayerRb;
private Vector3 rotDir;
[Export] private float rotVelocity = 15f;
private bool shooting;
[Export] private Node3D shots;
private const float laserTimerMax = 1f;
private float laserTimer = laserTimerMax;
public static PlayerShip Instance { get; private set; }
private Vector3 moveDir;
private Vector3 rotDir;
public override void _Ready() {
Position = new(0, 0, 5);
Instance = this;
}
private const string PLAYER_MOVE_FORWARD = "Player_Move_Forward";
private const string PLAYER_MOVE_BACKWARDS = "Player_Move_Backwards";
private const string PLAYER_MOVE_LEFT = "Player_Move_Left";
private const string PLAYER_MOVE_RIGHT = "Player_Move_Right";
private const string PLAYER_FIRE = "Player_Fire";
private bool shooting;
public override void _Process(double delta) {
moveDir = Vector3.Zero;
rotDir = Vector3.Zero;
public override void _Ready() {
Position = new(0, 0, 5);
Instance = this;
}
//Movement
if (Input.IsActionPressed(PLAYER_MOVE_FORWARD)) moveDir.Z -= moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_FORWARD)) rotDir.X = -rotVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_BACKWARDS)) moveDir.Z += moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_BACKWARDS)) rotDir.X = +rotVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_LEFT)) moveDir.X -= moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_LEFT)) rotDir.Z = -rotVelocity * 2;
if (Input.IsActionPressed(PLAYER_MOVE_RIGHT)) moveDir.X += moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_RIGHT)) rotDir.Z = +rotVelocity * 2;
if (Input.IsActionJustReleased(PLAYER_MOVE_LEFT) || Input.IsActionJustReleased(PLAYER_MOVE_RIGHT)) rotDir = Vector3.Zero;
public override void _Process(double delta) {
moveDir = Vector3.Zero;
rotDir = Vector3.Zero;
//Fire
if (Input.IsActionJustPressed(PLAYER_FIRE))
if (laser.Instantiate() is Node3D shot) {
shot.Position = PlayerRb.Position + new Vector3(0f, 0f, 0.74f);
shots.AddChild(shot);
SoundManager.Instance.PlayLaserSound(PlayerRb.Position);
}
}
//Movement
if (Input.IsActionPressed(PLAYER_MOVE_FORWARD)) moveDir.Z -= moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_FORWARD)) rotDir.X = -rotVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_BACKWARDS)) moveDir.Z += moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_BACKWARDS)) rotDir.X = +rotVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_LEFT)) moveDir.X -= moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_LEFT)) rotDir.Z = -rotVelocity * 2;
if (Input.IsActionPressed(PLAYER_MOVE_RIGHT)) moveDir.X += moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_RIGHT)) rotDir.Z = +rotVelocity * 2;
if (Input.IsActionJustReleased(PLAYER_MOVE_LEFT) || Input.IsActionJustReleased(PLAYER_MOVE_RIGHT)) rotDir = Vector3.Zero;
public override void _PhysicsProcess(double delta) {
if (moveDir != Vector3.Zero) {
jet.Visible = true;
KinematicCollision3D collision = PlayerRb.MoveAndCollide(moveDir * (float)delta);
if (collision?.GetCollider() is Node3D collider) GD.Print($"{Name} collides with {collider.GetParentNode3D().Name}");
}
else {
jet.Visible = false;
}
//Fire
if (Input.IsActionJustPressed(PLAYER_FIRE)) {
if (laser.Instantiate() is Node3D shot) {
shot.Position = PlayerRb.Position + new Vector3(0f, 0f, 0.74f);
shots.AddChild(shot);
SoundManager.Instance.PlayLaserSound(PlayerRb.Position);
}
}
}
PlayerRb.RotationDegrees = rotDir;
CheckBoundaries();
}
public override void _PhysicsProcess(double delta) {
if (moveDir != Vector3.Zero) {
jet.Visible = true;
KinematicCollision3D collision = PlayerRb.MoveAndCollide(moveDir * (float)delta);
if (collision?.GetCollider() is Node3D collider) GD.Print($"{Name} collides with {collider.GetParentNode3D().Name}");
}
else {
jet.Visible = false;
}
PlayerRb.RotationDegrees = rotDir;
CheckBoundaries();
}
private void CheckBoundaries() {
Vector3 correctedPos = PlayerRb.Position;
correctedPos.X = Position.X switch {
< -3.3f => -3.3f,
> 3.3f => 3.3f,
_ => correctedPos.X
};
correctedPos.Z = Position.Z switch {
< -5.2f => -5.2f,
> 5.2f => 5.2f,
_ => correctedPos.Z
};
PlayerRb.Position = correctedPos;
}
private void CheckBoundaries() {
Vector3 correctedPos = PlayerRb.Position;
correctedPos.X = Position.X switch {
< -3.3f => -3.3f,
> 3.3f => 3.3f,
_ => correctedPos.X
};
correctedPos.Z = Position.Z switch {
< -5.2f => -5.2f,
> 5.2f => 5.2f,
_ => correctedPos.Z
};
PlayerRb.Position = correctedPos;
}
public void Explode(Vector3 collisionPosition) {
if (explosion?.Instantiate() is GpuParticles3D ex) {
GetParent().AddChild(ex);
ex.Position = collisionPosition;
ex.Emitting = true;
}
public void Explode(Vector3 collisionPosition) {
if (explosion?.Instantiate() is GpuParticles3D ex) {
GetParent().AddChild(ex);
ex.Position = collisionPosition;
ex.Emitting = true;
}
SoundManager.Instance.PlayExplosion(collisionPosition);
GameManager.Instance.Lives--;
}
SoundManager.Instance.PlayExplosion(collisionPosition);
GameManager.Instance.Lives--;
}
}

@ -1,15 +1,16 @@
using Godot;
using Godot.Collections;
namespace Scripts;
public partial class SoundManager : Node {
[Export] private AudioStreamPlayer3D explosionSound;
[Export] private AudioStreamPlayer backgroundMusic;
[Export] private AudioStreamPlayer3D explosionSound;
[Export] private AudioStreamPlayer3D laserSound;
public static SoundManager Instance { get; private set; }
public override void _Ready() => Instance = this;
public override void _Ready() {
Instance = this;
}
public void PlayExplosion(Vector3 position) {
explosionSound.Position = position;

@ -0,0 +1,42 @@
[preset.0]
name="Linux/X11"
platform="Linux/X11"
runnable=true
dedicated_server=false
custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path=""
encryption_include_filters=""
encryption_exclude_filters=""
encrypt_pck=false
encrypt_directory=false
[preset.0.options]
custom_template/debug=""
custom_template/release=""
debug/export_console_wrapper=1
binary_format/embed_pck=false
texture_format/bptc=true
texture_format/s3tc=true
texture_format/etc=false
texture_format/etc2=false
binary_format/architecture="x86_64"
ssh_remote_deploy/enabled=false
ssh_remote_deploy/host="user@host_ip"
ssh_remote_deploy/port="22"
ssh_remote_deploy/extra_args_ssh=""
ssh_remote_deploy/extra_args_scp=""
ssh_remote_deploy/run_script="#!/usr/bin/env bash
export DISPLAY=:0
unzip -o -q \"{temp_dir}/{archive_name}\" -d \"{temp_dir}\"
\"{temp_dir}/{exe_name}\" {cmd_args}"
ssh_remote_deploy/cleanup_script="#!/usr/bin/env bash
kill $(pgrep -x -f \"{temp_dir}/{exe_name} {cmd_args}\")
rm -rf \"{temp_dir}\""
dotnet/include_scripts_content=false
dotnet/include_debug_symbols=true
dotnet/embed_build_outputs=false