Karl Zylinski 6 месяцев назад
Родитель
Сommit
020de47c84
2 измененных файлов с 16 добавлено и 5 удалено
  1. 3 3
      examples/snake/shader.hlsl
  2. 13 2
      examples/snake/snake.odin

+ 3 - 3
examples/snake/shader.hlsl

@@ -5,7 +5,7 @@ struct vs_in {
 	float2 position : POS;
 	float2 uv       : UV;
 	float4 color    : COL;
-	float4 color2   : WAA;
+	float2 wobble   : WAA;
 };
 struct vs_out {
 	float4 position : SV_POSITION;
@@ -16,9 +16,9 @@ Texture2D    tex : register(t0);
 SamplerState smp : register(s0);
 vs_out vs_main(vs_in input) {
 	vs_out output;
-	output.position = mul(mvp, float4(input.position, 0, 1.0f));
+	output.position = mul(mvp, float4(input.position + input.wobble, 0, 1.0f));
 	output.uv = input.uv;
-	output.color = input.color2;
+	output.color = input.color;
 	return output;
 }
 float4 ps_main(vs_out input) : SV_TARGET {

+ 13 - 2
examples/snake/snake.odin

@@ -85,7 +85,7 @@ main :: proc() {
 		.RG32_Float,
 		.RG32_Float,
 		.RGBA8_Norm,
-		.RGBA8_Norm,
+		.RG32_Float,
 	})
 
 	prev_time := time.now()
@@ -96,11 +96,14 @@ main :: proc() {
 	head_sprite := k2.load_texture_from_file("head.png")
 	body_sprite := k2.load_texture_from_file("body.png")
 	tail_sprite := k2.load_texture_from_file("tail.png")
+	food_eaten_at := time.now()
+	started_at := time.now()
 
 	for !k2.window_should_close() {
 		time_now := time.now()
 		dt := f32(time.duration_seconds(time.diff(prev_time, time_now)))
 		prev_time = time_now
+		total_time := time.duration_seconds(time.diff(started_at, time_now))
 		k2.process_events()
 
 		if k2.key_is_held(.Up) {
@@ -151,6 +154,7 @@ main :: proc() {
 				snake_length += 1
 				snake[snake_length - 1] = next_part_pos
 				place_food()
+				food_eaten_at = time.now()
 			}
 
 			tick_timer = TICK_RATE + tick_timer
@@ -163,7 +167,14 @@ main :: proc() {
 			zoom = f32(WINDOW_SIZE) / CANVAS_SIZE,
 		}
 
-		k2.set_vertex_input_override(shader, 3, k2.create_vertex_input_override(k2.Color{255, 255, 255, 128}))
+
+		time_since_food := time.duration_seconds(time.diff(food_eaten_at, time_now))
+
+		if time_since_food < 0.5 && total_time > 1 {
+			k2.set_vertex_input_override(shader, 3, k2.create_vertex_input_override(k2.Vec2{f32(math.cos(total_time*100)), f32(math.sin(total_time*120 + 3))}))
+		} else {
+			k2.set_vertex_input_override(shader, 3, {})
+		}
 
 		k2.set_camera(camera)
 		food_sprite.width = CELL_SIZE