[ad_1]

I’m making an attempt to be taught to create a 2D RPG multiplayer sport. I attempt to be taught godot by making a single participant sport first. Nonetheless, there’s not a lot tutorial on creating enemies for multiplayer. The enemy I attempt to create embrace three properties:

  1. Can chase participant if any participant is in sure vary.
  2. Assault the participant if the participant is even nearer
  3. In any other case simply roaming round

I set a Sport Supervisor(var Gamers{}) to retailer my participant data and in addition set a gaggle for my gamers

What I’ve attempt is to calculate the closest participant and test the gap to determine how the enemy will carry out, however it’s doesn’t work for all of the enemies I spawned, it’s solely work for 1 enemy.

Right here is how I spawn my enemies, I am additionally struggling to synchronize there place, I’ve solely attempt add a multiplayer synchronizer node as a baby of enemy node and synchronize there place however it’s would not work.

extends Node2D

var tilemap

@export var max_enemies = 20
@export var existing_enemies = 5

var enemy_count = 0
var enemy_scene = preload("res://Scenes/Enemy.tscn")
var rng = RandomNumberGenerator.new()

func valid_spawn_location(place : Vector2):
    # Spwan solely on grass and sand
    var layer_name_1 = tilemap.get_layer_name(1)
    var layer_name_2 = tilemap.get_layer_name(2)

    var valid_location = (layer_name_1 == "grass") || (layer_name_2 == "sand")
    return valid_location

func spawn_enemy():
    var enemy = enemy_scene.instantiate() # Spawn enemy
    add_child(enemy)
    enemy.identify = "Enemy"
    
    enemy.dying.join(_on_enemy_death)
    
    # Spwan enemy on legitimate location
    var valid_location = false
    whereas !valid_location:
        enemy.place.x = rng.randi() % tilemap.get_used_rect().dimension.x
        enemy.place.y = rng.randi() % tilemap.get_used_rect().dimension.y
    
        valid_location = valid_spawn_location(enemy.place)
    
    print("enemy spawn: " + str(enemy.place.x) + " " + str(enemy.place.y))
    enemy.place = global_position
    enemy.spawn()
    

# Spawn the enemy when the sport begin (5)
func _ready():
    tilemap = get_tree().root.get_node("Foremost/Map")
    rng.randomize() # Initialize the randomizer

    for i in vary(existing_enemies):
        spawn_enemy()
    enemy_count = existing_enemies


# Spawn the enemy throughout the sport (15)
func _on_timer_timeout():
    print("spawn_check")
    if enemy_count < max_enemies:
        print("spwan")
        spawn_enemy()
        enemy_count = enemy_count + 1


func _on_enemy_death():
    enemy_count = enemy_count - 1

And here is how I arrange the enemy to work together with the gamers

func _on_timer_timeout():
    # Calculate the gap of the participant relative place to the enemy's place
    var gamers = get_tree().get_nodes_in_group("gamers")
    var player_distance
    var min_dist = INF
    var cur_dist
        
    # Iterate over gamers and calculate distance
    for participant in gamers:
        cur_dist = participant.place - place
        if cur_dist.size() <= min_dist:
            goal = participant
            min_dist = cur_dist.size()
        
    player_distance = cur_dist
    #print("goal: " + goal.identify + " / distance: " + str(player_distance.size()))

    # Assault radius
    if player_distance.size() <= 20:
        new_direction = player_distance.normalized()
        # sync_new_direction() 
        course = Vector2.ZERO  # Cease shifting whereas attacking

    # Chase radius
    elif player_distance.size() <= 100:
        course = player_distance.normalized()
        # print("chess:" + str(course))
        # sync_new_direction()

    # Random roam radius
    elif timer == 0:
        var random_direction = rng.randf()
        
        # enemy cease
        if random_direction < 0.05:
            course = Vector2.ZERO
        # enemy transfer
        elif random_direction < 0.1:
            course = Vector2.DOWN.rotated(rng.randf() * 2 * PI)
            
        sync_new_direction()

Thanks for understanding my questions and I’m wanting ahead for any enemy construction ideas or any tutorial I can comply with. Additionally I’ll present extra data if wanted.

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *