diff --git a/game/entities/player.tscn b/game/entities/player.tscn index babbfc9..be7ad5f 100644 --- a/game/entities/player.tscn +++ b/game/entities/player.tscn @@ -7,6 +7,8 @@ script = ExtResource("1_3a8sv") [node name="Ship" parent="." instance=ExtResource("2_ktqbp")] +collision_layer = 2 +collision_mask = 21 size = Vector2(48, 32) acceleration = 46 deceleration = 23 diff --git a/game/entities/ship.tscn b/game/entities/ship.tscn index a665a63..f0272f1 100644 --- a/game/entities/ship.tscn +++ b/game/entities/ship.tscn @@ -7,6 +7,7 @@ height = 20.0 [node name="Ship" type="CharacterBody2D"] disable_mode = 1 +collision_layer = 7 motion_mode = 1 wall_min_slide_angle = 3.1415927 script = ExtResource("1_6isjb") diff --git a/game/entities/weapons/projectiles/abstract_projectile.gd b/game/entities/weapons/projectiles/abstract_projectile.gd index 31a8821..3edefba 100644 --- a/game/entities/weapons/projectiles/abstract_projectile.gd +++ b/game/entities/weapons/projectiles/abstract_projectile.gd @@ -2,6 +2,13 @@ class_name AbstractProjectile extends CharacterBody2D +const PLAYER_LAYER = 2 +const ENEMY_LAYER = 4 +const PLAYER_PROJECTILE_LAYER = 8 +const ENEMY_PROJECTILE_LAYER = 16 +const PROJECTILE_BORDER_LAYER = 32 + + signal destroyed @@ -12,6 +19,20 @@ signal destroyed @export var max_distance : int @export var max_livetime : int +@export var collide_player: bool: + set(value): + collide_player = value + _apply_collision_mask() + get: + return collide_player + +@export var collide_enemies: bool: + set(value): + collide_enemies = value + _apply_collision_mask() + get: + return collide_enemies + var _traveled_distance: float var _livetime: float @@ -19,10 +40,14 @@ var _livetime: float func _ready() -> void: velocity = direction.normalized() * speed + _apply_collision_mask() -func move(delta: float) -> void: - position += velocity * delta +func _physics_process(_delta: float) -> void: + var was_collided := move_and_slide() + if was_collided: + destroyed.emit() + queue_free() func process_acceleration(delta: float) -> void: @@ -46,3 +71,21 @@ func process_livetime(delta: float) -> void: _livetime += delta if _livetime > max_livetime: destroyed.emit() + +func _apply_collision_mask() -> void: + collision_mask |= PROJECTILE_BORDER_LAYER + + if collide_player: + collision_layer |= ENEMY_PROJECTILE_LAYER + collision_mask |= PLAYER_LAYER + else: + collision_layer &= ~ENEMY_PROJECTILE_LAYER + collision_mask &= ~PLAYER_LAYER + + if collide_enemies: + collision_layer |= PLAYER_PROJECTILE_LAYER + collision_mask |= ENEMY_LAYER + else: + collision_layer &= ~PLAYER_PROJECTILE_LAYER + collision_mask &= ~ENEMY_LAYER + diff --git a/game/entities/weapons/projectiles/abstract_projectile.tscn b/game/entities/weapons/projectiles/abstract_projectile.tscn index 65b9eb3..e4a3a2e 100644 --- a/game/entities/weapons/projectiles/abstract_projectile.tscn +++ b/game/entities/weapons/projectiles/abstract_projectile.tscn @@ -3,5 +3,7 @@ [ext_resource type="Script" uid="uid://ctmjb3nkxrepu" path="res://game/entities/weapons/projectiles/abstract_projectile.gd" id="1_4tgfk"] [node name="AbstractProjectile" type="CharacterBody2D"] +collision_layer = 0 +collision_mask = 0 motion_mode = 1 script = ExtResource("1_4tgfk") diff --git a/game/entities/weapons/projectiles/gatling_projectile.gd b/game/entities/weapons/projectiles/gatling_projectile.gd index ee294dd..04d8160 100644 --- a/game/entities/weapons/projectiles/gatling_projectile.gd +++ b/game/entities/weapons/projectiles/gatling_projectile.gd @@ -7,7 +7,3 @@ func _ready() -> void: $Sprite2D.texture = texture super._ready() - - -func _physics_process(delta: float) -> void: - move(delta) diff --git a/game/entities/weapons/projectiles/gatling_projectile.tscn b/game/entities/weapons/projectiles/gatling_projectile.tscn index 2dffcb7..bfeca46 100644 --- a/game/entities/weapons/projectiles/gatling_projectile.tscn +++ b/game/entities/weapons/projectiles/gatling_projectile.tscn @@ -1,11 +1,16 @@ -[gd_scene load_steps=3 format=3 uid="uid://cnoiv8hdgossf"] +[gd_scene load_steps=4 format=3 uid="uid://cnoiv8hdgossf"] [ext_resource type="Script" uid="uid://rtsf1n0djorp" path="res://game/entities/weapons/projectiles/gatling_projectile.gd" id="1_xq7oi"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_xq7oi"] size = Vector2(4, 4) +[sub_resource type="CircleShape2D" id="CircleShape2D_xq7oi"] +radius = 1.0 + [node name="GatlingProjectile" type="CharacterBody2D"] +collision_layer = 0 +collision_mask = 0 motion_mode = 1 script = ExtResource("1_xq7oi") damage = 6 @@ -16,3 +21,4 @@ metadata/_custom_type_script = "uid://ctmjb3nkxrepu" texture = SubResource("PlaceholderTexture2D_xq7oi") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_xq7oi") diff --git a/game/entities/weapons/weapon.gd b/game/entities/weapons/weapon.gd index eca3235..9a46cad 100644 --- a/game/entities/weapons/weapon.gd +++ b/game/entities/weapons/weapon.gd @@ -2,6 +2,11 @@ class_name Weapon extends Node2D +enum Belonging { PLAYER, ENEMY } + + +@export var belonging: Belonging + @export var damage : int @export var bullet_per_shot : int @export var sector_angle : int @@ -41,10 +46,17 @@ func _ready() -> void: func shoot() -> void: if not _can_shoot(): return - print("shot") var projectile := preload("res://game/entities/weapons/projectiles/gatling_projectile.tscn").instantiate() - projectile.direction = Vector2.RIGHT + + match belonging: + Belonging.PLAYER: + projectile.direction = Vector2.RIGHT + projectile.collide_enemies = true + Belonging.ENEMY: + projectile.direction = Vector2.LEFT + projectile.collide_player = true + add_child(projectile) for reloader in reloaders: diff --git a/game/entities/weapons/weapon.tscn b/game/entities/weapons/weapon.tscn index 459e213..ee742a9 100644 --- a/game/entities/weapons/weapon.tscn +++ b/game/entities/weapons/weapon.tscn @@ -4,5 +4,9 @@ [node name="Weapon" type="Node2D"] script = ExtResource("1_4oq3a") +belonging = null +damage = null +bullet_per_shot = null +sector_angle = null [node name="Sprite2D" type="Sprite2D" parent="."] diff --git a/game/passage.tscn b/game/passage.tscn index 56ffa21..3479b5e 100644 --- a/game/passage.tscn +++ b/game/passage.tscn @@ -4,28 +4,58 @@ [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_ltkyg"] -[node name="Passage" type="StaticBody2D"] +[node name="Passage" type="Node"] script = ExtResource("1_ltkyg") -[node name="CollisionTop" type="CollisionShape2D" parent="."] +[node name="World" type="StaticBody2D" parent="."] +collision_mask = 0 + +[node name="CollisionTop" type="CollisionShape2D" parent="World"] position = Vector2(320, 0) rotation = 3.1415927 shape = SubResource("WorldBoundaryShape2D_ltkyg") one_way_collision = true -[node name="CollisionBottom" type="CollisionShape2D" parent="."] +[node name="CollisionBottom" type="CollisionShape2D" parent="World"] position = Vector2(320, 360) shape = SubResource("WorldBoundaryShape2D_ltkyg") one_way_collision = true -[node name="CollisionLeft" type="CollisionShape2D" parent="."] +[node name="CollisionLeft" type="CollisionShape2D" parent="World"] position = Vector2(0, 180) rotation = 1.5707964 shape = SubResource("WorldBoundaryShape2D_ltkyg") one_way_collision = true -[node name="CollisionRight" type="CollisionShape2D" parent="."] +[node name="CollisionRight" type="CollisionShape2D" parent="World"] position = Vector2(640, 180) rotation = -1.5707964 shape = SubResource("WorldBoundaryShape2D_ltkyg") one_way_collision = true + +[node name="ProjectileBorder" type="StaticBody2D" parent="."] +collision_layer = 32 +collision_mask = 0 + +[node name="CollisionTop" type="CollisionShape2D" parent="ProjectileBorder"] +position = Vector2(320, -50) +rotation = 3.1415927 +shape = SubResource("WorldBoundaryShape2D_ltkyg") +one_way_collision = true + +[node name="CollisionBottom" type="CollisionShape2D" parent="ProjectileBorder"] +position = Vector2(320, 410) +shape = SubResource("WorldBoundaryShape2D_ltkyg") +one_way_collision = true + +[node name="CollisionLeft" type="CollisionShape2D" parent="ProjectileBorder"] +position = Vector2(-50, 180) +rotation = 1.5707964 +shape = SubResource("WorldBoundaryShape2D_ltkyg") +one_way_collision = true + +[node name="CollisionRight" type="CollisionShape2D" parent="ProjectileBorder"] +position = Vector2(690, 180) +rotation = -1.5707964 +shape = SubResource("WorldBoundaryShape2D_ltkyg") +one_way_collision = true diff --git a/project.godot b/project.godot index 4a149f7..aa35b52 100644 --- a/project.godot +++ b/project.godot @@ -346,7 +346,12 @@ previous_reactor_scheme={ [layer_names] -2d_physics/layer_1="Player" +2d_physics/layer_1="World" +2d_physics/layer_2="Player" +2d_physics/layer_3="Enemy" +2d_physics/layer_4="PlayerProjectile" +2d_physics/layer_5="EnemyProjectile" +2d_physics/layer_6="ProjectileBorder" [rendering]