Skip to content

Commit

Permalink
create rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
myin142 committed Jun 30, 2024
1 parent eee3b6e commit 767449f
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 49 deletions.
26 changes: 26 additions & 0 deletions godot/src/GridMapUI.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class_name GridMapUI
extends Control

@export var room_manager: GridRoomMap

func _ready():
room_manager.changed.connect(func(): update())

func update():
queue_redraw()

func _draw():
for room in room_manager.rooms:
var color = Color.WHITE
if room == room_manager.boss_room:
color = Color.RED
elif room in room_manager.items:
color = Color.GREEN
elif room == room_manager.coord:
color = Color.BLUE
elif room == room_manager.spawn:
color = Color.ORANGE
elif room in room_manager.cleared:
color = Color.GRAY

draw_circle(Vector2(room) * Vector2(5, 5), 2, color)
112 changes: 112 additions & 0 deletions godot/src/GridRoomMap.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
class_name GridRoomMap
extends Node

signal changed()
signal generated()

const GROUP = "GridRoomMap"
const DIRS = [Vector2i.LEFT, Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN]

@export var map_size := Vector2i(10, 10)
@export var iterations := 30
@export var item_room_chance := 0.1

@onready var spawn: Vector2i = floor(map_size / 2)
@onready var cleared = [spawn]
@onready var coord: Vector2i = spawn:
set(v):
if not coord in rooms:
print("Invalid move to %s" % coord)
return

coord = v
changed.emit()

var boss_room: Vector2i
var pre_boss_room: Vector2i
var rooms = {}
var items = []

func _ready():
add_to_group(GROUP)

func generate():
_create_rooms()
_mark_item_rooms()
_mark_boss_room()
self.coord = spawn
generated.emit()

func is_item_room():
return coord in items

func is_boss_room():
return coord == boss_room

func is_pre_boss_room():
return coord == pre_boss_room

func is_enemy_room():
return not is_item_room() and not is_boss_room() and not is_pre_boss_room()

func is_room_cleared(c: Vector2i):
return c in cleared or is_item_room() or is_pre_boss_room()

func set_room_cleared(c):
if not is_room_cleared(c):
cleared.append(c)

func get_room(c = coord):
return rooms[c]

func _mark_item_rooms():
for room in rooms:
var close_items = items.filter(func(p): return Vector2(abs(p - room)).length() <= 2)
if randf() < item_room_chance and close_items.is_empty():
items.append(room)

func _mark_boss_room():
var x = _get_random_boss_room()
if x.is_empty():
return

pre_boss_room = x[0]
boss_room = x[1]
rooms[boss_room] = {}

func _get_random_boss_room():
var room_keys = rooms.keys()
room_keys.shuffle()

for room in room_keys:
for dir in _get_unlinked_dirs(room):
var pos = room + dir
if get_linked_dirs(pos).size() == 1 and not pos in items:
return [room, pos]

return []

func _get_unlinked_dirs(pos: Vector2i) -> Array:
return _get_available_dirs(pos).filter(func(d): return not (pos + d) in rooms)

func get_linked_dirs(pos: Vector2i = coord) -> Array:
return _get_available_dirs(pos).filter(func(d): return (pos + d) in rooms)

func _get_available_dirs(pos: Vector2i):
return DIRS.duplicate().filter(func(d): return not _is_outside_map(pos + d))

func _create_rooms():
var start = spawn
for i in range(iterations):
rooms[start] = {}

var available_dirs = _get_available_dirs(start)
if available_dirs.is_empty(): continue

var dir = available_dirs.pick_random()
start += dir

func _is_outside_map(pos: Vector2i):
var start = Vector2i.ZERO
var end = map_size
return pos.x < start.x or pos.x > end.x or pos.y < start.y or pos.y > end.y
2 changes: 1 addition & 1 deletion godot/src/base-system/combat/HitFlash2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extends Node
@export var node: Node2D

func flash():
if node and node.material:
if node and node.material and is_inside_tree():
var mat = node.material as ShaderMaterial
_set_hit_flash(mat, true)
await get_tree().create_timer(time).timeout
Expand Down
3 changes: 2 additions & 1 deletion godot/src/base-system/combat/Hurtbox.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ func damage(dmg: int, knockback_force: Vector2):
if frame_freeze and not health.is_dead():
frame_freeze.freeze()

get_tree().create_timer(invincible_time).timeout.connect(func(): invincible = false)
if is_inside_tree():
get_tree().create_timer(invincible_time).timeout.connect(func(): invincible = false)
return true
80 changes: 53 additions & 27 deletions godot/src/game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,90 @@ const ROOM = preload("res://src/props/room.tscn")
@onready var music_player = $MusicPlayer
@onready var mirror_viewport = $MirrorViewport
@onready var relative_remote_transform_2d = $Player/RelativeRemoteTransform2D
@onready var grid_room_map = $GridRoomMap

@export var enemy_value := 5.0
@export var enemy_kills := 5.0

@export var value_increase := 1.5
@export var kill_increase := 1.8

var previous_dir := Vector2.ZERO
var room_dirs := [Vector2.UP, Vector2.LEFT, Vector2.RIGHT, Vector2.DOWN]
var rooms = {}

var _logger = Logger.new("Game")

func _ready():
GameManager.game_start()
_setup_first_room.call_deferred()
music_player.play("RESET")
GameManager.mirrored.connect(func(mirror): music_player.play("mirror" if mirror else "normal"))

func _setup_first_room():
var room = _create_room()
add_child(room)
room.start(enemy_value, enemy_kills)

func _setup_next_room(current_room: Room):
var room = _create_room()
enemy_value *= value_increase
enemy_kills *= kill_increase
var dir = room_dirs.filter(func(d): return d != previous_dir).pick_random()
_logger.debug("Creating room in dir %s" % dir)
current_room.rooms[dir] = room
current_room.open_door(dir)
grid_room_map.generated.connect(func():
for coord in grid_room_map.rooms:
_create_room(coord)

print("Created rooms")
var room = rooms[grid_room_map.coord]
var pos = room.global_position
player.global_position = pos
cam.global_position = pos
relative_remote_transform_2d.center = pos
room.open_doors()

await get_tree().create_timer(0.5).timeout
cam.position_smoothing_enabled = true
)
grid_room_map.generate()

room.global_position = current_room.global_position + dir * room.get_size()
add_child.call_deferred(room)
#func _setup_first_room():
#var room = _create_room()
#add_child(room)
#room.start(enemy_value, enemy_kills)
#
#func _setup_next_room(current_room: Room):
#var room = _create_room()
#var dir = room_dirs.filter(func(d): return d != previous_dir).pick_random()
#_logger.debug("Creating room in dir %s" % dir)
#current_room.rooms[dir] = room
#current_room.open_doors()
#
#room.global_position = current_room.global_position + dir * room.get_size()
#add_child.call_deferred(room)

func _create_room() -> Room:
func _create_room(coord: Vector2i) -> Room:
var room = ROOM.instantiate()
var mat = room.material as ShaderMaterial
mat.set_shader_parameter("reflection_viewport", mirror_viewport.get_viewport().get_texture())

add_child(room)
rooms[coord] = room
room.global_position = coord * room.get_size()
room.setup_doors(grid_room_map.get_linked_dirs(coord))

room.finished.connect(func():
_setup_next_room(room)
GameManager.room_done()
grid_room_map.set_room_cleared(coord)
room.open_doors()
enemy_value *= value_increase
enemy_kills *= kill_increase
)
room.entered.connect(func(dir):
_logger.debug("Player entered in direction %s" % dir)
var new_room = room.rooms[dir]
cam.global_position = new_room.global_position
var next_coord = coord + dir
if not next_coord in rooms:
print("Room not available in %s" % next_coord)
return

var next_room = rooms[next_coord]
cam.global_position = next_room.global_position
relative_remote_transform_2d.center = cam.global_position

player.velocity = Vector2.ZERO
player.global_position = new_room.get_door(-dir).global_position + dir * 10
player.global_position = next_room.get_door(-dir).global_position + Vector2(dir) * 10
player.immediate_return_trident()

previous_dir = -dir
new_room.start(enemy_value, enemy_kills, dir)
GameManager.room_start()
get_tree().create_timer(1.0).timeout.connect(func(): room.queue_free())
grid_room_map.coord = next_coord
if not grid_room_map.is_room_cleared(next_coord):
next_room.start(enemy_value, enemy_kills, dir)
)
return room

Expand Down
39 changes: 35 additions & 4 deletions godot/src/game.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[gd_scene load_steps=11 format=3 uid="uid://di8g8e6s34cik"]
[gd_scene load_steps=13 format=3 uid="uid://di8g8e6s34cik"]

[ext_resource type="Script" path="res://src/game.gd" id="1_fscq0"]
[ext_resource type="Script" path="res://src/base-system/RelativeRemoteTransform2D.gd" id="3_vvprc"]
[ext_resource type="AudioStream" uid="uid://cj3tphknhrawu" path="res://assets/sound/Sparkle.mp3" id="4_rug7x"]
[ext_resource type="AudioStream" uid="uid://bygqjhqr85tjf" path="res://assets/sound/危機.mp3" id="5_3pgwk"]
[ext_resource type="PackedScene" uid="uid://dlby6flxkmnjs" path="res://src/character/player.tscn" id="6_2xq02"]
[ext_resource type="PackedScene" uid="uid://1sccjkvbts1e" path="res://src/character/player_reflection.tscn" id="6_bcrcl"]
[ext_resource type="Script" path="res://src/GridRoomMap.gd" id="7_hygqf"]
[ext_resource type="Script" path="res://src/GridMapUI.gd" id="8_21wk6"]

[sub_resource type="Animation" id="Animation_880qs"]
length = 0.001
Expand Down Expand Up @@ -33,6 +35,18 @@ tracks/1/keys = {
"update": 1,
"values": [true]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("BGM:volume_db")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [-30.0]
}

[sub_resource type="Animation" id="Animation_sp5nt"]
resource_name = "mirror"
Expand Down Expand Up @@ -149,7 +163,6 @@ _data = {
script = ExtResource("1_fscq0")

[node name="Camera2D" type="Camera2D" parent="."]
position_smoothing_enabled = true

[node name="Player" parent="." instance=ExtResource("6_2xq02")]
z_index = 10
Expand All @@ -163,12 +176,17 @@ relative_to_origin = 1.06
[node name="BGM" type="AudioStreamPlayer" parent="."]
process_mode = 3
stream = ExtResource("4_rug7x")
volume_db = -25.0
volume_db = -30.0

[node name="BGM2" type="AudioStreamPlayer" parent="."]
process_mode = 3
stream = ExtResource("4_rug7x")
volume_db = -30.0

[node name="BGMirror" type="AudioStreamPlayer" parent="."]
process_mode = 3
stream = ExtResource("5_3pgwk")
volume_db = -50.0
volume_db = -60.0

[node name="MusicPlayer" type="AnimationPlayer" parent="."]
process_mode = 3
Expand All @@ -188,5 +206,18 @@ position_smoothing_enabled = true
[node name="PlayerReflection" parent="MirrorViewport" node_paths=PackedStringArray("player") instance=ExtResource("6_bcrcl")]
player = NodePath("../../Player")

[node name="GridRoomMap" type="Node" parent="."]
script = ExtResource("7_hygqf")

[node name="CanvasLayer" type="CanvasLayer" parent="."]

[node name="GridMapUI" type="Control" parent="CanvasLayer" node_paths=PackedStringArray("room_manager")]
layout_mode = 3
anchors_preset = 0
offset_right = 40.0
offset_bottom = 40.0
script = ExtResource("8_21wk6")
room_manager = NodePath("../../GridRoomMap")

[connection signal="died" from="Player" to="." method="_on_player_died"]
[connection signal="reflected" from="Player" to="." method="_on_player_reflected"]
3 changes: 3 additions & 0 deletions godot/src/props/door.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func open():
func close():
animation_player.play("close")

func disable():
$CollisionShape2D.disabled = true
hide()

func _on_enter_area_body_entered(body):
entered.emit()
Loading

0 comments on commit 767449f

Please sign in to comment.