0 votes

I'm making a game with a grid with a player that has grid-based movement. An aiming reticle will show when i press a certain key and then hides it when i release that key. The player is in the grid like i want it to; but the reticle isn't, can you tell me how to attach it to the grid please here's the code.

Grid:

    extends TileMap

    enum ENTITY_TYPE {PLAYER, OBJECT, COLLECTIBLE}

    var tile_size = get_cell_size()
    var half_tile_size = tile_size / 2
    var grid_size = Vector2(16, 8)

    var grid = []
    onready var player = preload("res://Scenes/Entities/Player.tscn")

    func _ready():

        for x in range(grid_size.x):
            grid.append([])
            for y in range(grid_size.y):
                grid[x].append(null)

        #Spawn Player
        var new_player = player.instance()
        new_player.set_pos(map_to_world(Vector2( 4, 4)) + half_tile_size)
        add_child(new_player)

        var position = []
        for x in range(5):
            var placed = false 
            while not placed:
                var grid_pos = Vector2(randi() % int(grid_size.x), randi() % int(grid_size.y))
                if not grid[grid_pos.x][grid_pos.y]:
                    if not grid_pos in position:
                        position.append(grid_pos)
                        placed = true

    func get_cell_content(pos = Vector2()):
        return grid[pos.x][pos.y]

    func is_cell_vacent(pos = Vector2(), direction = Vector2()):
        var grid_pos = world_to_map(pos) + direction

        if grid_pos.x < grid_size.x and grid_pos.x >= 0:
            if grid_pos.y < grid_size.y and grid_pos.y >= 0:
                return true if grid[grid_pos.x][grid_pos.y] == null else false 
        return false 

    func update_child_pos(new_pos, direction, type):
        var grid_pos = world_to_map(new_pos)
        print(grid_pos)
        grid[grid_pos.x][grid_pos.y] = null

        var new_grid_pos = grid_pos + direction
        grid[new_grid_pos.x][new_grid_pos.y] = type

        var target_pos = map_to_world(new_grid_pos) + half_tile_size
        return target_pos

Grid Visualizer:

extends Node2D

onready var Grid = get_parent()

func _ready():
    set_opacity(0.2)

func _draw():
    var LINE_COLOR = Color(0, 0, 0)
    var LINE_WIDTH = 2
    var window_size = OS.get_window_size()

    for x in range(Grid.grid_size.x + 1):
        var col_pos = x * Grid.tile_size.x
        var limit = Grid.grid_size.y * Grid.tile_size.y
        draw_line(Vector2(col_pos, 0), Vector2(col_pos, limit), LINE_COLOR, LINE_WIDTH)
    for y in range(Grid.grid_size.y + 1):
        var row_pos = y * Grid.tile_size.y
        var limit = Grid.grid_size.x * Grid.tile_size.x
        draw_line(Vector2(0, row_pos), Vector2(limit, row_pos), LINE_COLOR, LINE_WIDTH)

Player:

extends KinematicBody2D

#Grid Movement Varibles
var direction = Vector2()

const MAX_SPEED = 400

var speed = 0
var velocity = Vector2()

#Grid Direction Varibles 
var target_pos = Vector2()
var target_direction = Vector2()
var is_moving = false

#Grid Varibles
var grid
var type

#Aiming Varibles
onready var r = get_node("Aiming_Reticle")
onready var g = get_node("Aiming_Reticle1")
onready var b = get_node("Aiming_Reticle2")
onready var t = get_node("Aiming_Reticle3")

func _ready():
    grid = get_parent()
    type = grid.PLAYER
    set_fixed_process(true)
    set_process_input(true)
    r.hide()
    g.hide()
    b.hide()
    t.hide()

func _fixed_process(delta):
    direction = Vector2()
    speed = 0

    if Input.is_action_pressed("ui_up"):
        direction.y = -1

    elif Input.is_action_pressed("ui_down"):
        direction.y = 1 

    if Input.is_action_pressed("ui_left"):
        direction.x = -1

    elif Input.is_action_pressed("ui_right"):
        direction.x = 1

    if not is_moving and direction != Vector2():
        target_direction = direction.normalized()
        if grid.is_cell_vacent(get_pos(), direction):
            target_pos = grid.update_child_pos(get_pos(), direction, type)
            is_moving = true

    elif is_moving:
        speed = MAX_SPEED
        velocity = speed * target_direction * delta

        var pos = get_pos()
        var distance_to_target = pos.distance_to(target_pos)
        var move_distance = velocity.length()

        if move_distance > distance_to_target:
            velocity = target_direction * distance_to_target
            is_moving = false 

        move(velocity)

func _input(event):

    if event.is_action_pressed("ui_event 1"):
        r.show()
    if event.is_action_pressed("ui_event 2"):
        g.show()
    if event.is_action_pressed("ui_event 3"):
        t.show()
    if event.is_action_pressed("ui_event 4"):
        b.show()

    if event.is_action_released("ui_event 1"):
        r.hide()
    if event.is_action_released("ui_event 2"):
        g.hide()
    if event.is_action_released("ui_event 3"):
        t.hide()
    if event.is_action_released("ui_event 4"):
        b.hide()
in Engine by (41 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.