From 52602364f30964e25daafd09e20c5fa498209e70 Mon Sep 17 00:00:00 2001 From: Just Dzhi Date: Sun, 1 Oct 2023 16:37:24 +0400 Subject: [PATCH] IN PROCESS Shoot --- project.godot | 6 +++++ src/models/bullet/Bullet.gd | 8 +++++++ src/models/bullet/Bullet.tscn | 16 +++++++++++++ src/models/bullet/bullet.png | Bin 0 -> 1796 bytes src/models/bullet/bullet.png.import | 35 ++++++++++++++++++++++++++++ src/models/enemies/bat/bat.gd | 2 +- src/models/player/player.gd | 15 +++++++++++- 7 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/models/bullet/Bullet.gd create mode 100644 src/models/bullet/Bullet.tscn create mode 100644 src/models/bullet/bullet.png create mode 100644 src/models/bullet/bullet.png.import diff --git a/project.godot b/project.godot index b373694..7b17d93 100644 --- a/project.godot +++ b/project.godot @@ -75,6 +75,12 @@ down={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":16777234,"unicode":0,"echo":false,"script":null) ] } +shoot={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":32,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null) + ] +} [physics] diff --git a/src/models/bullet/Bullet.gd b/src/models/bullet/Bullet.gd new file mode 100644 index 0000000..1239bb3 --- /dev/null +++ b/src/models/bullet/Bullet.gd @@ -0,0 +1,8 @@ +extends KinematicBody2D + +var speed = 128 +var velocity = Vector2.ZERO + +func _physics_process(_delta): + velocity.x = speed + move_and_slide(velocity) diff --git a/src/models/bullet/Bullet.tscn b/src/models/bullet/Bullet.tscn new file mode 100644 index 0000000..ea59ef4 --- /dev/null +++ b/src/models/bullet/Bullet.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://src/models/bullet/bullet.png" type="Texture" id=1] +[ext_resource path="res://src/models/bullet/Bullet.gd" type="Script" id=2] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 3.5, 3.5 ) + +[node name="Bullet" type="KinematicBody2D"] +script = ExtResource( 2 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) diff --git a/src/models/bullet/bullet.png b/src/models/bullet/bullet.png new file mode 100644 index 0000000000000000000000000000000000000000..48433ffe38a058355e634e34879ca71a1082411a GIT binary patch literal 1796 zcmcIlO>Epm7&Wa_(pHgzdO<~^#yL;~UXTCQYpZUXW|Jlo4r#hlQn)al8Sh$IdyMVP zZi0{y7vRE$6Nm#sAaQ^SArJ=+2nmU*>aDo}5{If+#2F5Jvuke)YSUa;$?Ly2?|bvT z=b3lc*DlYVIDJBrq}kP#_6D3E7st#o`2T5R^ERB0XDinVNjiB-9FIvKe)Oay9lI8H zw#u#GMUU{LfhiAJW0YiomZWnRMj0mCtdv96jZyO_9fta@$uHL_v8gSeF-h&Sa-y zs01M-J8nY{n>Kdb7i0q&hK8D&ZmPQDSxry3v6QeD>4RlP~wR>ZJHa5&Zlslru4Adh1RmQ4 zRiR~3)oePJPFl9^U_+7b)m`L$o(G&lcY&W0FXDNEAwO|~yG+Z{u1GVm!z@Gz&M2stuQLciNEi zb(pLWy9^UuHCwKu8m@)aRuo1mji{^Jh&aeGCw2RAtzbf=9b9p&^}gk?9t>-)xSX!` za5LR6SPE0kVK 64: velocity.x = cos(angle) diff --git a/src/models/player/player.gd b/src/models/player/player.gd index ad57e33..503f55d 100644 --- a/src/models/player/player.gd +++ b/src/models/player/player.gd @@ -5,6 +5,14 @@ var speed_multiplier: float = 1.0 var slowed = false var target var speed = 100 + +onready var bullet = preload("res://src/models/bullet/Bullet.tscn") +var bullet_instance + +func _process(_delta): + if Input.is_action_just_pressed("shoot"): + _shoot() + func get_input(): var input_direction = Input.get_vector("left", "right", "up", "down") velocity = input_direction * speed @@ -29,7 +37,6 @@ func _physics_process(_delta): const SHADER = preload("res://res/shaders/outline.tres") - func choose_target(): var least_dist = 4000 var enemy_dist @@ -40,3 +47,9 @@ func choose_target(): least_dist = enemy_dist target = enemy #enemy.set_shader_param("width", 0) + +func _shoot(): + bullet_instance = bullet.instance() + get_parent().add_child(bullet_instance) + bullet_instance.global_position.x = get_node("../Player").global_position.x + 12 + bullet_instance.global_position.y = get_node("../Player").global_position.y - 4