Compare commits

..

6 Commits

20 changed files with 393 additions and 57 deletions

View File

@ -0,0 +1,3 @@
source_md5="88802550c0ee17f57dfd0bd814e77811"
dest_md5="64a5bb7ee98d59912ece658665687037"

BIN
art/tiles/brickwall.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://art/tiles/brickwall.png"
dest_files=[ "res://.import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=false
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

22
nodes/4WayController.gd Normal file
View File

@ -0,0 +1,22 @@
extends TK_Controller
class_name TK_4WayController
## Composable 4-way grid controller.
## Depends on placement below some Node2D below some GridContext.
# todo: Network player aware.
# todo: AI aware.
var _game_object: Node2D
func _enter_tree() -> void:
_game_object = get_parent()
func _input(p_event: InputEvent):
var direction := InputUtils.input_event_to_4way_direction(p_event)
if direction == Vector2.ZERO:
return
var cell = _context.local_position_to_context_position(_game_object.position)
var new_cell = cell + direction
_context.try_moving(_game_object, cell, new_cell)

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://nodes/GridController.gd" type="Script" id=1]
[ext_resource path="res://nodes/4WayController.gd" type="Script" id=1]
[node name="GridController" type="Node"]
script = ExtResource( 1 )

18
nodes/Context.gd Normal file
View File

@ -0,0 +1,18 @@
tool
extends Node2D
class_name TK_Context
# todo: Abstract actions.
# todo: Signal registration of GameObject actions.
func local_position_to_context_position(_p_position: Vector2) -> Vector2:
push_error("Abstract class method is unimplemented")
return Vector2.ZERO
func is_path_traversible(_p_game_object: Node2D, _p_from: Vector2, _p_to: Vector2) -> bool:
push_error("Abstract class method is unimplemented")
return false
func try_moving(_p_game_object: Node2D, _p_from: Vector2, _p_to: Vector2) -> bool:
push_error("Abstract class method is unimplemented")
return false

13
nodes/Controller.gd Normal file
View File

@ -0,0 +1,13 @@
extends Node
class_name TK_Controller
var _context: Node2D
func _enter_tree() -> void:
_context = _get_context()
func _get_context() -> Node:
var current := self.get_parent().get_parent()
while not current is TK_Context:
current = current.get_parent()
return current

View File

@ -4,7 +4,17 @@ 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
const NO_SIZE_SENTINEL = Vector2(-1, -1)
export var target_size: Vector2 = NO_SIZE_SENTINEL setget _set_target_size
# todo: Ask context about preferred sizing if there's any.
func _enter_tree() -> void:
# It's okay to fail here for non-grid contexts.
if get_parent().get_parent().connect("cell_size_changed", self, "_update_cell_size"):
_set_target_size(get_parent().get_parent().cell_size)
elif target_size == NO_SIZE_SENTINEL:
_set_target_size(texture.get_size())
func _set_target_size(p_size: Vector2) -> void:
target_size = p_size
@ -12,3 +22,6 @@ func _set_target_size(p_size: Vector2) -> void:
func _update_scale() -> void:
scale = target_size / texture.get_size()
func _update_cell_size(_p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void:
_set_target_size(p_new_cell_size)

12
nodes/GameObject.gd Normal file
View File

@ -0,0 +1,12 @@
tool
extends Node2D
class_name TK_GameObject
func _ready() -> void:
# It's okay to fail here for non-grid contexts.
if get_parent().connect("cell_size_changed", self, "_update_cell_size"):
pass
func _update_cell_size(p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void:
var delta := p_new_cell_size / p_old_cell_size
position *= delta

6
nodes/GameObject.tscn Normal file
View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://nodes/GameObject.gd" type="Script" id=1]
[node name="GameObject" type="Node2D"]
script = ExtResource( 1 )

View File

@ -1,20 +1,48 @@
extends Node2D
class_name TK_GridContext
tool
extends TK_Context
class_name n_parent
## todo: Cell visualization.
## todo: Integration of TileMap.
export var cell_size: Vector2 = Vector2(64, 64)
export var cell_size: Vector2 = Vector2(64, 64) setget _set_cell_size
func position_to_cell_position(p_position: Vector2) -> Vector2:
signal cell_size_changed(p_old_cell_size, p_new_cell_size)
var _colliders: Dictionary # Node to null (set)
func local_position_to_context_position(p_position: Vector2) -> Vector2:
return p_position / cell_size
func is_cell_traversible(p_cell_position: Vector2) -> bool:
func is_path_traversible(p_game_object: Node2D, p_from: Vector2, p_to: Vector2) -> bool:
for collider in _colliders:
if not collider.is_path_traversible(p_game_object, p_from, p_to):
return false
return true
func try_moving(p_node: Node2D, p_cell_position: Vector2) -> bool:
if not is_cell_traversible(p_cell_position):
# todo: Effectful traversal.
func try_moving(p_game_object: Node2D, p_from: Vector2, p_to: Vector2) -> bool:
if not is_path_traversible(p_game_object, p_from, p_to):
return false
p_node.position = cell_size * p_cell_position
p_game_object.position = cell_size * p_to
return true
func register_collider(p_collider: Node) -> void:
_colliders[p_collider] = null
if not p_collider.is_connected("child_exiting_tree", self, "_on_collider_exiting_tree") and \
p_collider.connect("child_exiting_tree", self, "_on_collider_exiting_tree") != OK:
assert(false)
func _on_collider_exiting_tree(p_collider) -> void:
if not _colliders.erase(p_collider):
pass
func _set_cell_size(p_new_cell_size: Vector2) -> void:
if p_new_cell_size.x == 0 or p_new_cell_size.y == 0:
push_error("Cell size cannot be zero")
return
var cell_size_cache = cell_size
cell_size = p_new_cell_size
emit_signal("cell_size_changed", cell_size_cache, p_new_cell_size)

View File

@ -1,30 +0,0 @@
extends Node
class_name TK_GridController
## Composable 4-way grid controller.
## Depends on placement below some Node2D below some GridContext.
# todo: Turn-based logic context.
signal moved(p_new_cell_position)
var _grid_context_cache: Node2D
func _enter_tree() -> void:
_grid_context_cache = _get_grid_context()
func _input(p_event: InputEvent):
var direction := InputUtils.input_event_to_4way_direction(p_event)
if direction == Vector2.ZERO:
return
var cell = _grid_context_cache.position_to_cell_position(get_parent().position)
var new_cell = cell + direction
if _grid_context_cache.try_moving(get_parent(), new_cell):
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

83
nodes/TileMapObject.gd Normal file
View File

@ -0,0 +1,83 @@
tool
extends TileMap
# todo: Give care about descriptor swaps being loseless / non introducing artifacts.
# todo: Prevent user from deleting or editing tile_set property.
# todo: Update it when changed.
## Dictates whether tileset collision information should be exposed to game context.
export var expose_collisions: bool = true
# todo: Use Node identity instead of name.
var _descriptor_name_to_id: Dictionary # String to int
var _id_to_descriptor_name: Dictionary # int to String
func change_tile_texture(p_tile: String, p_texture: Texture) -> void:
if not p_tile in _descriptor_name_to_id:
push_error("Invalid tile name")
return
var id := _descriptor_name_to_id[p_tile] as int
tile_set.tile_set_texture(id, p_texture)
func is_path_traversible(p_game_object: Node2D, p_from: Vector2, p_to: Vector2) -> bool:
if p_from.distance_squared_to(p_to) == 1:
var id := get_cell(int(p_to.x), int(p_to.y))
if id == -1: return true
var descriptor = _id_to_descriptor_name[id] as String
return get_node(descriptor).is_traversible_for(p_game_object)
else:
print_debug("Unimplemented")
return false
func _enter_tree() -> void:
tile_set = TileSet.new()
var n_parent := get_parent()
if not n_parent is TK_Context:
return
n_parent.register_collider(self)
# It's okay to fail here for non-grid contexts.
if not n_parent.is_connected("cell_size_changed", self, "_update_cell_size") and \
n_parent.connect("cell_size_changed", self, "_update_cell_size") != OK:
assert(false)
_update_cell_size(n_parent.cell_size, n_parent.cell_size)
if not is_connected("child_entered_tree", self, "_on_child_entered_tree") and \
connect("child_entered_tree", self, "_on_child_entered_tree") != OK:
assert(false)
if not is_connected("child_exiting_tree", self, "_on_child_exiting_tree") and \
connect("child_exiting_tree", self, "_on_child_exiting_tree") != OK:
assert(false)
func _update_cell_size(_p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void:
# We're assuming that all tile textures are of same dimensions.
# todo: Diagnose mismatch by walking over all textures?
if tile_set.get_tiles_ids().size() == 0:
return
var tileset_texture_size := tile_set.tile_get_texture(0).get_size()
scale = p_new_cell_size / tileset_texture_size
cell_size = p_new_cell_size / scale
func _on_child_entered_tree(p_node: Node) -> void:
var id := tile_set.get_last_unused_tile_id()
_descriptor_name_to_id[p_node.name] = id
_id_to_descriptor_name[id] = p_node.name
tile_set.create_tile(id)
tile_set.tile_set_texture(id, p_node.texture)
if not p_node.id.empty():
tile_set.tile_set_name(id, p_node.id)
func _on_child_exiting_tree(p_node: Node) -> void:
var id := _descriptor_name_to_id[p_node.name] as int
for cell in get_used_cells_by_id(id):
set_cellv(cell, -1)
tile_set.remove_tile(id)
if not _descriptor_name_to_id.erase(p_node.name):
assert(false)
if not _id_to_descriptor_name.erase(id):
assert(false)

View File

@ -0,0 +1,20 @@
tool
extends Node
class_name TK_TileMapTileDescriptor
export var texture: Texture setget _set_texture
# todo: More layer customizeability.
export var is_solid: bool
# todo: Update TileMapObject in editor when it's changed.
export var id: String = ""
# todo: Should work for non-grid objects.
func is_traversible_for(_p_game_object: Node2D) -> bool:
return not is_solid
func _set_texture(p_texture: Texture) -> void:
texture = p_texture
if is_inside_tree():
get_parent().change_tile_texture(name, p_texture)

View File

@ -2,7 +2,7 @@ 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))
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())
return p_a - p_b * (p_a / p_b).floor()

View File

@ -1,6 +1,6 @@
extends Node
func input_event_to_4way_direction(p_event: InputEvent) -> Vector2:
func input_event_to_8way_direction(p_event: InputEvent) -> Vector2:
var result := Vector2.ZERO
if p_event.is_action_pressed("move_down"):
result.y += 1
@ -11,3 +11,15 @@ func input_event_to_4way_direction(p_event: InputEvent) -> Vector2:
if p_event.is_action_pressed("move_left"):
result.x -= 1
return result
func input_event_to_4way_direction(p_event: InputEvent) -> Vector2:
if p_event.is_action_pressed("move_down") and not p_event.is_action_pressed("move_up"):
return Vector2.DOWN
elif p_event.is_action_pressed("move_up") and not p_event.is_action_pressed("move_down"):
return Vector2.UP
elif p_event.is_action_pressed("move_left") and not p_event.is_action_pressed("move_right"):
return Vector2.LEFT
elif p_event.is_action_pressed("move_right") and not p_event.is_action_pressed("move_left"):
return Vector2.RIGHT
else:
return Vector2.ZERO

View File

@ -104,25 +104,45 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://addons/Godoxel/Util.gd"
}, {
"base": "TK_Controller",
"class": "TK_4WayController",
"language": "GDScript",
"path": "res://nodes/4WayController.gd"
}, {
"base": "Node",
"class": "TK_Arithmetic",
"language": "GDScript",
"path": "res://nodes/singletons/Arithmetic.gd"
}, {
"base": "Node2D",
"class": "TK_Context",
"language": "GDScript",
"path": "res://nodes/Context.gd"
}, {
"base": "Node",
"class": "TK_Controller",
"language": "GDScript",
"path": "res://nodes/Controller.gd"
}, {
"base": "Sprite",
"class": "TK_FittingSprite",
"language": "GDScript",
"path": "res://nodes/FittingSprite.gd"
}, {
"base": "Node2D",
"class": "TK_GridContext",
"class": "TK_GameObject",
"language": "GDScript",
"path": "res://nodes/GridContext.gd"
"path": "res://nodes/GameObject.gd"
}, {
"base": "Node",
"class": "TK_GridController",
"class": "TK_TileMapTileDescriptor",
"language": "GDScript",
"path": "res://nodes/GridController.gd"
"path": "res://nodes/TileMapTileDescriptor.gd"
}, {
"base": "TK_Context",
"class": "n_parent",
"language": "GDScript",
"path": "res://nodes/GridContext.gd"
} ]
_global_script_class_icons={
"BrushPrefabs": "",
@ -144,16 +164,21 @@ _global_script_class_icons={
"GERainbow": "",
"GERect": "",
"GEUtils": "",
"TK_4WayController": "",
"TK_Arithmetic": "",
"TK_Context": "",
"TK_Controller": "",
"TK_FittingSprite": "",
"TK_GridContext": "",
"TK_GridController": ""
"TK_GameObject": "",
"TK_TileMapTileDescriptor": "",
"n_parent": ""
}
[application]
config/name="TochieKit"
run/main_scene="res://scenes/Game.tscn"
run/low_processor_mode=true
config/icon="res://icon.png"
[autoload]
@ -161,6 +186,12 @@ config/icon="res://icon.png"
Arithmetic="*res://nodes/singletons/Arithmetic.gd"
InputUtils="*res://nodes/singletons/InputUtils.gd"
[display]
window/size/height=576
window/stretch/mode="2d"
window/stretch/aspect="keep"
[editor_plugins]
enabled=PoolStringArray( "res://addons/Godoxel/plugin.cfg" )
@ -196,6 +227,15 @@ move_left={
]
}
[input_devices]
buffering/agile_event_flushing=true
pointing/emulate_touch_from_mouse=true
[node]
name_num_separator=3
[physics]
common/enable_pause_aware_picking=true
@ -203,8 +243,18 @@ common/enable_pause_aware_picking=true
[rendering]
quality/driver/driver_name="GLES2"
quality/intended_usage/framebuffer_allocation=1
quality/intended_usage/framebuffer_allocation.mobile=1
2d/snapping/use_gpu_pixel_snap=true
threads/thread_model=2
vram_compression/import_etc=true
vram_compression/import_etc2=false
quality/reflections/texture_array_reflections=false
quality/shading/force_vertex_shading=true
quality/shading/force_lambert_over_burley=true
quality/shading/force_blinn_over_ggx=true
quality/filters/use_nearest_mipmap_filter=true
batching/options/single_rect_fallback=true
quality/lightmapping/use_bicubic_sampling=false
environment/default_clear_color=Color( 0.00784314, 0, 0.117647, 1 )
environment/default_environment="res://default_env.tres"

View File

@ -1,21 +1,59 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=10 format=2]
[ext_resource path="res://nodes/FittingSprite.gd" type="Script" id=1]
[ext_resource path="res://art/entities/fiend.png" type="Texture" id=2]
[ext_resource path="res://nodes/GridController.gd" type="Script" id=3]
[ext_resource path="res://nodes/4WayController.tscn" type="PackedScene" id=3]
[ext_resource path="res://nodes/GridContext.tscn" type="PackedScene" id=4]
[ext_resource path="res://nodes/GameObject.tscn" type="PackedScene" id=5]
[ext_resource path="res://art/tiles/brickwall.png" type="Texture" id=6]
[ext_resource path="res://nodes/TileMapObject.tscn" type="PackedScene" id=7]
[ext_resource path="res://scenes/tiles/Brickwall.tscn" type="PackedScene" id=8]
[node name="Game" type="Node"]
[sub_resource type="TileSet" id=1]
0/name = "tk.Brickwall"
0/texture = ExtResource( 6 )
0/tex_offset = Vector2( 0, 0 )
0/modulate = Color( 1, 1, 1, 1 )
0/region = Rect2( 0, 0, 0, 0 )
0/tile_mode = 0
0/occluder_offset = Vector2( 0, 0 )
0/navigation_offset = Vector2( 0, 0 )
0/shape_offset = Vector2( 0, 0 )
0/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
0/shape_one_way = false
0/shape_one_way_margin = 0.0
0/shapes = [ ]
0/z_index = 0
[node name="Gauntlet" type="Node"]
[node name="GridContext" parent="." instance=ExtResource( 4 )]
[node name="Fiend" type="Sprite" parent="GridContext"]
position = Vector2( 128, 128 )
[node name="TileMapObject" parent="GridContext" instance=ExtResource( 7 )]
scale = Vector2( 4, 4 )
tile_set = SubResource( 1 )
cell_size = Vector2( 16, 16 )
tile_data = PoolIntArray( 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0, 0, 12, 0, 0, 13, 0, 0, 14, 0, 0, 15, 0, 0, 65536, 0, 0, 65551, 0, 0, 131072, 0, 0, 131087, 0, 0, 196608, 0, 0, 196623, 0, 0, 262144, 0, 0, 262159, 0, 0, 327680, 0, 0, 327695, 0, 0, 393216, 0, 0, 393231, 0, 0, 458752, 0, 0, 458767, 0, 0, 524288, 0, 0, 524289, 0, 0, 524290, 0, 0, 524291, 0, 0, 524292, 0, 0, 524293, 0, 0, 524294, 0, 0, 524295, 0, 0, 524296, 0, 0, 524297, 0, 0, 524298, 0, 0, 524299, 0, 0, 524300, 0, 0, 524301, 0, 0, 524302, 0, 0, 524303, 0, 0 )
[node name="Brickwall" parent="GridContext/TileMapObject" instance=ExtResource( 8 )]
[node name="Player" parent="GridContext" instance=ExtResource( 5 )]
position = Vector2( 384, 128 )
[node name="Visual" type="Sprite" parent="GridContext/Player"]
scale = Vector2( 4, 4 )
texture = ExtResource( 2 )
centered = false
script = ExtResource( 1 )
__meta__ = {
"_edit_lock_": true
}
target_size = Vector2( 64, 64 )
[node name="GridController" type="Node" parent="GridContext/Fiend"]
script = ExtResource( 3 )
[node name="4WayController" parent="GridContext/Player" instance=ExtResource( 3 )]
[node name="Camera2D" type="Camera2D" parent="GridContext/Player"]
position = Vector2( 32, 32 )
__meta__ = {
"_edit_lock_": true
}

View File

@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://art/tiles/brickwall.png" type="Texture" id=1]
[ext_resource path="res://nodes/TileMapTileDescriptor.gd" type="Script" id=2]
[node name="Brickwall" type="Node"]
script = ExtResource( 2 )
texture = ExtResource( 1 )
is_solid = true
id = "tk.Brickwall"