From cbb667117356043f026e7d041e767a7a4c692ffa Mon Sep 17 00:00:00 2001 From: rom Date: Mon, 26 Apr 2021 17:55:15 +0200 Subject: bulletmaster3000 --- scenes/levels/pixel/PixelDefender.gd | 73 ++++++++++++++++++++++++++++++++++ scenes/levels/pixel/PixelDefender.tscn | 17 ++++++++ scenes/levels/pixel/bullet.gd | 13 ++++++ scenes/levels/pixel/bullet.tscn | 18 +++++++++ scenes/levels/pixel/spaceship.gd | 2 + scenes/levels/pixel/spaceship.tscn | 14 +++++++ 6 files changed, 137 insertions(+) create mode 100644 scenes/levels/pixel/PixelDefender.gd create mode 100644 scenes/levels/pixel/PixelDefender.tscn create mode 100644 scenes/levels/pixel/bullet.gd create mode 100644 scenes/levels/pixel/bullet.tscn create mode 100644 scenes/levels/pixel/spaceship.gd create mode 100644 scenes/levels/pixel/spaceship.tscn (limited to 'scenes/levels/pixel') diff --git a/scenes/levels/pixel/PixelDefender.gd b/scenes/levels/pixel/PixelDefender.gd new file mode 100644 index 0000000..2fc2e2d --- /dev/null +++ b/scenes/levels/pixel/PixelDefender.gd @@ -0,0 +1,73 @@ +extends Level + + +const _bullet_scene = preload("res://scenes/levels/pixel/bullet.tscn") +const _enemy_scene = preload("res://scenes/levels/pixel/spaceship.tscn") + + +func _new_bullet(velocity, team) -> Bullet: + var bullet = _bullet_scene.instance() + bullet.velocity = velocity + bullet.team = team + $BulletHolder.add_child(bullet) + return bullet + +func _new_enemy(): + var enemy = _enemy_scene.instance() + enemy.position = Vector2(500 + randi() % 80, (randf() * 600) - 350) + enemy.scale.x *= -1 + enemy.team = Bullet.CHAOS + $Enemies.add_child(enemy) + enemy.connect("area_shape_entered", self, "_on_enemy_area_shape_entered", [enemy]) + return enemy + +func start(): + .start() + var enemy_count = 5 + if Settings.difficulty == Settings.Difficulty.HARD: + enemy_count += 3 + for i in range(5): + _new_enemy() + +var t = 0 +var bullet_timer = 1.5 +var enemy_speed = 1 + +func _process(delta): + if not running: + return + t += delta + + while t > bullet_timer: + t -= bullet_timer + var b = _new_bullet(10, Bullet.ORDER) + b.position = $spaceship.position + Vector2(16, 0) + for e in $Enemies.get_children(): + b = _new_bullet(10, Bullet.CHAOS) + b.position = e.position - Vector2(16, 0) + if $Enemies.get_child_count() == 0: + finish_level() + return + for e in $Enemies.get_children(): + e.position -= Vector2(enemy_speed, 0) + if e.position.x < $spaceship.position.x: + lose_all_hearts() + return + + $spaceship.position.y = max(0, min(get_viewport().get_mouse_position().y, get_viewport_rect().size.y)) - 360 + +func _on_enemy_area_shape_entered(area_id, area, area_shape, local_shape, e): + if area is Bullet and area.team == Bullet.CHAOS: + return + if area is Bullet: + area.queue_free() + elif area.team == Bullet.CHAOS: + return + e.queue_free() + +func _on_spaceship_area_shape_entered(area_id, area, area_shape, local_shape): + if area is Bullet: + if area.team == Bullet.CHAOS: + lose_heart() + else: + lose_all_hearts() diff --git a/scenes/levels/pixel/PixelDefender.tscn b/scenes/levels/pixel/PixelDefender.tscn new file mode 100644 index 0000000..29c7674 --- /dev/null +++ b/scenes/levels/pixel/PixelDefender.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://scenes/levels/pixel/PixelDefender.gd" type="Script" id=1] +[ext_resource path="res://scenes/levels/pixel/spaceship.tscn" type="PackedScene" id=2] + +[node name="Node2D" type="Node2D"] +position = Vector2( 640, 360 ) +script = ExtResource( 1 ) + +[node name="BulletHolder" type="Node2D" parent="."] + +[node name="spaceship" parent="." instance=ExtResource( 2 )] +position = Vector2( -432.818, -39.5735 ) + +[node name="Enemies" type="Node2D" parent="."] + +[connection signal="area_shape_entered" from="spaceship" to="." method="_on_spaceship_area_shape_entered"] diff --git a/scenes/levels/pixel/bullet.gd b/scenes/levels/pixel/bullet.gd new file mode 100644 index 0000000..865af35 --- /dev/null +++ b/scenes/levels/pixel/bullet.gd @@ -0,0 +1,13 @@ +extends Area2D + +class_name Bullet + +enum {ORDER, CHAOS} +var velocity = 10 +var team = ORDER + +func _process(delta): + if team == ORDER: + position.x += velocity + else: + position.x -= velocity diff --git a/scenes/levels/pixel/bullet.tscn b/scenes/levels/pixel/bullet.tscn new file mode 100644 index 0000000..10ff51d --- /dev/null +++ b/scenes/levels/pixel/bullet.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://textures/bullet.png" type="Texture" id=1] +[ext_resource path="res://scenes/levels/pixel/bullet.gd" type="Script" id=2] + +[sub_resource type="CircleShape2D" id=1] +radius = 5.58358 + +[node name="Bullet" type="Area2D"] +script = ExtResource( 2 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( -0.148651, 0.297302 ) +shape = SubResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +position = Vector2( 0.297302, -0.148651 ) +texture = ExtResource( 1 ) diff --git a/scenes/levels/pixel/spaceship.gd b/scenes/levels/pixel/spaceship.gd new file mode 100644 index 0000000..3c39665 --- /dev/null +++ b/scenes/levels/pixel/spaceship.gd @@ -0,0 +1,2 @@ +extends Area2D +var team = Bullet.ORDER diff --git a/scenes/levels/pixel/spaceship.tscn b/scenes/levels/pixel/spaceship.tscn new file mode 100644 index 0000000..1247f56 --- /dev/null +++ b/scenes/levels/pixel/spaceship.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://textures/spaceship.png" type="Texture" id=1] +[ext_resource path="res://scenes/levels/pixel/spaceship.gd" type="Script" id=2] + +[node name="spaceship" type="Area2D"] +position = Vector2( 50, 360 ) +script = ExtResource( 2 ) + +[node name="spaceship" type="Sprite" parent="."] +texture = ExtResource( 1 ) + +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] +polygon = PoolVector2Array( 16, 0, -16, 16, -16, -16 ) -- cgit