diff --git a/game/levels/test_level.tscn b/game/levels/test_level.tscn index 1dbb38c..197e43e 100644 --- a/game/levels/test_level.tscn +++ b/game/levels/test_level.tscn @@ -28,8 +28,8 @@ script = ExtResource("2_a2sw7") [node name="AbstractPlatform" parent="Platforms" parent_id_path=PackedInt32Array(1140671629) index="0" unique_id=1226827206 instance=ExtResource("3_pmn5w")] -position = Vector2(144, -58) -platform_width = 256 +position = Vector2(182, -42) +platform_width = 104 [node name="LeverDoor" parent="Doors" parent_id_path=PackedInt32Array(1468144392) index="0" unique_id=1575363350 instance=ExtResource("2_r01v2")] position = Vector2(416, -56) diff --git a/game/platform/abstract_platform.gd b/game/platform/abstract_platform.gd index 823c591..cd7b8e7 100644 --- a/game/platform/abstract_platform.gd +++ b/game/platform/abstract_platform.gd @@ -17,12 +17,13 @@ const MAX_WIDTH = TILE_SIZE * 32 @onready var collision: CollisionShape2D = $AnimatableBody2D/CollisionShape2D -@onready var platform_sprite: Sprite2D = $PlatformSprite +@onready var platform_rect: ColorRect = $PlatformRect @onready var icon_sprite: AnimatedSprite2D = $IconSprite func _ready() -> void: - platform_sprite.material.set_shader_parameter("tile_size", TILE_SIZE) + platform_rect.material.set_shader_parameter("tile_size", TILE_SIZE) + platform_rect.material.set_shader_parameter("seed", randf()) rebuild_platform() @@ -33,7 +34,9 @@ func rebuild_platform() -> void: collision.shape = shape shape.size = Vector2(platform_width, TILE_SIZE) - platform_sprite.material.set_shader_parameter("platform_width", platform_width) + platform_rect.size.x = platform_width + platform_rect.position.x = -platform_width/2 + platform_rect.material.set_shader_parameter("platform_width", platform_width) func _physics_process(_delta: float) -> void: diff --git a/game/platform/abstract_platform.tscn b/game/platform/abstract_platform.tscn index 59a2551..aee5ddd 100644 --- a/game/platform/abstract_platform.tscn +++ b/game/platform/abstract_platform.tscn @@ -5,19 +5,38 @@ [ext_resource type="Shader" uid="uid://d4hm3vjx1rhsf" path="res://game/shaders/platform.gdshader" id="2_jsq8n"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_2ivtb"] +resource_local_to_scene = true shader = ExtResource("2_jsq8n") -shader_parameter/tile_size = 16.0 -shader_parameter/platform_width = 16.0 +shader_parameter/tile_size = 8.0 +shader_parameter/platform_width = 24.0 +shader_parameter/atlas = ExtResource("2_31xwx") +shader_parameter/seed = 0.038039055 + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_jsq8n"] +resource_local_to_scene = true +size = Vector2(16, 8) [node name="AbstractPlatform" type="Node2D" unique_id=1226827206] script = ExtResource("1_426vp") -[node name="PlatformSprite" type="Sprite2D" parent="." unique_id=1901062470] +[node name="PlatformRect" type="ColorRect" parent="." unique_id=646284480] material = SubResource("ShaderMaterial_2ivtb") -texture = ExtResource("2_31xwx") +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -8.0 +offset_top = -4.0 +offset_right = 8.0 +offset_bottom = 4.0 +grow_horizontal = 2 +grow_vertical = 2 +pivot_offset_ratio = Vector2(0.5, 0.5) [node name="IconSprite" type="AnimatedSprite2D" parent="." unique_id=287774650] [node name="AnimatableBody2D" type="AnimatableBody2D" parent="." unique_id=908936954] [node name="CollisionShape2D" type="CollisionShape2D" parent="AnimatableBody2D" unique_id=175449124] +shape = SubResource("RectangleShape2D_jsq8n") diff --git a/game/shaders/black_n_white.gdshader b/game/shaders/black_n_white.gdshader index 2c86172..3a29246 100644 --- a/game/shaders/black_n_white.gdshader +++ b/game/shaders/black_n_white.gdshader @@ -1,52 +1,7 @@ shader_type canvas_item; -uniform vec4 black_color : source_color = vec4(0.0, 0.0, 0.0, 1.0); -uniform vec4 white_color : source_color = vec4(1.0, 1.0, 1.0, 1.0); -uniform bool switch_colors = false; -uniform float threshold : hint_range(0.0, 1.0) = 0.5; -uniform float speed : hint_range(0.0, 10.0) = 0.5; -uniform float intensity : hint_range(0.0, 1.0) = 1.0; -uniform float scale : hint_range(0.0, 100.0) = 20.0; - -vec2 random(vec2 uv){ - uv = vec2( dot(uv, vec2(127.1,311.7) ), - dot(uv, vec2(269.5,183.3) ) ); - return -1.0 + 2.0 * fract(sin(uv) * 43758.5453123); -} - -float noise(vec2 uv) { - vec2 uv_index = floor(uv); - vec2 uv_fract = fract(uv); - - vec2 blur = smoothstep(0.0, 1.0, uv_fract); - - return mix( mix( dot( random(uv_index + vec2(0.0,0.0) ), uv_fract - vec2(0.0,0.0) ), - dot( random(uv_index + vec2(1.0,0.0) ), uv_fract - vec2(1.0,0.0) ), blur.x), - mix( dot( random(uv_index + vec2(0.0,1.0) ), uv_fract - vec2(0.0,1.0) ), - dot( random(uv_index + vec2(1.0,1.0) ), uv_fract - vec2(1.0,1.0) ), blur.x), blur.y) + 0.5; -} - +#include "get_black_n_white.gdshaderinc" void fragment() { - if (COLOR.a > 0.01) { - float l = dot(COLOR.rgb, vec3(0.2126, 0.7152, 0.0722)); - - bool is_black = l < threshold; - if (switch_colors) { - is_black = !is_black; - } - if (intensity < 1.0) { - vec2 coord = UV * scale + TIME * speed; - if (noise(coord) > intensity) { - is_black = !is_black; - } - } - - if (is_black) { - COLOR = black_color; - } - else { - COLOR = white_color; - } - } + COLOR = get_black_and_white(COLOR, UV, TIME); } diff --git a/game/shaders/get_black_n_white.gdshaderinc b/game/shaders/get_black_n_white.gdshaderinc new file mode 100644 index 0000000..e7cbec1 --- /dev/null +++ b/game/shaders/get_black_n_white.gdshaderinc @@ -0,0 +1,47 @@ +uniform vec4 black_color : source_color = vec4(0.0, 0.0, 0.0, 1.0); +uniform vec4 white_color : source_color = vec4(1.0, 1.0, 1.0, 1.0); +uniform bool switch_colors = false; +uniform float threshold : hint_range(0.0, 1.0) = 0.5; +uniform float speed : hint_range(0.0, 10.0) = 0.5; +uniform float intensity : hint_range(0.0, 1.0) = 1.0; +uniform float scale : hint_range(0.0, 100.0) = 20.0; + +vec2 random(vec2 uv){ + uv = vec2( dot(uv, vec2(127.1,311.7) ), + dot(uv, vec2(269.5,183.3) ) ); + return -1.0 + 2.0 * fract(sin(uv) * 43758.5453123); +} + +float noise(vec2 uv) { + vec2 uv_index = floor(uv); + vec2 uv_fract = fract(uv); + + vec2 blur = smoothstep(0.0, 1.0, uv_fract); + + return mix( mix( dot( random(uv_index + vec2(0.0,0.0) ), uv_fract - vec2(0.0,0.0) ), + dot( random(uv_index + vec2(1.0,0.0) ), uv_fract - vec2(1.0,0.0) ), blur.x), + mix( dot( random(uv_index + vec2(0.0,1.0) ), uv_fract - vec2(0.0,1.0) ), + dot( random(uv_index + vec2(1.0,1.0) ), uv_fract - vec2(1.0,1.0) ), blur.x), blur.y) + 0.5; +} + +vec4 get_black_and_white(vec4 color, vec2 uv, float time) { + if (color.a < 0.01) return color; + + float l = dot(color.rgb, vec3(0.2126, 0.7152, 0.0722)); + + bool is_black = l < threshold; + if (switch_colors) { + is_black = !is_black; + } + if (intensity < 1.0) { + vec2 coord = uv * scale + time * speed; + if (noise(coord) > intensity) { + is_black = !is_black; + } + } + + if (is_black) { + return black_color; + } + return white_color; +} diff --git a/game/shaders/get_black_n_white.gdshaderinc.uid b/game/shaders/get_black_n_white.gdshaderinc.uid new file mode 100644 index 0000000..86cfa9d --- /dev/null +++ b/game/shaders/get_black_n_white.gdshaderinc.uid @@ -0,0 +1 @@ +uid://bvklme5ww0g2f diff --git a/game/shaders/platform.gdshader b/game/shaders/platform.gdshader index 3ca545b..bbcec0a 100644 --- a/game/shaders/platform.gdshader +++ b/game/shaders/platform.gdshader @@ -1,10 +1,38 @@ shader_type canvas_item; - uniform float tile_size = 8.0; -uniform float platform_width = 16.0; +uniform float platform_width = 24.0; +uniform sampler2D atlas : filter_nearest; +uniform float seed : hint_range(0.0, 1.0) = 0.0; +const float left_cap_count = 2.0; +const float gap_count = 8.0; +const float right_cap_count = 2.0; + +float random(float v, float s) { + return fract(sin(v * 127.1 + s * 100.0) * 43758.5453); +} void fragment() { - + vec2 platform_pixel = UV * vec2(platform_width, tile_size); + vec2 atlas_pixel = platform_pixel; + + float column = floor(platform_pixel.x / tile_size); + float rand_val = random(column, seed); + float tile_offset_x = mod(platform_pixel.x, tile_size); + + if (platform_pixel.x < tile_size) { + float cap_index = floor(rand_val * left_cap_count); + atlas_pixel.x = cap_index * tile_size + tile_offset_x; + } else if (platform_pixel.x < platform_width - tile_size) { + float gap_index = floor(rand_val * gap_count); + atlas_pixel.x = left_cap_count * tile_size + gap_index * tile_size + tile_offset_x; + } else { + float cap_index = floor(rand_val * right_cap_count); + atlas_pixel.x = (left_cap_count + gap_count) * tile_size + cap_index * tile_size + tile_offset_x; + } + + vec2 atlas_size = vec2(textureSize(atlas, 0)); + vec2 atlas_uv = atlas_pixel / atlas_size; + COLOR = texture(atlas, atlas_uv); } diff --git a/images/level/platform.png b/images/level/platform.png index f959508..d374314 100644 --- a/images/level/platform.png +++ b/images/level/platform.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ea113a80d2b76536d74fd36c997e5028bfb2b177c7637a524413529261b1bf9d -size 227 +oid sha256:466b93aba54fada2909f51f7deee64f13f93d564c13f4f85c6a5c457dd669c13 +size 218