- Switching from sprites to UI Elements

- Binding Main Menu and Game Scene Together
TODO: Settings, Score, Highscore, Controller Support, Color Pallet Rework
This commit is contained in:
Snoweuph 2023-03-31 19:01:28 +02:00
parent da639880b5
commit 3037c1fce0
7 changed files with 192 additions and 171 deletions

File diff suppressed because one or more lines are too long

View file

@ -6,20 +6,44 @@
[sub_resource type="Resource" id="Resource_22g0s"] [sub_resource type="Resource" id="Resource_22g0s"]
script = ExtResource("3_gnesl") script = ExtResource("3_gnesl")
empty = Color(1, 1, 1, 1) empty = Color(0.2, 0.247059, 0.180392, 1)
snake_head = Color(0, 1, 0, 1) snake_head = Color(0.517647, 0.803922, 0.647059, 1)
snake_body = Color(0.133333, 0.545098, 0.133333, 1) snake_body = Color(0.262745, 0.486275, 0.368627, 1)
apple = Color(1, 0, 0, 1) apple = Color(0.941176, 0.396078, 0.396078, 1)
[node name="Node2D" type="Node2D"] [node name="Node2D" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="GameManager" type="Node" parent="."] [node name="GameManager" type="Node" parent="."]
[node name="Map" type="Node2D" parent="."] [node name="ColorRect" type="ColorRect" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0.0784314, 0.105882, 0.0666667, 1)
[node name="AspectRatioContainer" type="AspectRatioContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="Map" type="GridContainer" parent="AspectRatioContainer"]
layout_mode = 2
theme_override_constants/h_separation = 5
theme_override_constants/v_separation = 5
columns = 2
script = ExtResource("1_lps27") script = ExtResource("1_lps27")
map_size = 25 map_size = 25
texture = ExtResource("2_wiemx") texture = ExtResource("2_wiemx")
gaps = 5
tile_color = SubResource("Resource_22g0s") tile_color = SubResource("Resource_22g0s")
[node name="Camera2D" type="Camera2D" parent="."]

View file

@ -1,5 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://gn5or2td1tmd"] [gd_scene load_steps=3 format=3 uid="uid://c7ok1re4seqsh"]
[ext_resource type="Script" path="res://Scripts/Main Menu Buttons.gd" id="1_abac4"]
[ext_resource type="Theme" uid="uid://clkt54a4wy6tn" path="res://Assets/Base.tres" id="1_b1d2h"] [ext_resource type="Theme" uid="uid://clkt54a4wy6tn" path="res://Assets/Base.tres" id="1_b1d2h"]
[node name="Control" type="Control"] [node name="Control" type="Control"]
@ -9,6 +10,7 @@ anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
script = ExtResource("1_abac4")
[node name="ColorRect" type="ColorRect" parent="."] [node name="ColorRect" type="ColorRect" parent="."]
layout_mode = 1 layout_mode = 1
@ -47,27 +49,25 @@ vertical_alignment = 1
[node name="Menu Butons" type="VBoxContainer" parent="MarginContainer/VBoxContainer"] [node name="Menu Butons" type="VBoxContainer" parent="MarginContainer/VBoxContainer"]
layout_mode = 2 layout_mode = 2
[node name="Button" type="Button" parent="MarginContainer/VBoxContainer/Menu Butons"] [node name="Start Button" type="Button" parent="MarginContainer/VBoxContainer/Menu Butons"]
layout_mode = 2 layout_mode = 2
theme = ExtResource("1_b1d2h") theme = ExtResource("1_b1d2h")
theme_override_font_sizes/font_size = 96 theme_override_font_sizes/font_size = 96
text = "Start" text = "Start"
[node name="Button2" type="Button" parent="MarginContainer/VBoxContainer/Menu Butons"] [node name="About Button" type="Button" parent="MarginContainer/VBoxContainer/Menu Butons"]
layout_mode = 2
theme = ExtResource("1_b1d2h")
theme_override_font_sizes/font_size = 96
text = "Settings"
[node name="Button3" type="Button" parent="MarginContainer/VBoxContainer/Menu Butons"]
layout_mode = 2 layout_mode = 2
theme = ExtResource("1_b1d2h") theme = ExtResource("1_b1d2h")
theme_override_font_sizes/font_size = 96 theme_override_font_sizes/font_size = 96
text = "About text = "About
" "
[node name="Button4" type="Button" parent="MarginContainer/VBoxContainer/Menu Butons"] [node name="Quit Button" type="Button" parent="MarginContainer/VBoxContainer/Menu Butons"]
layout_mode = 2 layout_mode = 2
theme = ExtResource("1_b1d2h") theme = ExtResource("1_b1d2h")
theme_override_font_sizes/font_size = 96 theme_override_font_sizes/font_size = 96
text = "Quit" text = "Quit"
[connection signal="pressed" from="MarginContainer/VBoxContainer/Menu Butons/Start Button" to="." method="_on_start_button_pressed"]
[connection signal="pressed" from="MarginContainer/VBoxContainer/Menu Butons/About Button" to="." method="_on_about_button_pressed"]
[connection signal="pressed" from="MarginContainer/VBoxContainer/Menu Butons/Quit Button" to="." method="_on_quit_button_pressed"]

View file

@ -1,8 +1,7 @@
extends Node extends GridContainer
@export var map_size := 15 @export var map_size := 15
@export var texture : Texture2D @export var texture : Texture2D
@export var gaps := 10
@export var speed := 0.25 @export var speed := 0.25
@export var tile_color := TileColors.new() @export var tile_color := TileColors.new()
@ -18,13 +17,13 @@ var timer := 0.0
var game_is_running := true var game_is_running := true
func generate_tiles(): func generate_tiles():
self.columns = map_size
for x in range(map_size): for x in range(map_size):
tiles.append([]) tiles.append([])
tiles[x] = [] tiles[x] = []
for y in range(map_size): for y in range(map_size):
var tile = Tile.new(x ,y, texture, tile_color) var tile = Tile.new(x ,y, tile_color)
tile.calculate_tile_position_and_size(min(DisplayServer.window_get_size(DisplayServer.get_window_list()[0]).x, DisplayServer.window_get_size(DisplayServer.get_window_list()[0]).y), gaps, map_size, x, y)
self.add_child(tile) self.add_child(tile)
tiles[x].append([]) tiles[x].append([])
tiles[x][y] = tile tiles[x][y] = tile
@ -50,7 +49,7 @@ func set_snake_to_start_position():
func read_input() -> Vector2:# func read_input() -> Vector2:#
# cant use Input.get_vector because it normalizes the vector # cant use Input.get_vector because it normalizes the vector
return Vector2(Input.get_axis("left", "right"), Input.get_axis("up", "down")) return Vector2(Input.get_axis("up", "down"), Input.get_axis("left", "right"))
func process_tiles(): func process_tiles():
@ -94,7 +93,6 @@ func process_snake_rotation():
func _ready(): func _ready():
game_start() game_start()
get_tree().get_root().size_changed.connect(onScreenResize)
pass pass
func game_start(): func game_start():
@ -108,8 +106,8 @@ func game_start():
pass pass
func game_stop(): func game_stop():
print("dead")
game_is_running = false game_is_running = false
get_tree().change_scene_to_file("res://Scene/Main Menu.tscn")
pass pass
func _process(delta): func _process(delta):
@ -121,11 +119,3 @@ func _process(delta):
process_tiles() process_tiles()
timer += delta timer += delta
pass pass
func onScreenResize():
for x in range(map_size):
for y in range(map_size):
tiles[x][y].calculate_tile_position_and_size(min(DisplayServer.window_get_size(DisplayServer.get_window_list()[0]).x, DisplayServer.window_get_size(DisplayServer.get_window_list()[0]).y), gaps, map_size, x, y)
pass

View file

@ -0,0 +1,12 @@
extends Control
func _on_start_button_pressed():
get_tree().change_scene_to_file("res://Scene/Game.tscn")
func _on_quit_button_pressed():
get_tree().quit()
func _on_about_button_pressed():
OS.shell_open("https://git.euph.dev/snoweuph/Snake")

View file

@ -1,4 +1,4 @@
extends Sprite2D extends ColorRect
class_name Tile class_name Tile
@ -16,27 +16,22 @@ enum States {
@export var x : int @export var x : int
@export var y : int @export var y : int
func _init(_x : int, _y : int, _texture : Texture2D, _tile_colors : TileColors): func _init(_x : int, _y : int, _tile_colors : TileColors):
state = States.EMPTY state = States.EMPTY
self.x = _x self.x = _x
self.y = _y self.y = _y
self.texture = _texture self.size_flags_horizontal += Control.SIZE_EXPAND
self.size_flags_vertical += Control.SIZE_EXPAND
self.tile_color = _tile_colors self.tile_color = _tile_colors
func update_color(): func update_color():
match state: match state:
States.EMPTY: States.EMPTY:
self.modulate = tile_color.empty self.color = tile_color.empty
States.SNAKE: States.SNAKE:
self.modulate = tile_color.snake_body if snake_pos > 1 else tile_color.snake_head self.color = tile_color.snake_body if snake_pos > 1 else tile_color.snake_head
States.APPLE: States.APPLE:
self.modulate = tile_color.apple self.color = tile_color.apple
pass
func calculate_tile_position_and_size(viewport_min : int, gaps : int, map_size: int, x : int, y : int):
var tile_scale = ( viewport_min - gaps ) / map_size - gaps
self.scale = Vector2(tile_scale, tile_scale)
self.position = Vector2(x * ( tile_scale + gaps ) - map_size / 2 * (tile_scale + gaps), y * (tile_scale + gaps) - map_size / 2 * (tile_scale + gaps))
pass pass
func _process(delta): func _process(delta):

View file

@ -11,7 +11,7 @@ config_version=5
[application] [application]
config/name="Snake" config/name="Snake"
run/main_scene="res://Scene/Game.tscn" run/main_scene="res://Scene/Main Menu.tscn"
config/features=PackedStringArray("4.0", "GL Compatibility") config/features=PackedStringArray("4.0", "GL Compatibility")
config/icon="res://icon.svg" config/icon="res://icon.svg"