simple composable grid context and grid controller, fiend sprite, godotxel addon
This commit is contained in:
14
nodes/FittingSprite.gd
Normal file
14
nodes/FittingSprite.gd
Normal file
@ -0,0 +1,14 @@
|
||||
tool
|
||||
extends Sprite
|
||||
class_name TK_FittingSprite
|
||||
|
||||
## A Sprite that is resized to absolute pixel size, no matter the texture.
|
||||
|
||||
export var target_size: Vector2 setget _set_target_size
|
||||
|
||||
func _set_target_size(p_size: Vector2) -> void:
|
||||
target_size = p_size
|
||||
_update_scale()
|
||||
|
||||
func _update_scale() -> void:
|
||||
scale = target_size / texture.get_size()
|
13
nodes/GridContext.gd
Normal file
13
nodes/GridContext.gd
Normal file
@ -0,0 +1,13 @@
|
||||
extends Node2D
|
||||
class_name TK_GridContext
|
||||
|
||||
## todo: Cell visualization.
|
||||
## todo: Integration of TileMap.
|
||||
|
||||
export var cell_size: Vector2 = Vector2(64, 64)
|
||||
|
||||
func position_to_cell_position(p_position: Vector2) -> Vector2:
|
||||
return Arithmetic.vector2_mod(global_position - p_position, cell_size)
|
||||
|
||||
func is_cell_traversible(p_cell_position: Vector2) -> bool:
|
||||
return true
|
6
nodes/GridContext.tscn
Normal file
6
nodes/GridContext.tscn
Normal file
@ -0,0 +1,6 @@
|
||||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://nodes/GridContext.gd" type="Script" id=1]
|
||||
|
||||
[node name="GridContext" type="Node2D"]
|
||||
script = ExtResource( 1 )
|
26
nodes/GridController.gd
Normal file
26
nodes/GridController.gd
Normal file
@ -0,0 +1,26 @@
|
||||
extends Node
|
||||
class_name TK_GridController
|
||||
|
||||
## Composable 4-way grid controller.
|
||||
## Depends on placement below some Node2D below some GridContext.
|
||||
|
||||
signal moved(p_new_cell_position)
|
||||
|
||||
func _input(p_event: InputEvent):
|
||||
var direction := InputUtils.input_event_to_4way_direction(p_event)
|
||||
if direction == Vector2.ZERO:
|
||||
return
|
||||
|
||||
var context := _get_grid_context()
|
||||
var cell = context.position_to_cell_position(get_parent().global_position)
|
||||
var new_cell = cell + direction
|
||||
if context.is_cell_traversible(new_cell):
|
||||
# todo: Should we move the outmost node2d?
|
||||
get_parent().position += context.cell_size * direction
|
||||
emit_signal("moved", new_cell)
|
||||
|
||||
func _get_grid_context() -> Node:
|
||||
var current := self.get_parent().get_parent()
|
||||
while current.name != "GridContext":
|
||||
current = current.get_parent()
|
||||
return current
|
6
nodes/GridController.tscn
Normal file
6
nodes/GridController.tscn
Normal file
@ -0,0 +1,6 @@
|
||||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://nodes/GridController.gd" type="Script" id=1]
|
||||
|
||||
[node name="GridController" type="Node"]
|
||||
script = ExtResource( 1 )
|
8
nodes/singletons/Arithmetic.gd
Normal file
8
nodes/singletons/Arithmetic.gd
Normal file
@ -0,0 +1,8 @@
|
||||
extends Node
|
||||
class_name TK_Arithmetic
|
||||
|
||||
static func float_mod(p_a: float, p_b: float) -> float:
|
||||
return p_a - (p_b * floor(p_a / p_b))
|
||||
|
||||
static func vector2_mod(p_a: Vector2, p_b: Vector2) -> Vector2:
|
||||
return p_a - (p_b * (p_a / p_b).floor())
|
13
nodes/singletons/InputUtils.gd
Normal file
13
nodes/singletons/InputUtils.gd
Normal file
@ -0,0 +1,13 @@
|
||||
extends Node
|
||||
|
||||
func input_event_to_4way_direction(p_event: InputEvent) -> Vector2:
|
||||
var result := Vector2.ZERO
|
||||
if p_event.is_action_pressed("move_down"):
|
||||
result.y += 1
|
||||
if p_event.is_action_pressed("move_right"):
|
||||
result.x += 1
|
||||
if p_event.is_action_pressed("move_up"):
|
||||
result.y -= 1
|
||||
if p_event.is_action_pressed("move_left"):
|
||||
result.x -= 1
|
||||
return result
|
Reference in New Issue
Block a user