fixing comments in code
This commit is contained in:
parent
7ea2915f5d
commit
4d48ced249
8 changed files with 92 additions and 91 deletions
8
finish.gd
Normal file
8
finish.gd
Normal 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")
|
75
generator.gd
75
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)
|
||||
"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
|
||||
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
|
||||
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
|
||||
|
||||
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)
|
||||
# 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
|
||||
|
||||
position = 16 * maze_pos+Vector2i(1, 1)
|
||||
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
|
||||
|
|
10
main.tscn
10
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"]
|
||||
|
|
24
player.gd
24
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")
|
||||
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
|
||||
velocity = direction*SPEED
|
||||
|
||||
move_and_slide()
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
8
ziel.gd
8
ziel.gd
|
@ -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")
|
Loading…
Add table
Reference in a new issue