aboutsummaryrefslogtreecommitdiff
path: root/source/game/WorldManager.cs
blob: 31040f88cb463d007a1ec7f48bb73227e4f7800a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Threading.Tasks;
using Celesteia.Game.ECS;
using Celesteia.Game.Planets;
using Celesteia.Game.Planets.Generation;
using Microsoft.Xna.Framework;

namespace Celesteia.Game {
    public class WorldManager : GameComponent
    {
        private new GameInstance Game => (GameInstance) base.Game;
        public WorldManager(GameInstance Game) : base(Game) {}

        private GameWorld _loaded;

        private GameWorld LoadWorld(GameWorld gameWorld) {
            if (_loaded != null) _loaded.Dispose();

            return _loaded = gameWorld;
        }

        public async Task<GameWorld> LoadNewWorld(Action<string> progressReport = null, int? seed = null) {
            // Asynchronously generate the world.
            GameWorld generatedWorld = await Task.Run<GameWorld>(() => {
                GeneratedPlanet planet = new GeneratedPlanet(250, 75, seed);
                planet.Generate(new TerranPlanetGenerator(planet), progressReport);
                return new GameWorld(planet);
            });

            return LoadWorld(generatedWorld);
        }
    }
}