Skip to content

Commit

Permalink
introduce collision layers
Browse files Browse the repository at this point in the history
change player to characterbody2d
  • Loading branch information
seriousdev-gh committed Jul 19, 2024
1 parent 7e6c88c commit ae67590
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 123 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Несколько классических игр для изучения игрового движка Godot.

[Play](https://seriousdev-gh.github.io/godot_simple_games/)
[Play](https://seriousdev-gh.github.io/godot_simple_games/)

### Art
- https://opengameart.org/content/space-shooter-redux
Binary file added assets/playerLife1_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions assets/playerLife1_red.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://c1a6bmwaojung"
path="res://.godot/imported/playerLife1_red.png-0096b1eca825fbaff431327f77b406b2.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://assets/playerLife1_red.png"
dest_files=["res://.godot/imported/playerLife1_red.png-0096b1eca825fbaff431327f77b406b2.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
7 changes: 5 additions & 2 deletions games/space_shooter/enemy_1.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ func _process(delta):

func logic():
await get_tree().create_timer(1).timeout
if get_tree().get_current_scene().is_game_over:
return

var projectile = projectile_scene.instantiate()
var player = get_tree().get_current_scene().get_node("Player")

projectile.position = position
projectile.look_at(player.position)
get_tree().get_current_scene().add_child(projectile)
Expand All @@ -34,6 +38,5 @@ func kill():


func _on_enemy_area_entered(area):
print("Entered: " + area.name)
if area.name == 'PlayerProjectile':
if area.name == 'PlayerProjectile' || area.name == 'PlayerArea':
kill()
4 changes: 3 additions & 1 deletion games/space_shooter/enemy_1.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
[ext_resource type="PackedScene" uid="uid://bxw41m6b8658i" path="res://games/space_shooter/enemy_laser_1.tscn" id="3_hc0k5"]
[ext_resource type="PackedScene" uid="uid://b1jltndmr23o0" path="res://scenes/explosion.tscn" id="5_kkc2m"]

[node name="Node2D" type="Node2D"]
[node name="Enemy1" type="Node2D"]
script = ExtResource("2_8k6oi")
particles_scene = ExtResource("5_kkc2m")
projectile_scene = ExtResource("3_hc0k5")

[node name="Enemy" type="Area2D" parent="."]
position = Vector2(0, 5)
scale = Vector2(0.5, 0.5)
collision_layer = 4
collision_mask = 3

[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Enemy"]
polygon = PackedVector2Array(42, -38, 42, 12, 10, 36, -10, 36, -42, 12, -42, -38)
Expand Down
5 changes: 2 additions & 3 deletions games/space_shooter/enemy_laser_1.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ extends Node2D
func _process(delta):
translate(Vector2.from_angle(rotation) * speed * delta)

func _on_area_2d_area_entered(area):
if area.name != "EnemyArea":
queue_free()
func _on_enemy_projectile_area_entered(_area):
queue_free()
3 changes: 3 additions & 0 deletions games/space_shooter/enemy_laser_1.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ texture = ExtResource("1_etjbt")
[node name="EnemyProjectile" type="Area2D" parent="LaserRed16"]
position = Vector2(0, -6.5)
scale = Vector2(0.5, 0.5)
collision_layer = 8

[node name="CollisionShape2D" type="CollisionShape2D" parent="LaserRed16/EnemyProjectile"]
position = Vector2(0, 10)
shape = SubResource("CapsuleShape2D_g2fpe")

[connection signal="area_entered" from="LaserRed16/EnemyProjectile" to="." method="_on_enemy_projectile_area_entered"]
36 changes: 30 additions & 6 deletions games/space_shooter/game.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
extends Node2D

@export var enemy_scenes : Array[PackedScene]
var levels : Array
var is_game_over = false

func seconds(secs):
await get_tree().create_timer(secs).timeout

func _ready():
seed(123456)
Expand All @@ -13,18 +16,39 @@ func setup_level1():
var enemy = enemy_scenes[0].instantiate()
enemy.position = line_random_point($Spawners/Top)
add_child(enemy)
await get_tree().create_timer(0.5).timeout
await get_tree().create_timer(3).timeout
await seconds(0.5)
await seconds(3)
if is_game_over:
return

print("wave 2")
for i in 5:
for i in 10:
var enemy = enemy_scenes[0].instantiate()
enemy.position = line_random_point($Spawners/Top)
add_child(enemy)
await get_tree().create_timer(0.3).timeout
await get_tree().create_timer(3).timeout
await seconds(0.3)
await seconds(3)
if is_game_over:
return

func line_random_point(line) -> Vector2:
var p1 = line.get_point_position(0)
var p2 = line.get_point_position(1)
return line.position + Vector2(randf_range(p1.x, p2.x),randf_range(p1.y, p2.y))


func _on_player_game_over():
is_game_over = true
$Player.set_process(false)
$Background/AnimationPlayer.stop()

# is there a better way to free subtree of nodes where `node` is located?
func free_node_spawned_in_game(node):
var immediate_parent = node.get_parent()
while immediate_parent.get_parent().name != 'Game':
immediate_parent = immediate_parent.get_parent()
immediate_parent.queue_free()

# destroy anything that leaves game area
func _on_free_on_exit_zone_area_exited(area):
free_node_spawned_in_game(area)
113 changes: 51 additions & 62 deletions games/space_shooter/game.tscn
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
[gd_scene load_steps=16 format=3 uid="uid://bjfcqwlc875vd"]
[gd_scene load_steps=14 format=3 uid="uid://bjfcqwlc875vd"]

[ext_resource type="PackedScene" uid="uid://n2q8ah2ddl06" path="res://games/space_shooter/player.tscn" id="1_2e4hq"]
[ext_resource type="Script" path="res://games/space_shooter/game.gd" id="1_rbw86"]
[ext_resource type="PackedScene" uid="uid://ccgesfkgw36wd" path="res://games/space_shooter/enemy_1.tscn" id="2_1be6a"]
[ext_resource type="Texture2D" uid="uid://fv34cx08eivf" path="res://assets/background/darkPurple.png" id="3_34xym"]
[ext_resource type="Texture2D" uid="uid://c1a6bmwaojung" path="res://assets/playerLife1_red.png" id="5_uv1ag"]

[sub_resource type="Animation" id="Animation_5uc25"]
resource_name = "new_animation"
length = 5.0
loop_mode = 1
[sub_resource type="RectangleShape2D" id="RectangleShape2D_6ttr0"]
size = Vector2(1759, 1083)

[sub_resource type="Animation" id="Animation_ivt6l"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Polygon2D:texture_offset")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 5),
"transitions": PackedFloat32Array(1, 1),
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, 0), Vector2(0, -256)]
"values": [Vector2(0, 0)]
}

[sub_resource type="Animation" id="Animation_ivt6l"]
length = 0.001
[sub_resource type="Animation" id="Animation_5uc25"]
resource_name = "new_animation"
length = 5.0
loop_mode = 1
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Polygon2D:texture_offset")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"times": PackedFloat32Array(0, 5),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Vector2(0, 0)]
"values": [Vector2(0, 0), Vector2(0, -256)]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_1rsic"]
Expand All @@ -44,32 +48,29 @@ _data = {
}

[sub_resource type="RectangleShape2D" id="RectangleShape2D_3gjyp"]
size = Vector2(21, 678)
size = Vector2(72, 678)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_4p6ip"]
size = Vector2(24, 684)
size = Vector2(81, 684)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_x03u4"]
size = Vector2(1181, 16)
size = Vector2(1181, 53)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_cy65t"]
size = Vector2(1177, 20)

[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_pg8gc"]
normal = Vector2(0.999986, -0.00529093)

[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_th8r8"]
normal = Vector2(-0.999957, 0.00930192)

[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_56evl"]
normal = Vector2(0, 1)

[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_qgsuh"]
size = Vector2(1177, 59)

[node name="Game" type="Node2D"]
script = ExtResource("1_rbw86")
enemy_scenes = Array[PackedScene]([ExtResource("2_1be6a")])

[node name="FreeOnExitZone" type="Area2D" parent="."]
collision_layer = 0
collision_mask = 255

[node name="CollisionShape2D" type="CollisionShape2D" parent="FreeOnExitZone"]
position = Vector2(-10.5, 5.5)
shape = SubResource("RectangleShape2D_6ttr0")

[node name="Background" type="Node2D" parent="."]

[node name="Polygon2D" type="Polygon2D" parent="Background"]
Expand All @@ -88,61 +89,49 @@ autoplay = "new_animation"
[node name="Player" parent="." instance=ExtResource("1_2e4hq")]
position = Vector2(0, 267)

[node name="BoundaryLeft" type="Area2D" parent="."]
[node name="BoundaryLeft" type="StaticBody2D" parent="."]
position = Vector2(-579, 0)
collision_layer = 16

[node name="CollisionShape2D" type="CollisionShape2D" parent="BoundaryLeft"]
position = Vector2(-11.5, 4)
position = Vector2(-37, 4)
shape = SubResource("RectangleShape2D_3gjyp")

[node name="BoundaryRight" type="Area2D" parent="."]
[node name="BoundaryRight" type="StaticBody2D" parent="."]
position = Vector2(576, 2)
collision_layer = 16

[node name="CollisionShape2D" type="CollisionShape2D" parent="BoundaryRight"]
position = Vector2(16, 3)
position = Vector2(44.5, 3)
shape = SubResource("RectangleShape2D_4p6ip")

[node name="BoundaryUp" type="Area2D" parent="."]
[node name="BoundaryUp" type="StaticBody2D" parent="."]
position = Vector2(0, -329)
collision_layer = 16

[node name="CollisionShape2D" type="CollisionShape2D" parent="BoundaryUp"]
position = Vector2(-0.5, -2)
position = Vector2(0, -25.5)
shape = SubResource("RectangleShape2D_x03u4")

[node name="BoundaryDown" type="Area2D" parent="."]
[node name="BoundaryDown" type="StaticBody2D" parent="."]
position = Vector2(0, 326)
collision_layer = 16

[node name="CollisionShape2D" type="CollisionShape2D" parent="BoundaryDown"]
position = Vector2(1, 10)
position = Vector2(1, 29.5)
shape = SubResource("RectangleShape2D_cy65t")

[node name="KillzoneLeft" type="Area2D" parent="."]
position = Vector2(-784, 0)

[node name="CollisionShape2D" type="CollisionShape2D" parent="KillzoneLeft"]
shape = SubResource("WorldBoundaryShape2D_pg8gc")

[node name="KillzoneRight" type="Area2D" parent="."]
position = Vector2(759, 2)

[node name="CollisionShape2D" type="CollisionShape2D" parent="KillzoneRight"]
shape = SubResource("WorldBoundaryShape2D_th8r8")

[node name="KillzoneTop" type="Area2D" parent="."]
position = Vector2(0, -626)

[node name="CollisionShape2D" type="CollisionShape2D" parent="KillzoneTop"]
position = Vector2(0, -58)
shape = SubResource("WorldBoundaryShape2D_56evl")

[node name="KillzoneBottom" type="Area2D" parent="."]
position = Vector2(0, 627)

[node name="CollisionShape2D" type="CollisionShape2D" parent="KillzoneBottom"]
shape = SubResource("WorldBoundaryShape2D_qgsuh")

[node name="Spawners" type="Node" parent="."]

[node name="Top" type="Line2D" parent="Spawners"]
position = Vector2(0, -585)
position = Vector2(0, -444)
points = PackedVector2Array(-484, 9, 499, 10)

[node name="Health" type="Node2D" parent="."]

[node name="PlayerLife1Red" type="Sprite2D" parent="Health"]
position = Vector2(-543, -300)
texture = ExtResource("5_uv1ag")

[connection signal="area_exited" from="FreeOnExitZone" to="." method="_on_free_on_exit_zone_area_exited"]
[connection signal="game_over" from="Player" to="." method="_on_player_game_over"]
Loading

0 comments on commit ae67590

Please sign in to comment.