Ok, dnes sme pohli s našou malou hrou Červíci bombíci á pridali sme obávaný pojem, ktorý som si kompletne vymyslel, Touch matrix ✓

Ok, dnes sme pohli s našou malou hrou Červíci bombíci á pridali sme obávaný pojem, ktorý som si kompletne vymyslel, Touch matrix ✓

Takže hovorili sme o Touch matrix pre našu hru Worms bamers https://hrubos.tech/repository/ , Bombíci červíci, toto ňou myslím:

https://hrubos.tech/blogy/content/images/20260127184614-006_touch_matrix.png

https://hrubos.tech/blogy/content/images/20260127184614-006_touch_matrix.png

Á takto to beží s výpismi, či zostrelíme bližší alebo necháme vzdialenejší šesťuholníček: https://hrubos.tech/blogy/content/images/20260127184752-005_20260127_182802.png

extends CharacterBody2D
@onready var line: Line2D = $Line2D
@onready var red_texture: Texture2D = preload("res://assets/tile2.png")
@onready var green_texture: Texture2D = preload("res://assets/tile1.png")

const SPEED = 300.0
const JUMP_VELOCITY = -400.0
func _ready():
    line.clear_points()

func _input(event):
    if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
        var click_pos: Vector2 = get_global_mouse_position()
        draw_line_to(click_pos)
        $"../GPUParticles2D".position=click_pos
        await get_tree().create_timer(0.4).timeout
        if is_click_on_static_body(click_pos):
            #print(await which_click_on_static_body(click_pos))
            await which_click_on_static_body(click_pos)


func is_click_on_static_body(world_pos: Vector2) -> bool:
    var space := get_world_2d().direct_space_state

    var query := PhysicsPointQueryParameters2D.new()
    query.position = world_pos
    query.collide_with_bodies = true
    query.collide_with_areas = false

    var result := space.intersect_point(query)

    for hit in result:
        if hit.collider is StaticBody2D:
            return true

    return false


func find_sprite_2d(node: Node) -> Sprite2D:
    if node is Sprite2D:
        return node

    for child in node.get_children():
        var result := find_sprite_2d(child)
        if result:
            return result

    return null

func get_static_body_positions(root: Node) -> Array[Vector2]:
    var positions: Array[Vector2] = []

    if root is StaticBody2D and root.name.begins_with("StaticBody2D"):
        positions.append(root.global_position)

    for child in root.get_children():
        positions.append_array(get_static_body_positions(child))

    return positions

# Returns all points that are roughly in the same Y-plane as hit_pos (within delta_y)
# and are located to the right (x > hit_pos.x)
func get_points_right_of_hit_in_y_plane(hit_pos: Vector2, points: Array, delta_y: float = 4.0) -> bool:
    var result: Array = []

    for pos in points:
        # 1️⃣ Check if Y is within delta
        if abs(pos.y - hit_pos.y) <= delta_y:
            # 2️⃣ Check if X is to the right of hit
            if pos.x > hit_pos.x:
                #result.append(pos)
                return true

    #return result
    return false



func which_click_on_static_body(world_pos: Vector2) -> StringName:
    var space := get_world_2d().direct_space_state
    var positions := get_static_body_positions(get_tree().current_scene)
    positions.sort_custom(func(a: Vector2, b: Vector2) -> bool:
        return a.y < b.y
    )

    var query := PhysicsPointQueryParameters2D.new()
    query.position = world_pos
    query.collide_with_bodies = true
    query.collide_with_areas = false

    var result := space.intersect_point(query)

    for hit in result:
        if hit.collider is StaticBody2D:
            var sprite := find_sprite_2d(hit.collider)
            if sprite:
                sprite.texture = red_texture
                print("Hit position is "+str(hit.collider.position)+" can be shot or not: "+str(!get_points_right_of_hit_in_y_plane(hit.collider.position, positions)))
                print(positions)
                if get_points_right_of_hit_in_y_plane(hit.collider.position, positions):
                    await get_tree().create_timer(0.4).timeout
                    sprite.texture = green_texture
            return hit.collider.name

    return ""


func draw_line_to(target_pos: Vector2):
    line.clear_points()
    line.add_point(Vector2.ZERO) # pozícia CharacterBody2D
    line.add_point(to_local(target_pos))


func _physics_process(delta: float) -> void:
    # Add the gravity.
    if not is_on_floor():
        velocity += get_gravity() * delta

    # Handle jump.
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = JUMP_VELOCITY

    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var direction := Input.get_axis("ui_left", "ui_right")
    if direction==1.0:
        velocity.x = direction * SPEED
        $CollisionPolygon2D/Sprite2D.flip_h=true
    elif direction==-1.0:
        velocity.x = direction * SPEED
        $CollisionPolygon2D/Sprite2D.flip_h=false
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)

    move_and_slide()


func _on_timer_timeout() -> void:
    draw_line_to(Vector2($".".position))
    $"../GPUParticles2D".position=Vector2(-500,-500)


Author: AarNoma

The first Slovak cyborg 1 system

Comments “Ok, dnes sme pohli s našou malou hrou Červíci bombíci á pridali sme obávaný pojem, ktorý som si kompletne vymyslel, Touch matrix ✓”