Generate Tiles and Color them

This commit is contained in:
snoweuph 2023-02-06 13:47:18 +01:00
parent ddf81f190d
commit 92fc52a8ba
6 changed files with 125 additions and 0 deletions

BIN
Assets/tile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 B

34
Assets/tile.png.import Normal file
View file

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

18
Scene/Game.tscn Normal file
View file

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

52
Scripts/MapGenerator.gd Normal file
View file

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

20
Scripts/Tile.gd Normal file
View file

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

View file

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