0 votes

I have been working on this project for weeks where map is generated randomly using diggers.

For spawning enemies. The vector2 position that the digger navigates through are stored into a dictionary spawn_tiles .But i'm unable to spawn enemies in a valid tile position valid tile being Vector2 of spawn tiles.

I'm trying to pick a random item form the spawn_tiles dictionary. And then add_child(enemy). All i get is errors so far.

Here is the code of Main.gd:-

extends Control

const FLOOR = -1
const WALL = 0

const gridSize = Vector2(160,80)
const gridSize_floor = Vector2(80,50)
var diggers = []
onready var tilemapo = $Mapo
onready var tilemapi = $Mapi
onready var tilemapj = $Mapj

var tile
var map: Map
var num = 5


func _ready():
    load_game()

func load_game():
    randomize()
    map = Map.new(tilemapo, tilemapi, tilemapj, gridSize, Vector2(1,1))
    gen_map()
    $Player.position = map.world_center() + Vector2.ONE * 16
    spawn_enemy(num)
    set_process(true)

func spawn_enemy(num):
    for i in range(num):
        var enemy = enemy1.instance()
        enemy_container.add_child(enemy)
        var random_tiles = Vector2(x, y)
        random_tiles = randomize().spawn_tiles
        enemy.set_position(Vector2(x, y))

func dig_map(digTile:int = FLOOR):
    while diggers.size() > 0:
        for i in range(diggers.size() - 1, -1, -1):
            diggers[i].dig(digTile)
            diggers[i].move()
            if diggers[i].is_dead:
                diggers.remove(i)

func _process(delta):
    if Input.is_action_just_pressed("ui_accept"):
        get_tree().reload_current_scene()
        _ready()
    elif Input.is_action_just_pressed("ui_cancel"):
        get_tree().quit()

    func gen_map():
    map.clear()
    map.fill(WALL)
    #diggers.append(Digger.new(map, map.center_coord(), 0.25, .0003))
    #diggers.append(Digger.new(map, Vector2.ZERO, 0.35, .0005))
    diggers.append(Digger.new(map, map.center_coord(), 0.55, .01, Vector2.LEFT))
    diggers.append(Digger.new(map, map.center_coord() + Vector2.RIGHT, 0.55, .01, Vector2.RIGHT))
    diggers.append(Digger.new(map, map.center_coord() + Vector2.UP, 0.55, .01, Vector2.UP))
    diggers.append(Digger.new(map, map.center_coord() + Vector2.DOWN, 0.55, .01, Vector2.DOWN))
    dig_map(FLOOR)

    map.update_bitmask_all()

Here is the code for Map.gd:-

extends Node2D
class_name Map

var tiles1: TileMap
var tiles2: TileMap
var tiles3: TileMap
var size: Vector2 setget ,get_size
#how much border should there be at the edge (digger will not dig past this)
var border : Vector2
var spawn_tiles={}

func get_size():
    return size

func _init(tmap1:TileMap, tmap2:TileMap, tmap3:TileMap, map_size:Vector2, borderSize:Vector2 = Vector2.ZERO):
    tiles1 = tmap1
    tiles2 = tmap2
    tiles3 = tmap3
    size = map_size
    border = borderSize

func clear():
    tiles1.clear()
    tiles2.clear()
    tiles3.clear()

func world_center():
    return tiles1.map_to_world(size / 2.0)

func center_coord():
    return size / 2

func set_cell(x:int, y:int, tile:int):
    if _is_valid(x,y):
        tiles1.set_cell(x,y,tile)
        tiles2.set_cell(x,y,tile)
        tiles3.set_cell(x,y,tile)
        if tile==FLOOR:
            spawn_tiles[Vector2(x,y)]=FLOOR
        else:
            spawn_tiles.remove(Vector2(x,y))
        return true
    else:
        return false

func get_cell(x:int, y:int):
    return [
        tiles1.get_cell(x,y),
        tiles2.get_cell(x,y), 
        tiles3.get_cell(x,y),
        ]

func _is_valid(x:int, y:int):
    return x >= border.x && x < size.x - border.x \
    && y >= border.y && y < size.y - border.y

func fill(tile:int):
    for y in size.y:
        for x in size.x:
            tiles1.set_cell(x, y, tile)
            tiles2.set_cell(x, y, tile)
            tiles3.set_cell(x, y, tile)

func update_bitmask_all():
    tiles1.update_bitmask_region(Vector2.ZERO, size)
    tiles2.update_bitmask_region(Vector2.ZERO, size)
    tiles3.update_bitmask_region(Vector2.ZERO, size)

func get_percent_tile(tile:int):
    var t = tiles1.get_used_cells_by_id(tile).size()
    return t / (size.x * size.y)
in Engine by (58 points)

What errors are you getting? You have better chances of someone helping you if you break your problem into smaller parts. Print the results you expect in different parts of the code, to try to understand what is not going on correctly.

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.