From 567c422f8cd42eba2437f9a8c2522716a1649be7 Mon Sep 17 00:00:00 2001 From: hazel Date: Mon, 26 Jan 2026 22:04:39 +0100 Subject: celesteia archive, last updated april 9th 2024 Signed-off-by: hazel --- source/game/ecs/EntityFactory.cs | 111 +++++++++++++++++++++++++++++++++++++++ source/game/ecs/GameWorld.cs | 41 +++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 source/game/ecs/EntityFactory.cs create mode 100644 source/game/ecs/GameWorld.cs (limited to 'source/game/ecs') diff --git a/source/game/ecs/EntityFactory.cs b/source/game/ecs/EntityFactory.cs new file mode 100644 index 0000000..482b137 --- /dev/null +++ b/source/game/ecs/EntityFactory.cs @@ -0,0 +1,111 @@ +using Celesteia.Resources; +using Microsoft.Xna.Framework; +using MonoGame.Extended; +using MonoGame.Extended.Entities; +using Celesteia.Resources.Sprites; +using Celesteia.Game.Components.Player; +using MonoGame.Extended.TextureAtlases; +using Microsoft.Xna.Framework.Graphics; +using Celesteia.Game.Input; +using Celesteia.Game.Components; +using Microsoft.Xna.Framework.Input; +using Celesteia.Game.Components.Physics; +using Celesteia.Game.Components.Items; +using Celesteia.Game.Components.Skybox; +using Celesteia.Resources.Types; +using Celesteia.Game.Input.Definitions.Keyboard; +using Celesteia.Game.Input.Definitions.Gamepad; +using Celesteia.Game.Input.Definitions.Mouse; +using MonoGame.Extended.Input; +using Celesteia.Game.Input.Definitions; +using Celesteia.Game.Input.Conditions; + +namespace Celesteia.Game.ECS { + /* + Contains various commonly used prefabrications for entities. + Many of the functions were moved to EntityTypes. + */ + + public class EntityFactory { + private readonly GameWorld _gameWorld; + + public EntityFactory(GameWorld gameWorld) => _gameWorld = gameWorld; + + public Entity CreateEntity(NamespacedKey key) => CreateEntity(ResourceManager.Entities.GetResource(key) as EntityType); + + public Entity CreateEntity(EntityType type) + { + Entity entity = _gameWorld.CreateEntity(); + type.Instantiate(entity); + + return entity; + } + + public static void BuildPlayer(Entity entity, Texture2D sprites) { + entity.Attach(new Transform2()); + + entity.Attach(new TargetPosition()); + + entity.Attach(new EntityFrames( + TextureAtlas.Create("player", sprites, 24, 24), + 0, 2, + ResourceManager.SPRITE_SCALING + )); + + entity.Attach(new Inventory(36, + new ItemStack(NamespacedKey.Base("iron_pickaxe"), 1), + new ItemStack(NamespacedKey.Base("wooden_torch"), 10) + )); + + entity.Attach(new PhysicsEntity(1.6f, true)); + + entity.Attach(new CollisionBox(1.5f, 3f)); + + entity.Attach(new PlayerInput() { + Horizontal = new AverageCondition( + new TrinaryKeyboardDefinition() { Negative = Keys.A, Positive = Keys.D, PollType = InputPollType.Held }, + new SensorGamepadDefinition() { Sensor = GamePadSensor.LeftThumbStickX } + ), + Run = new AnyCondition( + new BinaryKeyboardDefinition() { Keys = Keys.LeftShift, PollType = InputPollType.Held }, + new BinaryGamepadDefinition() { Buttons = Buttons.LeftShoulder, PollType = InputPollType.Held } + ), + Jump = new AnyCondition( + new BinaryKeyboardDefinition() { Keys = Keys.Space, PollType = InputPollType.Held }, + new BinaryGamepadDefinition() { Buttons = Buttons.A, PollType = InputPollType.Held } + ), + Inventory = new AnyCondition( + new BinaryKeyboardDefinition() { Keys = Keys.B, PollType = InputPollType.Pressed }, + new BinaryGamepadDefinition() { Buttons = Buttons.Y, PollType = InputPollType.Pressed } + ), + Crafting = new AnyCondition( + new BinaryKeyboardDefinition() { Keys = Keys.C, PollType = InputPollType.Pressed }, + new BinaryGamepadDefinition() { Buttons = Buttons.X, PollType = InputPollType.Pressed } + ), + Pause = new AnyCondition( + new BinaryKeyboardDefinition() { Keys = Keys.Escape, PollType = InputPollType.Pressed }, + new BinaryGamepadDefinition() { Buttons = Buttons.Start, PollType = InputPollType.Pressed } + ), + PrimaryUse = new AnyCondition( + new BinaryMouseDefinition() { Button = MouseButton.Left, PollType = InputPollType.Held }, + new BinaryGamepadDefinition() { Buttons = Buttons.RightTrigger, PollType = InputPollType.Held } + ), + SecondaryUse = new AnyCondition( + new BinaryMouseDefinition() { Button = MouseButton.Right, PollType = InputPollType.Held }, + new BinaryGamepadDefinition() { Buttons = Buttons.LeftTrigger, PollType = InputPollType.Held } + ) + }); + + entity.Attach(new LocalPlayer()); + + entity.Attach(new CameraFollow()); + + entity.Attach(new EntityAttributes(new EntityAttributes.EntityAttributeMap() + .Set(EntityAttribute.MovementSpeed, 12.5f) + .Set(EntityAttribute.JumpFuel, .5f) + .Set(EntityAttribute.JumpForce, 10f) + .Set(EntityAttribute.BlockRange, 7f) + )); + } + } +} \ No newline at end of file diff --git a/source/game/ecs/GameWorld.cs b/source/game/ecs/GameWorld.cs new file mode 100644 index 0000000..9ee516e --- /dev/null +++ b/source/game/ecs/GameWorld.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using Celesteia.Game.Components.Entity; +using Celesteia.Game.Planets; +using Celesteia.Game.Systems; +using Microsoft.Xna.Framework; +using MonoGame.Extended.Entities; +using MonoGame.Extended.Entities.Systems; + +namespace Celesteia.Game.ECS { + public class GameWorld : IDisposable { + public ChunkMap ChunkMap { get; private set; } + public GameWorld(ChunkMap map) => ChunkMap = map; + + public void Dispose() { + _w.Dispose(); + ChunkMap = null; + } + + private World _w; + private WorldBuilder _builder; + + public WorldBuilder BeginBuilder() => _builder = new WorldBuilder(); + + public WorldBuilder AddSystem(ISystem system) => _builder.AddSystem(system); + + public void EndBuilder() => _w = _builder.Build(); + + public Entity CreateEntity() { + Entity e = _w.CreateEntity(); + e.Attach(new GameWorldEntity(this, e.Id)); + + return e; + } + + public void DestroyEntity(int id) => _w.DestroyEntity(id); + + public void Update(GameTime gt) => _w.Update(gt); + public void Draw(GameTime gt) => _w.Draw(gt); + } +} \ No newline at end of file -- cgit v1.2.3