fixing comments in code

This commit is contained in:
megamichi 2025-02-28 23:05:41 +01:00
parent 7ea2915f5d
commit 4d48ced249
8 changed files with 92 additions and 91 deletions

View file

@ -1,4 +1,4 @@
# Maze Generator made in Godot 4 # Maze Generator made in Godot 4
uses TileMapLayers, uses TileMapLayers,
Also can played on Android with joysticks and Motion control. Also can played on Android with joysticks and Motion control.

8
finish.gd Normal file
View file

@ -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")

View file

@ -1,55 +1,56 @@
extends Sprite2D extends Sprite2D
var maze_pos = Vector2i(0, 0) var maze_pos = Vector2i(0, 0) # start position for the generator
var dir_history = [] var dir_history = []
var directions = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT] 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_n = Vector2i(0, 0) # no visited # dark blue
var tile_v = Vector2i(0, 1) # visited # blue var tile_v = Vector2i(0, 1) # visited # light blue
var tile_s = Vector2i(1, 1) # visited and stuck # green var tile_s = Vector2i(1, 1) # visited and stuck # blue
signal done signal done
@onready var Maze = $"../maze" @onready var Maze = $"../maze"
func _ready() -> void: 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(): func get_neighbors():
var dlist = [] "get surrounding not visited neighbors"
for dir in directions: var dlist = []
var next_pos = maze_pos + dir * 2 for dir in directions:
if $"../maze".get_cell_atlas_coords(next_pos) == tile_n: var next_pos = maze_pos + dir * 2
#$"../maze".set_cell(next_pos,0,tile_v) if $"../maze".get_cell_atlas_coords(next_pos) == tile_n:
#print(next_pos) dlist.append(dir)
dlist.append(dir) print(dlist)
print(dlist)
return dlist
return dlist
func _process(delta: float) -> void: func _process(delta: float) -> void:
var dlist = get_neighbors() var dlist = get_neighbors() # possible directions list
if dlist.is_empty(): if dlist.is_empty():
if dir_history.is_empty(): if dir_history.is_empty():
print("Maze generation completed") print("Maze generation completed")
done.emit() done.emit()
set_process(false) set_process(false)
return return
var removed_item = dir_history.pop_back()
$"../maze".set_cell(maze_pos-removed_item, 0, tile_s) # remove current cell in history if stuck and go back
$"../maze".set_cell(maze_pos, 0, tile_s) var removed_item = dir_history.pop_back()
maze_pos = maze_pos - removed_item*2 $"../maze".set_cell(maze_pos-removed_item, 0, tile_s)
$"../maze".set_cell(maze_pos, 0, tile_s)
else: maze_pos = maze_pos - removed_item*2
# 2.1 pick random neighbar
var dir = dlist.pick_random() else:
# Wand zwischen Zellen entfernen # 2.1 pick random neighbar
$"../maze".set_cell(maze_pos + dir, 0, tile_v) var dir = dlist.pick_random()
# add to stack # remove walls between the cells
dir_history.append(dir) $"../maze".set_cell(maze_pos + dir, 0, tile_v)
# set new cell # add direction to history
maze_pos += dir * 2 dir_history.append(dir)
$"../maze".set_cell(maze_pos, 0, tile_v) # set new cell
maze_pos += dir * 2
position = 16 * maze_pos+Vector2i(1, 1) $"../maze".set_cell(maze_pos, 0, tile_v)
position = 16 * maze_pos+Vector2i(1, 1) # just set sprite position

View file

@ -5,7 +5,7 @@
[ext_resource type="Texture2D" uid="uid://d11d3alxengr0" path="res://assets/maze_tileset.png" id="3_2kohv"] [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="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="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="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://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"] [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) scale = Vector2(0.1, 0.1)
texture = ExtResource("1_w8mkk") texture = ExtResource("1_w8mkk")
[node name="Ziel" type="Sprite2D" parent="."] [node name="FInish" type="Sprite2D" parent="."]
position = Vector2(1136, 624) position = Vector2(1136, 624)
texture = ExtResource("5_ikxa5") texture = ExtResource("5_ikxa5")
script = ExtResource("6_0cii3") 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") shape = SubResource("RectangleShape2D_is8wb")
[node name="CanvasLayer" type="CanvasLayer" parent="."] [node name="CanvasLayer" type="CanvasLayer" parent="."]
@ -116,5 +116,5 @@ texture_pressed = ExtResource("8_p48aj")
stretch_mode = 5 stretch_mode = 5
[connection signal="done" from="generator" to="Player" method="_on_generator_done"] [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"] [connection signal="toggled" from="CanvasLayer/stick/Button" to="Player" method="_on_button_toggled"]

View file

@ -6,23 +6,23 @@ var direction: Vector2
var button_toggled: bool var button_toggled: bool
var inp :Vector3 var inp :Vector3
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
if button_toggled: if button_toggled:
inp = Input.get_accelerometer() inp = Input.get_accelerometer()
direction = Vector2(inp.x,-inp.y) direction = Vector2(inp.x,-inp.y)
else: else:
if $"../CanvasLayer/stick/Joystick".posVector: if $"../CanvasLayer/stick/Joystick".posVector:
direction = $"../CanvasLayer/stick/Joystick".posVector direction = $"../CanvasLayer/stick/Joystick".posVector
else: else:
direction = Input.get_vector("ui_left", "ui_right","ui_up","ui_down") direction = Input.get_vector("ui_left", "ui_right","ui_up","ui_down")
velocity = direction*SPEED velocity = direction*SPEED
move_and_slide() move_and_slide()
func _on_generator_done() -> void: func _on_generator_done() -> void:
$Camera2D.enabled = true $Camera2D.enabled = true
func _on_button_toggled(toggled_on: bool) -> void: func _on_button_toggled(toggled_on: bool) -> void:
button_toggled = toggled_on button_toggled = toggled_on

View file

@ -8,31 +8,31 @@ var pressing = false
var deadzone = 15 var deadzone = 15
func _ready(): func _ready():
#deadzone = parent.deadzone #deadzone = parent.deadzone
maxLength *= parent.scale.x maxLength *= parent.scale.x
func _process(delta): func _process(delta):
if pressing: if pressing:
if get_global_mouse_position().distance_to(parent.global_position) <= maxLength: if get_global_mouse_position().distance_to(parent.global_position) <= maxLength:
global_position = get_global_mouse_position() global_position = get_global_mouse_position()
else: else:
var angle = parent.global_position.angle_to_point(get_global_mouse_position()) var angle = parent.global_position.angle_to_point(get_global_mouse_position())
global_position.x = parent.global_position.x + cos(angle)*maxLength global_position.x = parent.global_position.x + cos(angle)*maxLength
global_position.y = parent.global_position.y + sin(angle)*maxLength global_position.y = parent.global_position.y + sin(angle)*maxLength
calculateVector() calculateVector()
else: else:
global_position = lerp(global_position, parent.global_position, delta*50) global_position = lerp(global_position, parent.global_position, delta*50)
parent.posVector = Vector2(0,0) parent.posVector = Vector2(0,0)
func calculateVector(): func calculateVector():
if abs((global_position.x - parent.global_position.x)) >= deadzone: if abs((global_position.x - parent.global_position.x)) >= deadzone:
parent.posVector.x = (global_position.x - parent.global_position.x)/maxLength parent.posVector.x = (global_position.x - parent.global_position.x)/maxLength
if abs((global_position.y - parent.global_position.y)) >= deadzone: if abs((global_position.y - parent.global_position.y)) >= deadzone:
parent.posVector.y = (global_position.y - parent.global_position.y)/maxLength parent.posVector.y = (global_position.y - parent.global_position.y)/maxLength
func _on_button_button_down(): func _on_button_button_down():
pressing = true pressing = true
func _on_button_button_up(): func _on_button_button_up():
pressing = false pressing = false

View file

@ -4,5 +4,5 @@ var posVector: Vector2
@export var deadzone = 15 @export var deadzone = 15
func _ready() -> void: func _ready() -> void:
if not DisplayServer.is_touchscreen_available(): if not DisplayServer.is_touchscreen_available():
set_process(false) set_process(false)

View file

@ -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")