Directional shooting

This commit is contained in:
MaddoScientisto 2024-05-01 11:48:04 +02:00
commit 65ce30a2a4
7 changed files with 158 additions and 10 deletions

View file

@ -8,6 +8,7 @@ radius = 2.23607
[node name="Bullet" type="Area2D"] [node name="Bullet" type="Area2D"]
script = ExtResource("1_jvxw3") script = ExtResource("1_jvxw3")
Speed = 200.0
metadata/_edit_group_ = true metadata/_edit_group_ = true
[node name="Sprite2D" type="Sprite2D" parent="."] [node name="Sprite2D" type="Sprite2D" parent="."]

View file

@ -136,7 +136,7 @@ animations = [{
collision_layer = 2 collision_layer = 2
collision_mask = 3 collision_mask = 3
script = ExtResource("1_m27vu") script = ExtResource("1_m27vu")
Speed = 1800 Speed = 2000
BulletScene = ExtResource("2_ov36d") BulletScene = ExtResource("2_ov36d")
Muzzle = NodePath("Muzzle") Muzzle = NodePath("Muzzle")
metadata/_edit_group_ = true metadata/_edit_group_ = true

File diff suppressed because one or more lines are too long

View file

@ -12,9 +12,19 @@ public partial class Bullet : Area2D
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
Debug.WriteLine("Bullet Shot");
} }
public void SetDirection(Vector2 direction)
{
var normalized = direction.Normalized();
_direction = normalized;
Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}");
}
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) public override void _Process(double delta)
{ {

View file

@ -11,13 +11,19 @@ public partial class PlayerMovement : CharacterBody2D
public PackedScene BulletScene { get; set; } public PackedScene BulletScene { get; set; }
[Export] [Export]
public Marker2D Muzzle {get;set;} public Marker2D Muzzle { get; set; }
private AnimatedSprite2D _animatedSprite; private AnimatedSprite2D _animatedSprite;
private Vector2 _movementDirection { get; set; }
private Vector2 _facingDirection { get; set; }
public override void _Ready() public override void _Ready()
{ {
_animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D"); _animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
_movementDirection = Vector2.Zero;
_facingDirection = Vector2.Zero;
} }
/*public override _Process(float _delta) /*public override _Process(float _delta)
@ -41,12 +47,13 @@ public partial class PlayerMovement : CharacterBody2D
private void HandleShoot() private void HandleShoot()
{ {
if (Input.IsActionJustPressed("shoot")) if (Input.IsActionJustPressed("shoot"))
{ {
Debug.WriteLine("Shoot"); Debug.WriteLine("Shoot");
Bullet bullet = BulletScene.Instantiate<Bullet>(); Bullet bullet = BulletScene.Instantiate<Bullet>();
Owner.AddChild(bullet); Owner.AddChild(bullet);
bullet.Transform = Muzzle.GlobalTransform; bullet.Transform = Muzzle.GlobalTransform;
bullet.Position = this.Position; bullet.Position = this.Position;
bullet.SetDirection(this._movementDirection);
} }
} }
@ -89,9 +96,12 @@ public partial class PlayerMovement : CharacterBody2D
} }
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)
{ {
var inputDirection = GetInput(); _movementDirection = GetInput();
Velocity = inputDirection * (float)(Speed * delta); if (_movementDirection != Vector2.Zero) {
_facingDirection = _movementDirection;
}
Velocity = _movementDirection * (float)(Speed * delta);
MoveAndSlide(); MoveAndSlide();
} }

BIN
Sprites/tinyBlocks_NOiL_1.1update.png (Stored with Git LFS) Normal file

Binary file not shown.

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bxclo2fg3ulur"
path="res://.godot/imported/tinyBlocks_NOiL_1.1update.png-50a5b3cb0e9f95f564631e9103861ec5.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Sprites/tinyBlocks_NOiL_1.1update.png"
dest_files=["res://.godot/imported/tinyBlocks_NOiL_1.1update.png-50a5b3cb0e9f95f564631e9103861ec5.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