Download

Need help with logical layout of multiplayer code.

I have create some code inside GameModeBase which searches for all spawn points on the level, the thing is, when a player activates a new zone, ie opens a door, i want to activate spawn points inside that, problem i have is its going to be 4 player co-op, what would be the sanest way of activating zones, so the zone becomes actives, and the spawner becomes active, only the server (a client also) will spawn the enemies from the spawnpoint. Here is my code in GameModeBase, maybe someone can help me with the logic and ideas on a way to solve said problem.The reason i need guidance is GameModeBase is owned by the server, but any client can open a new zone.



// Fill out your copyright notice in the Description page of Project Settings.

#include "SZombieGameMode.h"
#include "Engine.h"
#include "SZombieSpawn.h"

ASZombieGameMode::ASZombieGameMode(const class FObjectInitializer&ObjectInitializer) : Super(ObjectInitializer)
{
    //Preset defaults for the gamemode.
    ZombieHealth = 150;
    ZombieStartHealth = 150;
    ZombieHealthIncrease = 100;
    ZombieHealthMulitplier = 0.1;
    CurrentWave = 0;
    bAreZombiesStillAlive = false;
    MaxZombiesToSpawnAtOnce = 30; //Hard limit of 30 zombies alive at one time on the map.
    ZombiesPerPlayer = 6; //Hard limit of the zombies per player.
}

void ASZombieGameMode::SpawnZombie()
{

}

void ASZombieGameMode::StartPlay()
{
    //Get all spawn points for the level.
    GetSpawnPoints();
    ActivateZone(StartingZone);
}

void ASZombieGameMode::PrepareWave()
{
    //Get the total zombies to spawn this wave.
    MaxZombiesInWave = CalculateAmountOfZombies();

}

void ASZombieGameMode::UpdateWaveCounter()
{
    //Simply increment the current round we are on
    CurrentWave++;
}

void ASZombieGameMode::AdjustZombies()
{

}

float ASZombieGameMode::CalculateAmountOfZombies()
{
    float zombiesToSpawn = 0;
    float zombiesToSpawnMultiplier = CurrentWave / 5;
    if (zombiesToSpawnMultiplier < 1)
    {
        zombiesToSpawnMultiplier = 1;
    }

    if (CurrentWave >= 10)
    {
        zombiesToSpawnMultiplier *= CurrentWave * 0.15;
    }

    if (GetTotalPlayersInGame() == 1)
    {
        zombiesToSpawn += int((0.5 * ZombiesPerPlayer) * zombiesToSpawnMultiplier);
    }
    else
    {
        zombiesToSpawn += int(((GetTotalPlayersInGame() - 1) * ZombiesPerPlayer) * zombiesToSpawnMultiplier);
    }

    return zombiesToSpawn;
}



int32 ASZombieGameMode::GetTotalPlayersInGame()
{
    //Eventually we will use this to return how many players are in the game.
    return 1;
}

void ASZombieGameMode::IncreaseZombieHealth()
{
    ZombieHealth = ZombieStartHealth;
    for (int i = 2; i <= CurrentWave; i++)
    {
        if (i >= 10)
        {
            ZombieHealth += FMath::FloorToInt(ZombieHealth * ZombieHealthMulitplier);
        }
        else
        {
            ZombieHealth = FMath::FloorToInt(ZombieHealth + ZombieHealthIncrease);
        }
    }
}

void ASZombieGameMode::SpeedUpLastZombie()
{
    if (CurrentWave < 3)
    {
        return;
    }

    //Will implement logic to speed up the last zombie.
}

void ASZombieGameMode::GetSpawnPoints()
{

    //Iterate through the current level and find all Zombie spawn points, then add them to the database.
    for (TActorIterator<ASZombieSpawn> ZombieSpawnItr(GetWorld()); ZombieSpawnItr; ++ZombieSpawnItr)
    {
        ASZombieSpawn* spawn = *ZombieSpawnItr;
        SpawnPoints.Add(spawn);
    }
}


void ASZombieGameMode::CalculatePlayerPointBonus()
{

}

void ASZombieGameMode::ActivateZone(ESpawnZones zone)
{
    //Loop through all the spawn points in the array
    for (ASZombieSpawn* spawn : SpawnPoints)
    {
        //If they match our zone, activate it.
        if (spawn->GetZone() == zone)
        {
            spawn->ActivateZone();
        }
    }
}