Snake/Scripts/Tile.gd

45 lines
1.1 KiB
GDScript

extends Sprite2D
class_name Tile
enum States {
EMPTY,
SNAKE,
APPLE
}
@export var state : States
# 0 means no snake at all, 1 means head and the biger, the further it is from the head
@export var snake_pos := 0
@export var tile_color : TileColors
@export var x : int
@export var y : int
func _init(_x : int, _y : int, _texture : Texture2D, _tile_colors : TileColors):
state = States.EMPTY
self.x = _x
self.y = _y
self.texture = _texture
self.tile_color = _tile_colors
func update_color():
match state:
States.EMPTY:
self.modulate = tile_color.empty
States.SNAKE:
self.modulate = tile_color.snake_body if snake_pos > 1 else tile_color.snake_head
States.APPLE:
self.modulate = 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
func _process(delta):
update_color()
pass