https://youtu.be/-CArr6225rI

Developer: Jannick Schalkwijk & Ian Kleton
Artists: Nog zoekend
Genre: 2D Survival/ RPG/ Multiplayer
Code: C# Unity

Beschrijving: Een Multiplayer Survival /RPG game waarbij je resources verzamelt om te craften. Met de items die je maakt dood je  bosses om zo nieuwe gebieden te unlocken. Je character heeft levels en skills die ook gebruikt worden om de bosses te doden.


We werken volgens een zelf geschreven GameDesign en een Technisch design en schetsen al onze ideeen eerst uit.




Code van de World generation met gebruik van de perlinnoise, Alle data wordt opgeslagen in een Byte Array die door wordt gestuurd bij de Multiplayer om zo min mogelijk data te verspillen. Er zitten parameters in die de caves vergroten en verkleinen, minimum waardes aan ores geven en andere belangrijke parameters bij de generation.

using UnityEngine;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;

public class Perlinnoise2D : MonoBehaviour
{
    public bool Generate;
    public string fileName = "InsertWorldNameHere";
    public bool Load;
    public float Seed;
    public float PerlinScale;
    public float cave;
    public int Width;
    public int Depth;
    public int Height;
    public int SurfaceHeight;
    public Texture[] OreTexture;
    public float[] OreSpawnRate;
    public int[] OreMinimumDepth;
    public byte[, , ,] World;
    public bool Finished;
    private Thread GenerationThread;
    private Thread TSave;
    private Thread TLoad;
    private Stopwatch GenerationTimer = new Stopwatch();
    public float save = 0;
    BinaryFormatter bf;// = new BinaryFormatter();
    FileStream fs;// = new FileStream(fileName, FileMode.Create, FileAccess.Write);
    System.Random r;
    void Start()
    {
        if (Generate)
        {
            

            bf = new BinaryFormatter();
            if (!Load)
            {
                World = new byte[4, Width * (Depth + Height), 256, 256];
                if (Seed == 0)//of er een seed gekozen is, zo niet word er een aangemaakt
                {
                    Seed = Random.Range(0f, 99999f);
                }
                r = new System.Random();
                GenerationThread = new Thread(FillArray);
                GenerationThread.Start();
            }
            else
            {
                TLoad = new Thread(LoadWorld);
                TLoad.Start();
            }
        }
        else
        {
            World = new byte[1,1, 256, 256];
        }
    }

    void FixedUpdate()
    {
        if (Generate)
        {
            save -= Time.deltaTime;
            if (Finished && save<0)
            {
                save = 60;
                TSave = new Thread(SaveWorld);
                TSave.Start();
            }
        }
    }

    void SaveWorld()
    {
        fs = new FileStream(fileName + ".nsfw", FileMode.Create, FileAccess.Write);
        bf.Serialize(fs, World);
        UnityEngine.Debug.Log("Saved");
        fs.Close();
    }

    void LoadWorld()
    {
        save = 60;
        fs = new FileStream(fileName + ".nsfw", FileMode.Open, FileAccess.Read);
        World = (byte[,,,])bf.Deserialize(fs);
        UnityEngine.Debug.Log("Loaded");
        Finished = true;
        fs.Close();
    }

    /// <summary>
    /// generates the world
    /// </summary>

    void FillArray()
    {
        try{
            GenerationTimer.Start();
            for (int y = 0; y < 256 * (Depth + Height); y++)
            {

                for (int x = 0; x < 256 * Width; x++)
                {
                    int chunk = (Mathf.FloorToInt(x / 256) * (Depth + Height)) + Mathf.FloorToInt(y / 256);
                    World[3, chunk, (x % 256), 255 - (y % 256)] = (byte)Mathf.FloorToInt(r.Next(3));
                    if (y == 256 * (Depth + Height) - 1)//laatste rij aan de onderkant van de wereld
                    {
                        World[0, chunk, (x % 256), 255 - (y % 256)] = 3;
                    }
                    else
                    {
                        float height = Mathf.Abs(Mathf.PerlinNoise(Seed + (x * PerlinScale), Seed) * 0.75f + Mathf.PerlinNoise(Seed * 2 + (x * PerlinScale), Seed) * 0.75f);//hoogte van het oppervlakte ophalen
                        float del = Mathf.Abs(Mathf.PerlinNoise(Seed + (x * PerlinScale), Seed + (y * PerlinScale)));//cave diepte ophalen
                        if (del > cave + ((y - Height * 256) * (PerlinScale / (((Depth * 128)) / 2))) && y - Height * 256 == (Mathf.Floor(height * SurfaceHeight)))//checken of het onder het oppervlakte zit
                        {
                            World[0, chunk, (x % 256), 255 - (y % 256)] = (byte)1;

                        }
                        else if ( y - Height * 256 > (Mathf.Floor(height * SurfaceHeight)))
                        {
                            World[1, chunk, (x % 256), 255 - (y % 256)] = (byte)1;//Background neerzetten
                            if (del > cave + ((y - Height * 256) * (PerlinScale / (((Depth * 128)) / 2))))//checken of er een cave zit
                            {
                                for (int i = 0; i < OreSpawnRate.Length; i++)//door alle soorten ores en stone loopen
                                {
                                    if (Mathf.Abs(Mathf.PerlinNoise(((Seed * (i + 2)) % 99999) + (x * PerlinScale / 0.75f), ((Seed * (i + 2)) % 99999) * (i + 2) + ((y - (Height * 256)) * PerlinScale / 0.75f))) < OreSpawnRate[i] && (y - (Height * 256)) > OreMinimumDepth[i])//checken of die ore op die plek moet spawnen
                                    {
                                        World[0, chunk, (x % 256), 255 - (y % 256)] = (byte)(i + 3);
                                    }
                                }
                                if (World[0, chunk, (x % 256), 255 - (y % 256)] == (byte)0)//als geen van al die ores gespawnt is dan word het dirt
                                {
                                    World[0, chunk, (x % 256), 255 - (y % 256)] = (byte)2;
                                }
                            }
                            else
                            {
                                World[0, chunk, (x % 256), 255 - (y % 256)] = (byte)0;//cave
                            }
                        }
                        if (World[0, chunk, (x % 256), 255 - (y % 256)] + World[1, chunk, (x % 256), 255 - (y % 256)] == 0) {
                            for (int i = 0; i < 7; i++)
                            {
                                for (int j = 0; j < 7; j++)
                                {
                                    try
                                    {


                                        if (i == 0 || i == 6 || j == 0 || j == 6)
                                        {
                                            if (World[2, (Mathf.FloorToInt((x + (i - 3)) / 256) * (Depth + Height)) + Mathf.FloorToInt((y + (j - 3)) / 256), ((x + (i - 3)) % 256), 255 - ((y + (j - 3)) % 256)] < 1)
                                            {
                                                World[2, (Mathf.FloorToInt((x + (i - 3)) / 256) * (Depth + Height)) + Mathf.FloorToInt((y + (j - 3)) / 256), ((x + (i - 3)) % 256), 255 - ((y + (j - 3)) % 256)] = 1;
                                            }
                                        }
                                        else if ((i == 1 || i == 5 && j<6&&j>0)|| (j == 1 || j == 5&&i<6&&i>0))
                                        {
                                            if (World[2, (Mathf.FloorToInt((x + (i - 3)) / 256) * (Depth + Height)) + Mathf.FloorToInt((y + (j - 3)) / 256), ((x + (i - 3)) % 256), 255 - ((y + (j - 3)) % 256)] < 2)
                                            {
                                                World[2, (Mathf.FloorToInt((x + (i - 3)) / 256) * (Depth + Height)) + Mathf.FloorToInt((y + (j - 3)) / 256), ((x + (i - 3)) % 256), 255 - ((y + (j - 3)) % 256)] = 2;
                                            }
                                        }
                                        else if ((i == 2 || i == 4&&j<5&&j>1 )|| (j == 2 || j == 4&&i<5&&i>1))
                                        {
                                            if (World[2, (Mathf.FloorToInt((x + (i - 3)) / 256) * (Depth + Height)) + Mathf.FloorToInt((y + (j - 3)) / 256), ((x + (i - 3)) % 256), 255 - ((y + (j - 3)) % 256)] < 3)
                                            {
                                                World[2, (Mathf.FloorToInt((x + (i - 3)) / 256) * (Depth + Height)) + Mathf.FloorToInt((y + (j - 3)) / 256), ((x + (i - 3)) % 256), 255 - ((y + (j - 3)) % 256)] = 3;
                                            }
                                        }
                                    }
                                    catch
                                    { }
                                }
                            }
                        }
                    }
                }
            }
            GenerationTimer.Stop();
            UnityEngine.Debug.Log("finished in " + GenerationTimer.Elapsed + " seconds");
            Finished = true;
        }
        catch(System.Exception e) { UnityEngine.Debug.Log(e); }
    }
}

Categories

Powered by Blogger.

Languages & Programs

Expert
Unity
Visual Studio
C#

Advanced
Unity Editor Tools
Javascript
Xamarin

Intermediate
CG
ASP.NET
HTML
PHP
PHP (Laravel)
Node.JS

Beginner
C++
Unreal Engine
CSS

- Copyright © Ian Kleton