diff --git a/README.md b/README.md index 26c5418..be30d9e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # Maze Generator made in Godot 4 uses TileMapLayers, -Also can played on Android with joysticks and Motion control. \ No newline at end of file +Also can played on Android with joysticks and Motion control. diff --git a/finish.gd b/finish.gd new file mode 100644 index 0000000..4d139d9 --- /dev/null +++ b/finish.gd @@ -0,0 +1,8 @@ +extends Sprite2D + + + + +func _on_area_2d_body_entered(body: Node2D) -> void: + if body.name == "Player": + get_tree().change_scene_to_file("res://end.tscn") diff --git a/generator.gd b/generator.gd index dda28f1..79b7752 100644 --- a/generator.gd +++ b/generator.gd @@ -1,55 +1,56 @@ extends Sprite2D -var maze_pos = Vector2i(0, 0) +var maze_pos = Vector2i(0, 0) # start position for the generator var dir_history = [] var directions = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT] -var tile_b = Vector2i(1, 0) # border # black -var tile_n = Vector2i(0, 0) # no visited # grey -var tile_v = Vector2i(0, 1) # visited # blue -var tile_s = Vector2i(1, 1) # visited and stuck # green + +var tile_n = Vector2i(0, 0) # no visited # dark blue +var tile_v = Vector2i(0, 1) # visited # light blue +var tile_s = Vector2i(1, 1) # visited and stuck # blue signal done @onready var Maze = $"../maze" func _ready() -> void: - Maze.set_cell(maze_pos,0,tile_v) + Maze.set_cell(maze_pos,0,tile_v) # set first cell as visited func get_neighbors(): - var dlist = [] - for dir in directions: - var next_pos = maze_pos + dir * 2 - if $"../maze".get_cell_atlas_coords(next_pos) == tile_n: - #$"../maze".set_cell(next_pos,0,tile_v) - #print(next_pos) - dlist.append(dir) - print(dlist) - - return dlist + "get surrounding not visited neighbors" + var dlist = [] + for dir in directions: + var next_pos = maze_pos + dir * 2 + if $"../maze".get_cell_atlas_coords(next_pos) == tile_n: + dlist.append(dir) + print(dlist) + + return dlist func _process(delta: float) -> void: - var dlist = get_neighbors() - if dlist.is_empty(): - if dir_history.is_empty(): - print("Maze generation completed") - done.emit() - set_process(false) - return - var removed_item = dir_history.pop_back() - $"../maze".set_cell(maze_pos-removed_item, 0, tile_s) - $"../maze".set_cell(maze_pos, 0, tile_s) - maze_pos = maze_pos - removed_item*2 - - else: - # 2.1 pick random neighbar - var dir = dlist.pick_random() - # Wand zwischen Zellen entfernen - $"../maze".set_cell(maze_pos + dir, 0, tile_v) - # add to stack - dir_history.append(dir) - # set new cell - maze_pos += dir * 2 - $"../maze".set_cell(maze_pos, 0, tile_v) - - position = 16 * maze_pos+Vector2i(1, 1) + var dlist = get_neighbors() # possible directions list + if dlist.is_empty(): + if dir_history.is_empty(): + print("Maze generation completed") + done.emit() + set_process(false) + return + + # remove current cell in history if stuck and go back + var removed_item = dir_history.pop_back() + $"../maze".set_cell(maze_pos-removed_item, 0, tile_s) + $"../maze".set_cell(maze_pos, 0, tile_s) + maze_pos = maze_pos - removed_item*2 + + else: + # 2.1 pick random neighbar + var dir = dlist.pick_random() + # remove walls between the cells + $"../maze".set_cell(maze_pos + dir, 0, tile_v) + # add direction to history + dir_history.append(dir) + # set new cell + maze_pos += dir * 2 + $"../maze".set_cell(maze_pos, 0, tile_v) + + position = 16 * maze_pos+Vector2i(1, 1) # just set sprite position diff --git a/main.tscn b/main.tscn index 734d656..8272109 100644 --- a/main.tscn +++ b/main.tscn @@ -5,7 +5,7 @@ [ext_resource type="Texture2D" uid="uid://d11d3alxengr0" path="res://assets/maze_tileset.png" id="3_2kohv"] [ext_resource type="Script" path="res://player.gd" id="4_8cre8"] [ext_resource type="Texture2D" uid="uid://b27nu8tq38o1h" path="res://assets/blo.png" id="5_ikxa5"] -[ext_resource type="Script" path="res://ziel.gd" id="6_0cii3"] +[ext_resource type="Script" path="res://finish.gd" id="6_0cii3"] [ext_resource type="PackedScene" uid="uid://dm6q0abakck7u" path="res://stick/joystick.tscn" id="6_fsx4f"] [ext_resource type="Texture2D" uid="uid://c8shyusyohh1u" path="res://assets/button.png" id="7_bd03u"] [ext_resource type="Texture2D" uid="uid://cwuboikffi42n" path="res://assets/butto_toggledn.png" id="8_p48aj"] @@ -80,14 +80,14 @@ position = Vector2(-1, -1) scale = Vector2(0.1, 0.1) texture = ExtResource("1_w8mkk") -[node name="Ziel" type="Sprite2D" parent="."] +[node name="FInish" type="Sprite2D" parent="."] position = Vector2(1136, 624) texture = ExtResource("5_ikxa5") script = ExtResource("6_0cii3") -[node name="Area2D" type="Area2D" parent="Ziel"] +[node name="Area2D" type="Area2D" parent="FInish"] -[node name="CollisionShape2D" type="CollisionShape2D" parent="Ziel/Area2D"] +[node name="CollisionShape2D" type="CollisionShape2D" parent="FInish/Area2D"] shape = SubResource("RectangleShape2D_is8wb") [node name="CanvasLayer" type="CanvasLayer" parent="."] @@ -116,5 +116,5 @@ texture_pressed = ExtResource("8_p48aj") stretch_mode = 5 [connection signal="done" from="generator" to="Player" method="_on_generator_done"] -[connection signal="body_entered" from="Ziel/Area2D" to="Ziel" method="_on_area_2d_body_entered"] +[connection signal="body_entered" from="FInish/Area2D" to="FInish" method="_on_area_2d_body_entered"] [connection signal="toggled" from="CanvasLayer/stick/Button" to="Player" method="_on_button_toggled"] diff --git a/player.gd b/player.gd index 4262baa..c4e36f3 100644 --- a/player.gd +++ b/player.gd @@ -6,23 +6,23 @@ var direction: Vector2 var button_toggled: bool var inp :Vector3 func _physics_process(delta: float) -> void: - if button_toggled: - inp = Input.get_accelerometer() - direction = Vector2(inp.x,-inp.y) - else: - if $"../CanvasLayer/stick/Joystick".posVector: - direction = $"../CanvasLayer/stick/Joystick".posVector - else: - direction = Input.get_vector("ui_left", "ui_right","ui_up","ui_down") - - velocity = direction*SPEED - - move_and_slide() + if button_toggled: + inp = Input.get_accelerometer() + direction = Vector2(inp.x,-inp.y) + else: + if $"../CanvasLayer/stick/Joystick".posVector: + direction = $"../CanvasLayer/stick/Joystick".posVector + else: + direction = Input.get_vector("ui_left", "ui_right","ui_up","ui_down") + + velocity = direction*SPEED + + move_and_slide() func _on_generator_done() -> void: - $Camera2D.enabled = true + $Camera2D.enabled = true func _on_button_toggled(toggled_on: bool) -> void: - button_toggled = toggled_on + button_toggled = toggled_on diff --git a/stick/Knob.gd b/stick/Knob.gd index 3e88c13..68ace23 100644 --- a/stick/Knob.gd +++ b/stick/Knob.gd @@ -8,31 +8,31 @@ var pressing = false var deadzone = 15 func _ready(): - #deadzone = parent.deadzone - maxLength *= parent.scale.x + #deadzone = parent.deadzone + maxLength *= parent.scale.x func _process(delta): - if pressing: - if get_global_mouse_position().distance_to(parent.global_position) <= maxLength: - global_position = get_global_mouse_position() - else: - var angle = parent.global_position.angle_to_point(get_global_mouse_position()) - global_position.x = parent.global_position.x + cos(angle)*maxLength - global_position.y = parent.global_position.y + sin(angle)*maxLength - calculateVector() - else: - global_position = lerp(global_position, parent.global_position, delta*50) - parent.posVector = Vector2(0,0) - + if pressing: + if get_global_mouse_position().distance_to(parent.global_position) <= maxLength: + global_position = get_global_mouse_position() + else: + var angle = parent.global_position.angle_to_point(get_global_mouse_position()) + global_position.x = parent.global_position.x + cos(angle)*maxLength + global_position.y = parent.global_position.y + sin(angle)*maxLength + calculateVector() + else: + global_position = lerp(global_position, parent.global_position, delta*50) + parent.posVector = Vector2(0,0) + func calculateVector(): - if abs((global_position.x - parent.global_position.x)) >= deadzone: - parent.posVector.x = (global_position.x - parent.global_position.x)/maxLength - if abs((global_position.y - parent.global_position.y)) >= deadzone: - parent.posVector.y = (global_position.y - parent.global_position.y)/maxLength - + if abs((global_position.x - parent.global_position.x)) >= deadzone: + parent.posVector.x = (global_position.x - parent.global_position.x)/maxLength + if abs((global_position.y - parent.global_position.y)) >= deadzone: + parent.posVector.y = (global_position.y - parent.global_position.y)/maxLength + func _on_button_button_down(): - pressing = true + pressing = true func _on_button_button_up(): - pressing = false + pressing = false diff --git a/stick/joystick.gd b/stick/joystick.gd index 686de3b..ade42d9 100644 --- a/stick/joystick.gd +++ b/stick/joystick.gd @@ -4,5 +4,5 @@ var posVector: Vector2 @export var deadzone = 15 func _ready() -> void: - if not DisplayServer.is_touchscreen_available(): - set_process(false) + if not DisplayServer.is_touchscreen_available(): + set_process(false) diff --git a/ziel.gd b/ziel.gd deleted file mode 100644 index 15fa9fb..0000000 --- a/ziel.gd +++ /dev/null @@ -1,8 +0,0 @@ -extends Sprite2D - - - - -func _on_area_2d_body_entered(body: Node2D) -> void: - if body.name == "Player": - get_tree().change_scene_to_file("res://end.tscn")