From 92fc52a8ba3c49b3f18d843762a603a7fc8c8b78 Mon Sep 17 00:00:00 2001 From: snoweuph Date: Mon, 6 Feb 2023 13:47:18 +0100 Subject: [PATCH] Generate Tiles and Color them --- Assets/tile.png | Bin 0 -> 83 bytes Assets/tile.png.import | 34 ++++++++++++++++++++++++++ Scene/Game.tscn | 18 ++++++++++++++ Scripts/MapGenerator.gd | 52 ++++++++++++++++++++++++++++++++++++++++ Scripts/Tile.gd | 20 ++++++++++++++++ project.godot | 1 + 6 files changed, 125 insertions(+) create mode 100644 Assets/tile.png create mode 100644 Assets/tile.png.import create mode 100644 Scene/Game.tscn create mode 100644 Scripts/MapGenerator.gd create mode 100644 Scripts/Tile.gd diff --git a/Assets/tile.png b/Assets/tile.png new file mode 100644 index 0000000000000000000000000000000000000000..0d819cee866656fd82a765047411abe4d85b528f GIT binary patch literal 83 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1|;Q0k92}K#X;^)4C~IxyaaN1JzX3_IA$hS de0y`Vo`F+}c~eks_a2}GgQu&X%Q~loCIFkA6Z8N8 literal 0 HcmV?d00001 diff --git a/Assets/tile.png.import b/Assets/tile.png.import new file mode 100644 index 0000000..f499170 --- /dev/null +++ b/Assets/tile.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://vv33w22kwgpc" +path="res://.godot/imported/tile.png-9493555de8edb914756543e894238c3e.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/tile.png" +dest_files=["res://.godot/imported/tile.png-9493555de8edb914756543e894238c3e.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/Scene/Game.tscn b/Scene/Game.tscn new file mode 100644 index 0000000..07e6562 --- /dev/null +++ b/Scene/Game.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=3 format=3 uid="uid://clirt527jo4x2"] + +[ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="1_dyj4p"] +[ext_resource type="Texture2D" uid="uid://vv33w22kwgpc" path="res://Assets/tile.png" id="2_wiemx"] + +[node name="Node2D" type="Node2D"] + +[node name="GameManager" type="Node" parent="."] + +[node name="Map" type="Node2D" parent="."] +script = ExtResource("1_dyj4p") +texture = ExtResource("2_wiemx") + +[node name="Camera2D" type="Camera2D" parent="."] + +[node name="Tile" type="Sprite2D" parent="."] +position = Vector2(182, 214) +texture = ExtResource("2_wiemx") diff --git a/Scripts/MapGenerator.gd b/Scripts/MapGenerator.gd new file mode 100644 index 0000000..a006eb9 --- /dev/null +++ b/Scripts/MapGenerator.gd @@ -0,0 +1,52 @@ +extends Node + +@export var map_size := 15 +@export var texture : Texture2D +@export var scale := 32.0 +@export var gaps := 4 + +@export_category("Colors") +@export var tile_color := Color.WHITE +@export var snake_color := Color.GREEN +@export var apple_color := Color.RED + +# Array of all Tiles of the Map +var tiles = [] + + +func generate_tiles(): + for x in range(map_size): + tiles.append([]) + tiles[x] = [] + for y in range(map_size): + var tile = Tile.new() + tile.texture = texture + tile.scale = Vector2(scale, scale) + tile.position = Vector2(x * ( scale + gaps ) - map_size / 2 * (scale + gaps), y * (scale + gaps) - map_size / 2 * (scale + gaps)) + self.add_child(tile) + tiles[x].append([]) + tiles[x][y] = tile + pass + +func _ready(): + generate_tiles() + pass + +func update_tile_colors(): + for x in range(map_size): + for y in range(map_size): + match tiles[x][y].get_state(): + + Tile.States.EMPTY: + tiles[x][y].modulate = tile_color + + Tile.States.SNAKE: + tiles[x][y].modulate = snake_color + + Tile.States.APPLE: + tiles[x][y].modulate = apple_color + pass + +func _process(delta): + update_tile_colors() + pass diff --git a/Scripts/Tile.gd b/Scripts/Tile.gd new file mode 100644 index 0000000..187383b --- /dev/null +++ b/Scripts/Tile.gd @@ -0,0 +1,20 @@ +extends Sprite2D + +class_name Tile + +enum States { + EMPTY, + SNAKE, + APPLE +} + +@export var state : States + +func _init(): + state = States.EMPTY + +func get_state() -> States: + return state + +func set_state(_state : States): + state = _state diff --git a/project.godot b/project.godot index 4c2d699..2a7ca0b 100644 --- a/project.godot +++ b/project.godot @@ -11,6 +11,7 @@ config_version=5 [application] config/name="Snake" +run/main_scene="res://Scene/Game.tscn" config/features=PackedStringArray("4.0", "GL Compatibility") config/icon="res://icon.svg"