From 0ccc272ada4d39da383acbbd75047c47397484d3 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 28 Jan 2025 10:43:35 +0100 Subject: [PATCH] Hitbox visible --- Scenes/Enemy.tscn | 2 ++ Scenes/player.tscn | 12 +++++++--- Scripts/PlayerMovement.cs | 22 +++++++++++++++++ Scripts/Weapon.cs | 44 ++++++++++++++++++++++------------ Sprites/Hitbox.aseprite | 3 +++ Sprites/Hitbox.png | 3 +++ Sprites/Hitbox.png.import | 34 ++++++++++++++++++++++++++ Sprites/SmallHitbox.aseprite | 3 +++ Sprites/SmallHitbox.png | 3 +++ Sprites/SmallHitbox.png.import | 34 ++++++++++++++++++++++++++ 10 files changed, 142 insertions(+), 18 deletions(-) create mode 100644 Sprites/Hitbox.aseprite create mode 100644 Sprites/Hitbox.png create mode 100644 Sprites/Hitbox.png.import create mode 100644 Sprites/SmallHitbox.aseprite create mode 100644 Sprites/SmallHitbox.png create mode 100644 Sprites/SmallHitbox.png.import diff --git a/Scenes/Enemy.tscn b/Scenes/Enemy.tscn index 4cae04fe..5f9ada5a 100644 --- a/Scenes/Enemy.tscn +++ b/Scenes/Enemy.tscn @@ -55,6 +55,8 @@ one_shot = true BulletScene = ExtResource("2_ogldd") BulletCapacity = 4 BulletSpeed = 50.0 +BulletsPerShot = 3 +BulletsSpreadAngle = 40.0 [node name="NavigationAgent2D" type="NavigationAgent2D" parent="."] diff --git a/Scenes/player.tscn b/Scenes/player.tscn index e359dd29..d9d6d50b 100644 --- a/Scenes/player.tscn +++ b/Scenes/player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=27 format=3 uid="uid://bghghp5ep4w2j"] +[gd_scene load_steps=28 format=3 uid="uid://bghghp5ep4w2j"] [ext_resource type="Script" path="res://Scripts/PlayerMovement.cs" id="1_m27vu"] [ext_resource type="Texture2D" uid="uid://la06powu57hu" path="res://Sprites/Cirno_Big.png" id="2_bwf6x"] @@ -6,6 +6,7 @@ [ext_resource type="PackedScene" uid="uid://chkpk7erlqajg" path="res://Scenes/Selector.tscn" id="3_8wt6s"] [ext_resource type="Script" path="res://addons/smoothing/smoothing_2d.gd" id="4_j4xhu"] [ext_resource type="PackedScene" uid="uid://cfb3nsay84xdb" path="res://Scenes/Weapons/crosshair.tscn" id="6_l43rf"] +[ext_resource type="Texture2D" uid="uid://bf37ce6jskdel" path="res://Sprites/SmallHitbox.png" id="7_msn8i"] [ext_resource type="Script" path="res://Scenes/InteractionController.cs" id="7_uvgjg"] [ext_resource type="PackedScene" uid="uid://crry0rgk7a8sm" path="res://Scenes/Weapons/BaseWeapon.tscn" id="9_wblq0"] @@ -139,9 +140,9 @@ animations = [{ radius = 17.2627 [sub_resource type="CircleShape2D" id="CircleShape2D_e6woi"] -radius = 1.41421 +radius = 1.0 -[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("Muzzle", "EquippedWeapon") groups=["Destroyable", "player"]] +[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("Muzzle", "EquippedWeapon", "HitboxSprite") groups=["Destroyable", "player"]] collision_layer = 2 collision_mask = 99 script = ExtResource("1_m27vu") @@ -151,6 +152,7 @@ SelectorScene = ExtResource("3_8wt6s") GameOverScene = "res://Scenes/GameOver.tscn" Muzzle = NodePath("Muzzle") EquippedWeapon = NodePath("Weapon") +HitboxSprite = NodePath("Smoothing2D/HitboxSprite") metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="."] @@ -181,7 +183,11 @@ animation = &"walk_left" [node name="Crosshair" parent="Smoothing2D" instance=ExtResource("6_l43rf")] +[node name="HitboxSprite" type="Sprite2D" parent="Smoothing2D"] +texture = ExtResource("7_msn8i") + [node name="InteractionController" type="Area2D" parent="."] +visible = false collision_layer = 2 collision_mask = 4 script = ExtResource("7_uvgjg") diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index dc5e08f0..8a4f75a8 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -44,6 +44,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible private GameManager _gameManager; + [Export] public Sprite2D HitboxSprite { get; set; } + private bool _isStrafing { get; set; } [Signal] @@ -192,10 +194,30 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible } + // public override void _Draw() + // { + // if (_isStrafing) + // { + // HitboxSprite.Visible = true; + // } + // else + // { + // HitboxSprite.Visible = false; + // } + // base._Draw(); + // } + public override void _PhysicsProcess(double delta) { _movementDirection = GetInput(); + _isStrafing = Input.IsActionPressed("strafe"); + // Toggle visibility of the hitbox sprite based on strafing + if (HitboxSprite != null) + { + HitboxSprite.Visible = _isStrafing; + } + _rightStickInput = GetRightStickInput(); // Update Facing Direction diff --git a/Scripts/Weapon.cs b/Scripts/Weapon.cs index b3c9b77e..f8b3479d 100644 --- a/Scripts/Weapon.cs +++ b/Scripts/Weapon.cs @@ -23,6 +23,11 @@ public partial class Weapon : Node2D [Export] public bool AutoReload = true; [Export] public bool InfiniteAmmo = true; + + [Export] public int BulletsPerShot = 1; + + [Export] public float BulletsSpreadAngle = 0f; + public int Ammo { get; set; } = 0; public int LoadedAmmo { get; private set; } @@ -80,23 +85,32 @@ public partial class Weapon : Node2D } // TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius? - // TODO: Create not as child but as standalone - - // if (BulletScene == null) return; - // - // var newInstance = BulletScene.Instantiate(); - // _gameManager.BulletsContainer.CallDeferred("add_child", newInstance); - - var bullet = this.CreateChildOf(_gameManager.BulletsContainer, BulletScene, _muzzle.GlobalPosition); - if (bullet == null) - { - GD.PrintErr("Bullet is null, not shooting"); - return; - }; + float halfSpread = BulletsSpreadAngle / 2f; + float spreadStep = BulletsPerShot > 1 ? BulletsSpreadAngle / (BulletsPerShot - 1) : 0; + - bullet.SetDirection(ShootDirection); - bullet.Speed = BulletSpeed; + for (int i = 0; i < BulletsPerShot; i++) + { + // Calculate angle offset for this bullet + float spreadOffset = -halfSpread + (spreadStep * i); + + // Rotate the ShootDirection by the spread angle + Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset)); + + + var bullet = this.CreateChildOf(_gameManager.BulletsContainer, BulletScene, _muzzle.GlobalPosition); + + if (bullet == null) + { + GD.PrintErr("Bullet is null, not shooting"); + return; + }; + + //bullet.SetDirection(ShootDirection); + bullet.SetDirection(spreadDirection); + bullet.Speed = BulletSpeed; + } LoadedAmmo -= 1; diff --git a/Sprites/Hitbox.aseprite b/Sprites/Hitbox.aseprite new file mode 100644 index 00000000..cff2e3a9 --- /dev/null +++ b/Sprites/Hitbox.aseprite @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6d57474cf6cb67a99095cfdb508de6081ce066233a28338a0fcc62b10adbee11 +size 580 diff --git a/Sprites/Hitbox.png b/Sprites/Hitbox.png new file mode 100644 index 00000000..69cb930f --- /dev/null +++ b/Sprites/Hitbox.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:14e62a6f05ed3e2ccc2804d8096a17bc458026194425bdb194aa832e1dc4f125 +size 131 diff --git a/Sprites/Hitbox.png.import b/Sprites/Hitbox.png.import new file mode 100644 index 00000000..47a692fd --- /dev/null +++ b/Sprites/Hitbox.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://b6sx61lte2s8b" +path="res://.godot/imported/Hitbox.png-f56dac3171bc251935bb76ec82c19051.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Sprites/Hitbox.png" +dest_files=["res://.godot/imported/Hitbox.png-f56dac3171bc251935bb76ec82c19051.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/Sprites/SmallHitbox.aseprite b/Sprites/SmallHitbox.aseprite new file mode 100644 index 00000000..0a179fb3 --- /dev/null +++ b/Sprites/SmallHitbox.aseprite @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cc77a7b4d75b8e58a6e9448a0966e763b50fbf86535be0b330d71b35a8271a26 +size 575 diff --git a/Sprites/SmallHitbox.png b/Sprites/SmallHitbox.png new file mode 100644 index 00000000..45436224 --- /dev/null +++ b/Sprites/SmallHitbox.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4062c183775e37065f367e5440f756b203c10f5e807737a46afaa4262b77227f +size 107 diff --git a/Sprites/SmallHitbox.png.import b/Sprites/SmallHitbox.png.import new file mode 100644 index 00000000..7bba46f8 --- /dev/null +++ b/Sprites/SmallHitbox.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://bf37ce6jskdel" +path="res://.godot/imported/SmallHitbox.png-03b8e18b0a68a463319a17855b03e809.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Sprites/SmallHitbox.png" +dest_files=["res://.godot/imported/SmallHitbox.png-03b8e18b0a68a463319a17855b03e809.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1