From 7d7b05ebc1761df4295e571429dea6c71f880bf8 Mon Sep 17 00:00:00 2001 From: Klaus A Date: Sun, 25 Feb 2018 03:40:17 +0100 Subject: [PATCH 1/9] Refactoring, Entity- and Componentsystem --- .../OctoAwesome.Basics/CollisionPlane.cs | 38 +-- .../Blocks/BirchWoodBlockDefinition.cs | 16 +- .../Entities/WauziEntity.cs | 83 ++++-- .../EntityComponents/BodyComponent.cs | 6 +- .../EntityComponents/BodyPowerComponent.cs | 7 +- .../EntityComponents/BoxCollisionComponent.cs | 109 ++++++- .../EntityComponents/CollisionComponent.cs | 3 +- .../EntityComponents/ControllableComponent.cs | 10 +- .../EntityComponents/ForceComponent.cs | 1 + .../EntityComponents/HeadComponent.cs | 6 +- .../EntityComponents/InventoryComponent.cs | 4 +- .../EntityComponents/MassComponent.cs | 3 +- .../EntityComponents/MoveableComponent.cs | 6 +- .../EntityComponents/PositionComponent.cs | 10 +- .../EntityComponents/PowerComponent.cs | 1 + .../EntityComponents/RenderComponent.cs | 6 +- .../EntityComponents/ToolBarComponent.cs | 9 +- OctoAwesome/OctoAwesome.Basics/Extension.cs | 25 +- .../OctoAwesome.Basics.csproj | 11 + .../SimComponents/PhysicEngineComponent.cs | 267 ++++++++++++++++++ .../AccelerationComponent.cs | 28 +- .../BlockInteractionComponent.cs | 19 +- .../CollisionComponent.cs | 3 +- .../ForceAggregatorComponent.cs | 3 +- .../SimulationComponents/MoveComponent.cs | 33 +-- .../NewtonGravitatorComponent.cs | 3 +- .../PowerAggregatorComponent.cs | 3 +- .../WattMoverComponent.cs | 7 +- .../OctoAwesome.Basics/WauziPopulator.cs | 5 + .../OctoAwesome.Basics/packages.config | 1 + .../Components/CameraComponent.cs | 32 +-- .../Components/EntityComponent.cs | 69 ++--- .../Components/PlayerComponent.cs | 252 ++++++++--------- .../Controls/CompassControl.cs | 2 +- .../Controls/DebugControl.cs | 17 +- .../Controls/InventoryControl.cs | 57 ++-- .../Controls/SceneControl.cs | 23 +- .../Controls/ToolbarControl.cs | 84 +++--- .../OctoAwesome.Client/Screens/GameScreen.cs | 63 +++-- .../Screens/InventoryScreen.cs | 129 +++++---- .../OctoAwesome.Network.csproj | 19 -- OctoAwesome/OctoAwesome.Network/Settings.cs | 6 +- .../OctoAwesome.Network/packages.config | 1 - .../OctoAwesome.Runtime/ExtensionLoader.cs | 3 +- .../OctoAwesome.Runtime/ResourceManager.cs | 3 +- OctoAwesome/OctoAwesome.Tests/TestPlanet.cs | 3 + OctoAwesome/OctoAwesome/Chunk.cs | 37 ++- OctoAwesome/OctoAwesome/ChunkColumn.cs | 3 +- .../CodeExtensions/Vector2Extension.cs | 26 ++ .../CodeExtensions/Vector3Extension.cs | 22 ++ OctoAwesome/OctoAwesome/Component.cs | 12 +- OctoAwesome/OctoAwesome/ComponentList.cs | 155 ++++++---- OctoAwesome/OctoAwesome/Entities/Entity.cs | 153 ++++++++++ .../OctoAwesome/Entities/EntityComponent.cs | 30 ++ .../{ => Entities}/EntityFilterAttribute.cs | 5 +- .../OctoAwesome/Entities/EntityList.cs | 103 +++++++ OctoAwesome/OctoAwesome/Entities/IDrawable.cs | 84 ++++++ .../OctoAwesome/Entities/IEntityDefinition.cs | 35 +++ .../OctoAwesome/Entities/IEntityList.cs | 46 +++ OctoAwesome/OctoAwesome/Entity.cs | 92 ------ OctoAwesome/OctoAwesome/EntityComponent.cs | 29 -- OctoAwesome/OctoAwesome/EntityList.cs | 71 ----- OctoAwesome/OctoAwesome/Extension.cs | 30 -- OctoAwesome/OctoAwesome/GlobalChunkCache.cs | 3 +- OctoAwesome/OctoAwesome/IChunk.cs | 18 ++ OctoAwesome/OctoAwesome/IChunkColumn.cs | 12 +- OctoAwesome/OctoAwesome/IEntityList.cs | 24 -- OctoAwesome/OctoAwesome/IExtensionLoader.cs | 3 +- OctoAwesome/OctoAwesome/IExtensionResolver.cs | 3 +- OctoAwesome/OctoAwesome/ILocalChunkCache.cs | 26 +- OctoAwesome/OctoAwesome/IMapPopulator.cs | 1 + OctoAwesome/OctoAwesome/IPlanet.cs | 5 + OctoAwesome/OctoAwesome/IResourceManager.cs | 1 + OctoAwesome/OctoAwesome/ISerializable.cs | 22 ++ OctoAwesome/OctoAwesome/LocalChunkCache.cs | 51 ++-- OctoAwesome/OctoAwesome/MapPopulator.cs | 4 + OctoAwesome/OctoAwesome/OctoAwesome.csproj | 48 ++-- OctoAwesome/OctoAwesome/Player.cs | 25 +- OctoAwesome/OctoAwesome/Simulation.cs | 41 ++- .../OctoAwesome/SimulationComponent.cs | 18 +- OctoAwesome/OctoAwesome/UpdateableEntity.cs | 29 -- OctoAwesome/OctoAwesome/packages.config | 4 + 82 files changed, 1792 insertions(+), 968 deletions(-) rename OctoAwesome/{OctoAwesome => OctoAwesome.Basics}/EntityComponents/BodyComponent.cs (90%) rename OctoAwesome/{OctoAwesome => OctoAwesome.Basics}/EntityComponents/ControllableComponent.cs (80%) rename OctoAwesome/{OctoAwesome => OctoAwesome.Basics}/EntityComponents/HeadComponent.cs (91%) rename OctoAwesome/{OctoAwesome => OctoAwesome.Basics}/EntityComponents/InventoryComponent.cs (98%) rename OctoAwesome/{OctoAwesome => OctoAwesome.Basics}/EntityComponents/PositionComponent.cs (84%) rename OctoAwesome/{OctoAwesome => OctoAwesome.Basics}/EntityComponents/RenderComponent.cs (71%) rename OctoAwesome/{OctoAwesome => OctoAwesome.Basics}/EntityComponents/ToolBarComponent.cs (93%) create mode 100644 OctoAwesome/OctoAwesome.Basics/SimComponents/PhysicEngineComponent.cs create mode 100644 OctoAwesome/OctoAwesome/CodeExtensions/Vector2Extension.cs create mode 100644 OctoAwesome/OctoAwesome/CodeExtensions/Vector3Extension.cs create mode 100644 OctoAwesome/OctoAwesome/Entities/Entity.cs create mode 100644 OctoAwesome/OctoAwesome/Entities/EntityComponent.cs rename OctoAwesome/OctoAwesome/{ => Entities}/EntityFilterAttribute.cs (85%) create mode 100644 OctoAwesome/OctoAwesome/Entities/EntityList.cs create mode 100644 OctoAwesome/OctoAwesome/Entities/IDrawable.cs create mode 100644 OctoAwesome/OctoAwesome/Entities/IEntityDefinition.cs create mode 100644 OctoAwesome/OctoAwesome/Entities/IEntityList.cs delete mode 100644 OctoAwesome/OctoAwesome/Entity.cs delete mode 100644 OctoAwesome/OctoAwesome/EntityComponent.cs delete mode 100644 OctoAwesome/OctoAwesome/EntityList.cs delete mode 100644 OctoAwesome/OctoAwesome/Extension.cs delete mode 100644 OctoAwesome/OctoAwesome/IEntityList.cs create mode 100644 OctoAwesome/OctoAwesome/ISerializable.cs delete mode 100644 OctoAwesome/OctoAwesome/UpdateableEntity.cs create mode 100644 OctoAwesome/OctoAwesome/packages.config diff --git a/OctoAwesome/OctoAwesome.Basics/CollisionPlane.cs b/OctoAwesome/OctoAwesome.Basics/CollisionPlane.cs index a3754201..47317443 100644 --- a/OctoAwesome/OctoAwesome.Basics/CollisionPlane.cs +++ b/OctoAwesome/OctoAwesome.Basics/CollisionPlane.cs @@ -1,5 +1,6 @@ using engenious; using OctoAwesome.Basics.EntityComponents; +using OctoAwesome.Entities; using OctoAwesome.EntityComponents; using System; using System.Collections.Generic; @@ -108,26 +109,31 @@ public static IEnumerable GetBlockCollisionPlanes(Index3 pos, Ve /// /// Gibt alle Flächen eines Spielers zurück /// + /// radius of the + /// height of the + /// velocity of the + /// ot the /// Gibt an ob die geschwindigkeit invertiert werden soll - /// Alle beteiligten Flächen des Spielers - public static IEnumerable GetPlayerCollisionPlanes(BodyComponent bodycomp, MoveableComponent movecomp,PositionComponent poscomp,bool invertvelocity = true) + /// + public static IEnumerable GetPlayerCollisionPlanes(float radius, float height, Vector3 velocity, + Coordinate coordinate, bool invertvelocity = true) { - var pos = poscomp.Position.BlockPosition; - var vel = invertvelocity ? -1 * movecomp.Velocity : movecomp.Velocity; + var pos = coordinate.BlockPosition; + var vel = invertvelocity ? -1 * velocity : velocity; //Ebene X if (vel.X > 0) { yield return new CollisionPlane( - new Vector3(pos.X - bodycomp.Radius, pos.Y - bodycomp.Radius, pos.Z), - new Vector3(pos.X - bodycomp.Radius, pos.Y + bodycomp.Radius, pos.Z + bodycomp.Height), + new Vector3(pos.X - radius, pos.Y - radius, pos.Z), + new Vector3(pos.X - radius, pos.Y + radius, pos.Z + height), new Vector3(-1, 0, 0)); } else if (vel.X < 0) { yield return new CollisionPlane( - new Vector3(pos.X + bodycomp.Radius, pos.Y - bodycomp.Radius, pos.Z), - new Vector3(pos.X + bodycomp.Radius, pos.Y + bodycomp.Radius, pos.Z + bodycomp.Height), + new Vector3(pos.X + radius, pos.Y - radius, pos.Z), + new Vector3(pos.X + radius, pos.Y + radius, pos.Z + height), new Vector3(1, 0, 0)); } @@ -135,15 +141,15 @@ public static IEnumerable GetPlayerCollisionPlanes(BodyComponent if (vel.Y > 0) { yield return new CollisionPlane( - new Vector3(pos.X - bodycomp.Radius, pos.Y - bodycomp.Radius, pos.Z ), - new Vector3(pos.X + bodycomp.Radius, pos.Y - bodycomp.Radius, pos.Z + bodycomp.Height), + new Vector3(pos.X - radius, pos.Y - radius, pos.Z ), + new Vector3(pos.X + radius, pos.Y - radius, pos.Z + height), new Vector3(0, -1, 0)); } else if (vel.Y < 0) { yield return new CollisionPlane( - new Vector3(pos.X - bodycomp.Radius, pos.Y + bodycomp.Radius, pos.Z ), - new Vector3(pos.X + bodycomp.Radius, pos.Y + bodycomp.Radius, pos.Z + bodycomp.Height), + new Vector3(pos.X - radius, pos.Y + radius, pos.Z ), + new Vector3(pos.X + radius, pos.Y + radius, pos.Z + height), new Vector3(0, 1, 0)); } @@ -151,15 +157,15 @@ public static IEnumerable GetPlayerCollisionPlanes(BodyComponent if (vel.Z > 0) { yield return new CollisionPlane( - new Vector3(pos.X - bodycomp.Radius, pos.Y - bodycomp.Radius, pos.Z), - new Vector3(pos.X + bodycomp.Radius, pos.Y + bodycomp.Radius, pos.Z), + new Vector3(pos.X - radius, pos.Y - radius, pos.Z), + new Vector3(pos.X + radius, pos.Y + radius, pos.Z), new Vector3(0, 0, -1)); } else if (vel.Z < 0) { yield return new CollisionPlane( - new Vector3(pos.X - bodycomp.Radius, pos.Y - bodycomp.Radius , pos.Z + bodycomp.Height), - new Vector3(pos.X + bodycomp.Radius, pos.Y + bodycomp.Radius, pos.Z + bodycomp.Height), + new Vector3(pos.X - radius, pos.Y - radius , pos.Z + height), + new Vector3(pos.X + radius, pos.Y + radius, pos.Z + height), new Vector3(0, 0, 1)); } } diff --git a/OctoAwesome/OctoAwesome.Basics/Definitions/Blocks/BirchWoodBlockDefinition.cs b/OctoAwesome/OctoAwesome.Basics/Definitions/Blocks/BirchWoodBlockDefinition.cs index 71450244..57b897e9 100644 --- a/OctoAwesome/OctoAwesome.Basics/Definitions/Blocks/BirchWoodBlockDefinition.cs +++ b/OctoAwesome/OctoAwesome.Basics/Definitions/Blocks/BirchWoodBlockDefinition.cs @@ -27,14 +27,7 @@ public override PhysicalProperties GetProperties(ILocalChunkCache manager, int x Granularity = 0.9f, Hardness = 0.1f }; - - public override void Hit(IBlockDefinition block, PhysicalProperties itemProperties) - { - throw new NotImplementedException(); - } - - public override int GetTextureIndex(Wall wall, ILocalChunkCache manager, - int x, int y, int z) + public override int GetTextureIndex(Wall wall, ILocalChunkCache manager, int x, int y, int z) { OrientationFlags orientation = (OrientationFlags)manager.GetBlockMeta(x, y, z); @@ -98,10 +91,9 @@ public override int GetTextureIndex(Wall wall, ILocalChunkCache manager, // Assert here return -1; } - public override int GetTextureRotation(Wall wall, ILocalChunkCache manager, int x, int y, int z) { - OrientationFlags orientation = (OrientationFlags)manager.GetBlockMeta(x, y, z); + OrientationFlags orientation = (OrientationFlags) manager.GetBlockMeta(x, y, z); switch (wall) { case Wall.Top: @@ -138,5 +130,9 @@ public override int GetTextureRotation(Wall wall, ILocalChunkCache manager, int return base.GetTextureRotation(wall, manager, x, y, z); //should never ever happen } } + public override void Hit(IBlockDefinition block, PhysicalProperties itemProperties) + { + throw new NotImplementedException(); + } } } diff --git a/OctoAwesome/OctoAwesome.Basics/Entities/WauziEntity.cs b/OctoAwesome/OctoAwesome.Basics/Entities/WauziEntity.cs index 818f58ad..68ef7b8d 100644 --- a/OctoAwesome/OctoAwesome.Basics/Entities/WauziEntity.cs +++ b/OctoAwesome/OctoAwesome.Basics/Entities/WauziEntity.cs @@ -5,55 +5,86 @@ using System.Threading.Tasks; using engenious; using OctoAwesome.Basics.EntityComponents; +using OctoAwesome.Entities; using OctoAwesome.EntityComponents; namespace OctoAwesome.Basics.Entities { - public class WauziEntity : UpdateableEntity + public class WauziEntity : Entity, IControllable, OctoAwesome.Entities.IDrawable { - public int JumpTime { get; set; } + class WauziController : IController + { + public float HeadTilt { get; set; } + public float HeadYaw { get; set; } + public Vector2 MoveValue { get; set; } + public Vector2 HeadValue { get; set; } + public Index3? InteractBlock { get; set; } + public Index3? ApplyBlock { get; set; } + public OrientationFlags? ApplySide { get; set; } + public InputTrigger JumpInput { get; } + public InputTrigger ApplyInput { get; } + public InputTrigger InteractInput { get; } + public WauziController() + { + JumpInput = new InputTrigger(); + ApplyInput = new InputTrigger(); + InteractInput = new InputTrigger(); + } - public WauziEntity() : base() + } + public int JumpTime { get; set; } + public IController Controller => currentcontroller; + public string Name => "Wauzi"; + public string ModelName => "dog"; + public string TextureName => "texdog"; + public float BaseRotationZ => -90f; + public Vector3 Body => new Vector3(1, 1, 1); + private IController currentcontroller; + public WauziEntity() : base(true) { + Register( new WauziController()); + SetPosition(new Coordinate(0, new Index3(0, 0, 200), new Vector3(0, 0, 0)), 0, true); } - protected override void OnInitialize(IResourceManager manager) { Cache = new LocalChunkCache(manager.GlobalChunkCache, true, 2, 1); } - public override void Update(GameTime gameTime) - { - BodyPowerComponent body = Components.GetComponent(); - ControllableComponent controller = Components.GetComponent(); - controller.MoveInput = new Vector2(0.5f, 0.5f) ; - - if (JumpTime <= 0) + { + if (currentcontroller != null && JumpTime <= 0) { - controller.JumpInput = true; + currentcontroller.JumpInput.Set(true); JumpTime = 10000; } else { JumpTime -= gameTime.ElapsedGameTime.Milliseconds; } - - if (controller.JumpActive) - { - controller.JumpInput = false; - } } + public void Register(IController controller) + { + this.currentcontroller = controller; + } + public void Reset() + { - public override void RegisterDefault() + } + public void Initialize(IGraphicsDevice device) + { + } + public void Draw(IGraphicsDevice graphicsDevice, GameTime gameTime) { - Components.AddComponent(new PositionComponent() { Position = new Coordinate(0, new Index3(0, 0, 200), new Vector3(0, 0, 0)) }); - Components.AddComponent(new GravityComponent()); - Components.AddComponent(new BodyComponent() { Mass = 50f, Height = 2f, Radius = 1.5f }); - Components.AddComponent(new BodyPowerComponent() { Power = 600f, JumpTime = 120 }); - Components.AddComponent(new MoveableComponent()); - Components.AddComponent(new BoxCollisionComponent()); - Components.AddComponent(new ControllableComponent()); - Components.AddComponent(new RenderComponent() { Name = "Wauzi", ModelName = "dog", TextureName = "texdog", BaseZRotation = -90 }, true); } + //public override void RegisterDefault() + //{ + // Components.AddComponent(new PositionComponent() { Position = new Coordinate(0, new Index3(0, 0, 200), new Vector3(0, 0, 0)) }); + // Components.AddComponent(new GravityComponent()); + // Components.AddComponent(new BodyComponent() { Mass = 50f, Height = 2f, Radius = 1.5f }); + // Components.AddComponent(new BodyPowerComponent() { Power = 600f, JumpTime = 120 }); + // Components.AddComponent(new MoveableComponent()); + // Components.AddComponent(new BoxCollisionComponent()); + // Components.AddComponent(new ControllableComponent()); + // Components.AddComponent(new RenderComponent() { Name = "Wauzi", ModelName = "dog", TextureName = "texdog", BaseZRotation = -90 }, true); + //} } } diff --git a/OctoAwesome/OctoAwesome/EntityComponents/BodyComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/BodyComponent.cs similarity index 90% rename from OctoAwesome/OctoAwesome/EntityComponents/BodyComponent.cs rename to OctoAwesome/OctoAwesome.Basics/EntityComponents/BodyComponent.cs index dc939209..49e7f119 100644 --- a/OctoAwesome/OctoAwesome/EntityComponents/BodyComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/BodyComponent.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; +using OctoAwesome.Entities; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace OctoAwesome.EntityComponents { diff --git a/OctoAwesome/OctoAwesome.Basics/EntityComponents/BodyPowerComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/BodyPowerComponent.cs index 58d8d90e..fb033417 100644 --- a/OctoAwesome/OctoAwesome.Basics/EntityComponents/BodyPowerComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/BodyPowerComponent.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.IO; namespace OctoAwesome.Basics.EntityComponents { diff --git a/OctoAwesome/OctoAwesome.Basics/EntityComponents/BoxCollisionComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/BoxCollisionComponent.cs index 992b0274..5502f325 100644 --- a/OctoAwesome/OctoAwesome.Basics/EntityComponents/BoxCollisionComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/BoxCollisionComponent.cs @@ -1,4 +1,7 @@ -using System; +using engenious; +using OctoAwesome.Entities; +using OctoAwesome.EntityComponents; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,5 +11,109 @@ namespace OctoAwesome.Basics.EntityComponents { public sealed class BoxCollisionComponent : CollisionComponent { + + private void CheckBoxCollision(GameTime gameTime, Entity e, MoveableComponent movecomp, PositionComponent poscomp) + { + BodyComponent bc = new BodyComponent(); + if (e.Components.ContainsComponent()) + bc = e.Components.GetComponent(); + + + Coordinate position = poscomp.Position; + + Vector3 move = movecomp.PositionMove; + + //Blocks finden die eine Kollision verursachen könnten + int minx = (int) Math.Floor(Math.Min( + position.BlockPosition.X - bc.Radius, + position.BlockPosition.X - bc.Radius + movecomp.PositionMove.X)); + int maxx = (int) Math.Ceiling(Math.Max( + position.BlockPosition.X + bc.Radius, + position.BlockPosition.X + bc.Radius + movecomp.PositionMove.X)); + int miny = (int) Math.Floor(Math.Min( + position.BlockPosition.Y - bc.Radius, + position.BlockPosition.Y - bc.Radius + movecomp.PositionMove.Y)); + int maxy = (int) Math.Ceiling(Math.Max( + position.BlockPosition.Y + bc.Radius, + position.BlockPosition.Y + bc.Radius + movecomp.PositionMove.Y)); + int minz = (int) Math.Floor(Math.Min( + position.BlockPosition.Z, + position.BlockPosition.Z + movecomp.PositionMove.Z)); + int maxz = (int) Math.Ceiling(Math.Max( + position.BlockPosition.Z + bc.Height, + position.BlockPosition.Z + bc.Height + movecomp.PositionMove.Z)); + + //Beteiligte Flächen des Spielers + var playerplanes = CollisionPlane.GetPlayerCollisionPlanes(bc.Radius, bc.Height, movecomp.Velocity, poscomp.Position); + + bool abort = false; + + for (int z = minz; z <= maxz && !abort; z++) + { + for (int y = miny; y <= maxy && !abort; y++) + { + for (int x = minx; x <= maxx && !abort; x++) + { + move = movecomp.Velocity * (float) gameTime.ElapsedGameTime.TotalSeconds; + + Index3 pos = new Index3(x, y, z); + Index3 blockPos = pos + position.GlobalBlockIndex; + ushort block = e.Cache.GetBlock(blockPos); + if (block == 0) + continue; + + + + var blockplane = CollisionPlane.GetBlockCollisionPlanes(pos, movecomp.Velocity); + + var planes = from pp in playerplanes + from bp in blockplane + where CollisionPlane.Intersect(bp, pp) + let distance = CollisionPlane.GetDistance(bp, pp) + where CollisionPlane.CheckDistance(distance, move) + select new { BlockPlane = bp, PlayerPlane = pp, Distance = distance }; + + foreach (var plane in planes) + { + + var subvelocity = (plane.Distance / (float) gameTime.ElapsedGameTime.TotalSeconds); + var diff = movecomp.Velocity - subvelocity; + + float vx; + float vy; + float vz; + + if (plane.BlockPlane.normal.X != 0 && (movecomp.Velocity.X > 0 && diff.X >= 0 && subvelocity.X >= 0 || movecomp.Velocity.X < 0 && diff.X <= 0 && subvelocity.X <= 0)) + vx = subvelocity.X; + else + vx = movecomp.Velocity.X; + + if (plane.BlockPlane.normal.Y != 0 && (movecomp.Velocity.Y > 0 && diff.Y >= 0 && subvelocity.Y >= 0 || movecomp.Velocity.Y < 0 && diff.Y <= 0 && subvelocity.Y <= 0)) + vy = subvelocity.Y; + else + vy = movecomp.Velocity.Y; + + if (plane.BlockPlane.normal.Z != 0 && (movecomp.Velocity.Z > 0 && diff.Z >= 0 && subvelocity.Z >= 0 || movecomp.Velocity.Z < 0 && diff.Z <= 0 && subvelocity.Z <= 0)) + vz = subvelocity.Z; + else + vz = movecomp.Velocity.Z; + + movecomp.Velocity = new Vector3(vx, vy, vz); + + if (vx == 0 && vy == 0 && vz == 0) + { + abort = true; + break; + } + } + } + } + } + + // TODO: Was ist für den Fall Gravitation = 0 oder im Scheitelpunkt des Sprungs? + //movecomp.OnGround = Player.Velocity.Z == 0f; + + movecomp.PositionMove = movecomp.Velocity * (float) gameTime.ElapsedGameTime.TotalSeconds; + } } } diff --git a/OctoAwesome/OctoAwesome.Basics/EntityComponents/CollisionComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/CollisionComponent.cs index 2c3c9aef..07ba04da 100644 --- a/OctoAwesome/OctoAwesome.Basics/EntityComponents/CollisionComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/CollisionComponent.cs @@ -1,4 +1,5 @@ -using System; +using OctoAwesome.Entities; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/OctoAwesome/OctoAwesome/EntityComponents/ControllableComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/ControllableComponent.cs similarity index 80% rename from OctoAwesome/OctoAwesome/EntityComponents/ControllableComponent.cs rename to OctoAwesome/OctoAwesome.Basics/EntityComponents/ControllableComponent.cs index e47c47d1..ed9e868c 100644 --- a/OctoAwesome/OctoAwesome/EntityComponents/ControllableComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/ControllableComponent.cs @@ -1,20 +1,16 @@ using engenious; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using OctoAwesome.Entities; namespace OctoAwesome.EntityComponents { public class ControllableComponent : EntityComponent { - public bool JumpInput { get; set; } - public Vector2 MoveInput { get; set; } public bool JumpActive { get; set; } public int JumpTime { get; set; } + public Vector2 MoveInput { get; set; } + public bool JumpInput { get; set; } public Index3? InteractBlock { get; set; } public Index3? ApplyBlock { get; set; } public OrientationFlags ApplySide { get; set; } diff --git a/OctoAwesome/OctoAwesome.Basics/EntityComponents/ForceComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/ForceComponent.cs index a692aa0b..880e678f 100644 --- a/OctoAwesome/OctoAwesome.Basics/EntityComponents/ForceComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/ForceComponent.cs @@ -1,4 +1,5 @@ using engenious; +using OctoAwesome.Entities; using System; using System.Collections.Generic; using System.Linq; diff --git a/OctoAwesome/OctoAwesome/EntityComponents/HeadComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/HeadComponent.cs similarity index 91% rename from OctoAwesome/OctoAwesome/EntityComponents/HeadComponent.cs rename to OctoAwesome/OctoAwesome.Basics/EntityComponents/HeadComponent.cs index 55cd73dd..4841b199 100644 --- a/OctoAwesome/OctoAwesome/EntityComponents/HeadComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/HeadComponent.cs @@ -1,10 +1,6 @@ using engenious; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.IO; +using OctoAwesome.Entities; namespace OctoAwesome.EntityComponents { diff --git a/OctoAwesome/OctoAwesome/EntityComponents/InventoryComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/InventoryComponent.cs similarity index 98% rename from OctoAwesome/OctoAwesome/EntityComponents/InventoryComponent.cs rename to OctoAwesome/OctoAwesome.Basics/EntityComponents/InventoryComponent.cs index 58e03739..c4aee3c3 100644 --- a/OctoAwesome/OctoAwesome/EntityComponents/InventoryComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/InventoryComponent.cs @@ -1,9 +1,7 @@ -using System; +using OctoAwesome.Entities; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace OctoAwesome.EntityComponents { diff --git a/OctoAwesome/OctoAwesome.Basics/EntityComponents/MassComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/MassComponent.cs index beca668c..468f450f 100644 --- a/OctoAwesome/OctoAwesome.Basics/EntityComponents/MassComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/MassComponent.cs @@ -1,4 +1,5 @@ -using System; +using OctoAwesome.Entities; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/OctoAwesome/OctoAwesome.Basics/EntityComponents/MoveableComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/MoveableComponent.cs index 62f74f56..db8b0d7e 100644 --- a/OctoAwesome/OctoAwesome.Basics/EntityComponents/MoveableComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/MoveableComponent.cs @@ -1,9 +1,5 @@ using engenious; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.EntityComponents { diff --git a/OctoAwesome/OctoAwesome/EntityComponents/PositionComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/PositionComponent.cs similarity index 84% rename from OctoAwesome/OctoAwesome/EntityComponents/PositionComponent.cs rename to OctoAwesome/OctoAwesome.Basics/EntityComponents/PositionComponent.cs index 3984e4ca..20b123b1 100644 --- a/OctoAwesome/OctoAwesome/EntityComponents/PositionComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/PositionComponent.cs @@ -1,10 +1,6 @@ using engenious; -using System; -using System.Collections.Generic; +using OctoAwesome.Entities; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace OctoAwesome.EntityComponents { @@ -43,7 +39,9 @@ public override void Deserialize(BinaryReader reader, IDefinitionManager definit float posY = reader.ReadSingle(); float posZ = reader.ReadSingle(); - Position = new Coordinate(planet, new Index3(blockX, blockY, blockZ), new Vector3(posX, posY, posZ)); + Position = new Coordinate(reader.ReadInt32(), + new Index3(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32()), + new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle())); } } } diff --git a/OctoAwesome/OctoAwesome.Basics/EntityComponents/PowerComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/PowerComponent.cs index 066ec00a..24b90886 100644 --- a/OctoAwesome/OctoAwesome.Basics/EntityComponents/PowerComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/PowerComponent.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using System.IO; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.EntityComponents { diff --git a/OctoAwesome/OctoAwesome/EntityComponents/RenderComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/RenderComponent.cs similarity index 71% rename from OctoAwesome/OctoAwesome/EntityComponents/RenderComponent.cs rename to OctoAwesome/OctoAwesome.Basics/EntityComponents/RenderComponent.cs index c39efecd..3d979dc9 100644 --- a/OctoAwesome/OctoAwesome/EntityComponents/RenderComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/RenderComponent.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using OctoAwesome.Entities; namespace OctoAwesome.EntityComponents { diff --git a/OctoAwesome/OctoAwesome/EntityComponents/ToolBarComponent.cs b/OctoAwesome/OctoAwesome.Basics/EntityComponents/ToolBarComponent.cs similarity index 93% rename from OctoAwesome/OctoAwesome/EntityComponents/ToolBarComponent.cs rename to OctoAwesome/OctoAwesome.Basics/EntityComponents/ToolBarComponent.cs index fd5a1c6a..fce07ff1 100644 --- a/OctoAwesome/OctoAwesome/EntityComponents/ToolBarComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/EntityComponents/ToolBarComponent.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using OctoAwesome.Entities; namespace OctoAwesome.EntityComponents { @@ -13,7 +8,7 @@ namespace OctoAwesome.EntityComponents public class ToolBarComponent : EntityComponent { /// - /// Gibt die Anzahl Tools in der Toolbar an. + /// Gibt die Anzahl der Tools in der Toolbar an. /// public const int TOOLCOUNT = 10; diff --git a/OctoAwesome/OctoAwesome.Basics/Extension.cs b/OctoAwesome/OctoAwesome.Basics/Extension.cs index 56174caa..062a6fb7 100644 --- a/OctoAwesome/OctoAwesome.Basics/Extension.cs +++ b/OctoAwesome/OctoAwesome.Basics/Extension.cs @@ -18,30 +18,31 @@ public sealed class Extension : IExtension public void Register(IExtensionLoader extensionLoader) { - foreach (var t in Assembly.GetExecutingAssembly().GetTypes().Where( t => !t.IsAbstract && typeof(IDefinition).IsAssignableFrom(t))) { - extensionLoader.RegisterDefinition((IDefinition)Activator.CreateInstance(t)); + extensionLoader.RegisterDefinition((IDefinition) Activator.CreateInstance(t)); } - extensionLoader.RegisterMapGenerator(new ComplexPlanetGenerator()); - extensionLoader.RegisterMapPopulator(new TreePopulator()); extensionLoader.RegisterMapPopulator(new WauziPopulator()); - extensionLoader.RegisterEntity(); extensionLoader.RegisterDefaultEntityExtender(); extensionLoader.RegisterEntityExtender((p) => { - p.Components.AddComponent(new GravityComponent()); - p.Components.AddComponent(new PositionComponent() { Position = new Coordinate(0, new Index3(0, 0, 200), new Vector3(0, 0, 0)) }); - p.Components.AddComponent(new BodyComponent() { Mass = 50f, Height = 3.5f, Radius = 0.75f }); - p.Components.AddComponent(new BodyPowerComponent() { Power = 600f, JumpTime = 120 }); - p.Components.AddComponent(new MoveableComponent()); - p.Components.AddComponent(new BoxCollisionComponent()); - p.Components.AddComponent(new EntityCollisionComponent()); + p.SetPosition(new Coordinate(0, new Index3(0, 0, 200), new Vector3(0, 0, 0)), 0, true); + //p.Components.AddComponent(new ControllableComponent()); + //p.Components.AddComponent(new HeadComponent() { Offset = new Vector3(0, 0, 3.2f) }); + //p.Components.AddComponent(new InventoryComponent()); // -> sinvoll + //p.Components.AddComponent(new ToolBarComponent()); // -> sinvoll + //p.Components.AddComponent(new GravityComponent()); + //p.Components.AddComponent(new PositionComponent() { Position = new Coordinate(0, new Index3(0, 0, 200), new Vector3(0, 0, 0)) }); + //p.Components.AddComponent(new BodyComponent() { Mass = 50f, Height = 3.5f, Radius = 0.75f }); + //p.Components.AddComponent(new BodyPowerComponent() { Power = 600f, JumpTime = 120 }); + //p.Components.AddComponent(new MoveableComponent()); + //p.Components.AddComponent(new BoxCollisionComponent()); + //p.Components.AddComponent(new EntityCollisionComponent()); }); extensionLoader.RegisterSimulationExtender((s) => diff --git a/OctoAwesome/OctoAwesome.Basics/OctoAwesome.Basics.csproj b/OctoAwesome/OctoAwesome.Basics/OctoAwesome.Basics.csproj index 943f3bac..e50e2931 100644 --- a/OctoAwesome/OctoAwesome.Basics/OctoAwesome.Basics.csproj +++ b/OctoAwesome/OctoAwesome.Basics/OctoAwesome.Basics.csproj @@ -59,6 +59,9 @@ ..\packages\engenious.0.1.11\lib\net40\System.Numerics.Vectors.dll + + ..\packages\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll + @@ -70,6 +73,13 @@ + + + + + + + @@ -135,6 +145,7 @@ + diff --git a/OctoAwesome/OctoAwesome.Basics/SimComponents/PhysicEngineComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimComponents/PhysicEngineComponent.cs new file mode 100644 index 00000000..d632912c --- /dev/null +++ b/OctoAwesome/OctoAwesome.Basics/SimComponents/PhysicEngineComponent.cs @@ -0,0 +1,267 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using engenious; +using engenious.Helper; +using OctoAwesome.CodeExtensions; +using OctoAwesome.Entities; +namespace OctoAwesome.Basics.SimComponents +{ + class StatespaceComponent : EntityComponent + { + //TODO: auslagern + public int DefaultJumpTime { get; internal set; } + public float DefaultMass { get; internal set; } + public float DefaultPower { get; internal set; } + public float DefaultRadius { get; internal set; } + public float DefaultHeight { get; internal set; } + + public float Azimuth { get; internal set; } + public int JumpTime { get; internal set; } + public bool JumpActive { get; internal set; } + public Entity Entity { get; } + public Vector3 ExternalForce { get; internal set; } + public Vector3 ExternalPower { get; internal set; } + public Vector3 Velocity { get; internal set; } + public Vector3 PositionDelta { get; internal set; } + public List Forces { get; internal set; } + public List<(Vector3 direction, float power)> Powers { get; internal set; } + + public StatespaceComponent() + { + Forces = new List(2); + Powers = new List<(Vector3 direction, float power)>(); + // TODO: auslagern... + DefaultMass = 1; + DefaultRadius = 1; + DefaultHeight = 1; + DefaultJumpTime = 120; + } + } + class PhysicEngineComponent : SimulationComponent + { + private Type statespacekey; + private List controllableentities; + public PhysicEngineComponent() + { + statespacekey = typeof(StatespaceComponent); + controllableentities = new List(); + } + public override void Register(Entity entity) + { + if (entity is IControllable controllable) + { + if(!entity.Components.ContainsComponent(statespacekey)) + { + entity.Components.AddComponent(statespacekey, new StatespaceComponent(), false); + controllableentities.Add(controllable); + } + } + } + public override void Update(GameTime gameTime) + { + //CalcEntityCollisons(gameTime, con, statespace); + foreach (IControllable con in controllableentities) + { + if (con.Controller == null) continue; + Entity entity = con as Entity; + if (entity.Components.TryGetComponent(statespacekey, out StatespaceComponent statespace)) + { + CalcWattMovement(gameTime, con, statespace); + CalcNewtonGravity(gameTime, entity, statespace); + CalcAggregator(gameTime, con, statespace); + CalcAcceleration(gameTime, con, statespace); + CalcWorldCollision(gameTime, entity, con, statespace); + entity.Move(statespace.PositionDelta, statespace.Azimuth); + } + } + } + private void CalcWattMovement(GameTime gameTime, IControllable con, StatespaceComponent statespace) + { + Vector3 direction = Vector3.Zero; + if (!con.Controller.MoveValue.IsZero()) + { + float lookX = (float) Math.Cos(con.Controller.HeadYaw); + float lookY = -(float) Math.Sin(con.Controller.HeadYaw); + direction = new Vector3(lookX, lookY, 0) * con.Controller.MoveValue.Y; + + float stafeX = (float) Math.Cos(con.Controller.HeadYaw + MathHelper.PiOver2); + float stafeY = -(float) Math.Sin(con.Controller.HeadYaw + MathHelper.PiOver2); + direction += new Vector3(stafeX, stafeY, 0) * con.Controller.MoveValue.X; + } + else + { + statespace.Powers.Add((con.Controller.MoveValue.ToVector3(), 600f)); + } + + if (con.Controller.JumpInput.Value && !statespace.JumpActive) + { + con.Controller.JumpInput.Validate(); + statespace.JumpTime = statespace.DefaultJumpTime; + statespace.JumpActive = true; + } + if (statespace.JumpActive) + { + direction += new Vector3(0, 0, 1); + statespace.JumpTime -= gameTime.ElapsedGameTime.Milliseconds; + + if (statespace.JumpTime <= 0) + statespace.JumpActive = false; + } + statespace.Powers.Add((direction, statespace.DefaultPower)); + } + private void CalcNewtonGravity(GameTime gameTime, Entity entity, StatespaceComponent statespace) + { + // TODO: check if valid ids > 0 + //if (entity.Position.Planet == 0) return; + float gravity = entity.Cache.GetPlanet(entity.Position.Planet).Gravity; + statespace.Forces.Add(new Vector3(0, 0, -statespace.DefaultMass * gravity)); + } + private void CalcAggregator(GameTime gameTime, IControllable con, StatespaceComponent statespace) + { + statespace.ExternalForce = statespace.Forces.Aggregate(Vector3.Zero, (seed, force) => seed + force); + statespace.ExternalPower = statespace.Powers.Aggregate(Vector3.Zero, (seed, pow) => seed + pow.direction * pow.power); + } + private void CalcAcceleration(GameTime gameTime, IControllable con, StatespaceComponent statespace) + { + // TODO: haut das hin + // was ist Kraft mal kraft xD + // Convert external Forces to Powers + Vector3 power = ((statespace.ExternalForce * statespace.ExternalForce) / (2 * statespace.DefaultMass)) * + (float) gameTime.ElapsedGameTime.TotalSeconds; + + // Take care of direction + power *= new Vector3( + Math.Sign(statespace.ExternalForce.X), + Math.Sign(statespace.ExternalForce.Y), + Math.Sign(statespace.ExternalForce.Z)); + + // Add external Power + power += statespace.ExternalPower; + + // Friction Power + power -= new Vector3(60F, 60f, 0.1f) * statespace.Velocity; + +#if DEBUG + var a1 = 2.0f / statespace.DefaultMass; + var b = 2 / statespace.DefaultMass; + var a2 = a1 * power; + var c = new Vector3(a1 * power.X, a1 * power.Y, a1 * power.Z); + + var a3 = a2 * (float) gameTime.ElapsedGameTime.TotalSeconds; +#endif + + // Calculate Velocity change + Vector3 velocityChange = ((2.0f / statespace.DefaultMass) * power) * (float) gameTime.ElapsedGameTime.TotalSeconds; + + // Take care of direction + statespace.Velocity += new Vector3( + (float) (velocityChange.X < 0 ? -Math.Sqrt(-velocityChange.X) : Math.Sqrt(velocityChange.X)), + (float) (velocityChange.Y < 0 ? -Math.Sqrt(-velocityChange.Y) : Math.Sqrt(velocityChange.Y)), + (float) (velocityChange.Z < 0 ? -Math.Sqrt(-velocityChange.Z) : Math.Sqrt(velocityChange.Z))); + + // Calculate Move Vector for the upcoming frame + statespace.PositionDelta = statespace.Velocity * (float) gameTime.ElapsedGameTime.TotalSeconds; + } + private void CalcWorldCollision(GameTime gameTime, Entity entity, IControllable con, StatespaceComponent statespace) + { + Coordinate position = con.Position; + Vector3 move = statespace.PositionDelta; + + //Blocks finden die eine Kollision verursachen könnten + int minx = (int) Math.Floor(Math.Min( + position.BlockPosition.X - statespace.DefaultRadius, + position.BlockPosition.X - statespace.DefaultRadius + statespace.PositionDelta.X)); + int maxx = (int) Math.Ceiling(Math.Max( + position.BlockPosition.X + statespace.DefaultRadius, + position.BlockPosition.X + statespace.DefaultRadius + statespace.PositionDelta.X)); + int miny = (int) Math.Floor(Math.Min( + position.BlockPosition.Y - statespace.DefaultRadius, + position.BlockPosition.Y - statespace.DefaultRadius + statespace.PositionDelta.Y)); + int maxy = (int) Math.Ceiling(Math.Max( + position.BlockPosition.Y + statespace.DefaultRadius, + position.BlockPosition.Y + statespace.DefaultRadius + statespace.PositionDelta.Y)); + int minz = (int) Math.Floor(Math.Min( + position.BlockPosition.Z, + position.BlockPosition.Z + statespace.PositionDelta.Z)); + int maxz = (int) Math.Ceiling(Math.Max( + position.BlockPosition.Z + statespace.DefaultHeight, + position.BlockPosition.Z + statespace.DefaultHeight + statespace.PositionDelta.Z)); + + //Beteiligte Flächen des Spielers + var playerplanes = CollisionPlane.GetPlayerCollisionPlanes(statespace.DefaultRadius, statespace.DefaultHeight, + statespace.Velocity, con.Position); + + bool abort = false; + + for (int z = minz; z <= maxz && !abort; z++) + { + for (int y = miny; y <= maxy && !abort; y++) + { + for (int x = minx; x <= maxx && !abort; x++) + { + move = statespace.Velocity * (float) gameTime.ElapsedGameTime.TotalSeconds; + + Index3 pos = new Index3(x, y, z); + Index3 blockPos = pos + position.GlobalBlockIndex; + ushort block = entity.Cache.GetBlock(blockPos); + if (block == 0) continue; + + + + var blockplane = CollisionPlane.GetBlockCollisionPlanes(pos, statespace.Velocity); + + var planes = from pp in playerplanes + from bp in blockplane + where CollisionPlane.Intersect(bp, pp) + let distance = CollisionPlane.GetDistance(bp, pp) + where CollisionPlane.CheckDistance(distance, move) + select new { BlockPlane = bp, PlayerPlane = pp, Distance = distance }; + + foreach (var plane in planes) + { + + var subvelocity = (plane.Distance / (float) gameTime.ElapsedGameTime.TotalSeconds); + var diff = statespace.Velocity - subvelocity; + + float vx; + float vy; + float vz; + + if (plane.BlockPlane.normal.X != 0 && (statespace.Velocity.X > 0 && diff.X >= 0 && subvelocity.X >= 0 || statespace.Velocity.X < 0 && diff.X <= 0 && subvelocity.X <= 0)) + vx = subvelocity.X; + else + vx = statespace.Velocity.X; + + if (plane.BlockPlane.normal.Y != 0 && (statespace.Velocity.Y > 0 && diff.Y >= 0 && subvelocity.Y >= 0 || statespace.Velocity.Y < 0 && diff.Y <= 0 && subvelocity.Y <= 0)) + vy = subvelocity.Y; + else + vy = statespace.Velocity.Y; + + if (plane.BlockPlane.normal.Z != 0 && (statespace.Velocity.Z > 0 && diff.Z >= 0 && subvelocity.Z >= 0 || statespace.Velocity.Z < 0 && diff.Z <= 0 && subvelocity.Z <= 0)) + vz = subvelocity.Z; + else + vz = statespace.Velocity.Z; + + statespace.Velocity = new Vector3(vx, vy, vz); + + if (vx == 0 && vy == 0 && vz == 0) + { + abort = true; + break; + } + } + } + } + } + + // TODO: Was ist für den Fall Gravitation = 0 oder im Scheitelpunkt des Sprungs? + // TODO: statespace.jumpactive überprüfen ? + //movecomp.OnGround = Player.Velocity.Z == 0f; + + if (!statespace.PositionDelta.IsZero()) + statespace.Azimuth = MathHelper.WrapAngle((float) Math.Atan2(statespace.PositionDelta.Y, statespace.PositionDelta.X)); + statespace.PositionDelta = statespace.Velocity * (float) gameTime.ElapsedGameTime.TotalSeconds; + } + } +} diff --git a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/AccelerationComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/AccelerationComponent.cs index 47765256..125099bc 100644 --- a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/AccelerationComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/AccelerationComponent.cs @@ -3,12 +3,13 @@ using System.Linq; using engenious; using OctoAwesome.Basics.EntityComponents; +using OctoAwesome.Entities; using OctoAwesome.EntityComponents; namespace OctoAwesome.Basics.SimulationComponents { [EntityFilter(typeof(MoveableComponent), typeof(BodyComponent))] - public sealed class AccelerationComponent : SimulationComponent + public sealed class AccelerationComponent : OSimulationComponent { private List acceleratedEntities = new List(); @@ -17,20 +18,20 @@ public override void Update(GameTime gameTime) foreach (var entity in acceleratedEntities) { // Convert external Forces to Powers - Vector3 power = ((entity.Move.ExternalForces * entity.Move.ExternalForces) / (2 * entity.Body.Mass)) * - (float)gameTime.ElapsedGameTime.TotalSeconds; + Vector3 power = ((entity.Move.ExternalForces * entity.Move.ExternalForces) / (2 * entity.Body.Mass)) * + (float) gameTime.ElapsedGameTime.TotalSeconds; // Take care of direction power *= new Vector3( - Math.Sign(entity.Move.ExternalForces.X), - Math.Sign(entity.Move.ExternalForces.Y), + Math.Sign(entity.Move.ExternalForces.X), + Math.Sign(entity.Move.ExternalForces.Y), Math.Sign(entity.Move.ExternalForces.Z)); // Add external Power power += entity.Move.ExternalPowers; // Friction Power - power -= new Vector3(60F,60f,0.1f) * entity.Move.Velocity; + power -= new Vector3(60F, 60f, 0.1f) * entity.Move.Velocity; //DEBUGGING var a1 = 2.0f / entity.Body.Mass; @@ -38,23 +39,22 @@ public override void Update(GameTime gameTime) var a2 = a1 * power; var c = new Vector3(a1 * power.X, a1 * power.Y, a1 * power.Z); - var a3 = a2 * (float)gameTime.ElapsedGameTime.TotalSeconds; - + var a3 = a2 * (float) gameTime.ElapsedGameTime.TotalSeconds; + //DEBUGGING // Calculate Velocity change - Vector3 velocityChange = ((2.0f / entity.Body.Mass) * power) * - (float)gameTime.ElapsedGameTime.TotalSeconds; + Vector3 velocityChange = ((2.0f / entity.Body.Mass) * power) * (float) gameTime.ElapsedGameTime.TotalSeconds; // Take care of direction entity.Move.Velocity += new Vector3( - (float)(velocityChange.X < 0 ? -Math.Sqrt(-velocityChange.X) : Math.Sqrt(velocityChange.X)), - (float)(velocityChange.Y < 0 ? -Math.Sqrt(-velocityChange.Y) : Math.Sqrt(velocityChange.Y)), - (float)(velocityChange.Z < 0 ? -Math.Sqrt(-velocityChange.Z) : Math.Sqrt(velocityChange.Z))); + (float) (velocityChange.X < 0 ? -Math.Sqrt(-velocityChange.X) : Math.Sqrt(velocityChange.X)), + (float) (velocityChange.Y < 0 ? -Math.Sqrt(-velocityChange.Y) : Math.Sqrt(velocityChange.Y)), + (float) (velocityChange.Z < 0 ? -Math.Sqrt(-velocityChange.Z) : Math.Sqrt(velocityChange.Z))); // Calculate Move Vector for the upcoming frame - entity.Move.PositionMove = entity.Move.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds; + entity.Move.PositionMove = entity.Move.Velocity * (float) gameTime.ElapsedGameTime.TotalSeconds; } } diff --git a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/BlockInteractionComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/BlockInteractionComponent.cs index 5b4af618..796f2bb9 100644 --- a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/BlockInteractionComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/BlockInteractionComponent.cs @@ -5,11 +5,12 @@ using System.Text; using System.Threading.Tasks; using engenious; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.SimulationComponents { [EntityFilter(typeof(ControllableComponent), typeof(InventoryComponent))] - public class BlockInteractionComponent : SimulationComponent + public class BlockInteractionComponent : OSimulationComponent { private Simulation simulation; @@ -32,25 +33,26 @@ protected override void UpdateEntity(GameTime gameTime, Entity entity, Controlla { var toolbar = entity.Components.GetComponent(); + // take block if (controller.InteractBlock.HasValue) { - ushort lastBlock = entity.Cache.GetBlock(controller.InteractBlock.Value); - entity.Cache.SetBlock(controller.InteractBlock.Value, 0); + ushort lastBlock = entity.Cache.GetBlock(controller.InteractBlock.Value, true); + //entity.Cache.SetBlock(controller.InteractBlock.Value, 0); if (lastBlock != 0) { var blockDefinition = simulation.ResourceManager.DefinitionManager.GetDefinitionByIndex(lastBlock); - if (blockDefinition is IInventoryableDefinition invDef) - inventory.AddUnit(invDef); + if (blockDefinition is IInventoryableDefinition invDef) inventory.AddUnit(invDef); } controller.InteractBlock = null; } + // set or interact with tool if (toolbar != null && controller.ApplyBlock.HasValue) { if (toolbar.ActiveTool != null) { - Index3 add = new Index3(); + Index3 add; switch (controller.ApplySide) { case OrientationFlags.SideWest: add = new Index3(-1, 0, 0); break; @@ -59,12 +61,11 @@ protected override void UpdateEntity(GameTime gameTime, Entity entity, Controlla case OrientationFlags.SideNorth: add = new Index3(0, 1, 0); break; case OrientationFlags.SideBottom: add = new Index3(0, 0, -1); break; case OrientationFlags.SideTop: add = new Index3(0, 0, 1); break; + default: add = new Index3(); break; } - if (toolbar.ActiveTool.Definition is IBlockDefinition) + if (toolbar.ActiveTool.Definition is IBlockDefinition definition) { - IBlockDefinition definition = toolbar.ActiveTool.Definition as IBlockDefinition; - Index3 idx = controller.ApplyBlock.Value + add; var boxes = definition.GetCollisionBoxes(entity.Cache, idx.X, idx.Y, idx.Z); diff --git a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/CollisionComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/CollisionComponent.cs index c93c232a..c223fdbe 100644 --- a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/CollisionComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/CollisionComponent.cs @@ -4,10 +4,11 @@ using System.Text; using System.Threading.Tasks; using engenious; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.SimulationComponents { - public sealed class CollisionComponent : SimulationComponent + public sealed class CollisionComponent : OSimulationComponent { protected override bool AddEntity(Entity entity) { diff --git a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/ForceAggregatorComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/ForceAggregatorComponent.cs index 19d6f1f3..70de3ab7 100644 --- a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/ForceAggregatorComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/ForceAggregatorComponent.cs @@ -2,11 +2,12 @@ using System.Linq; using engenious; using OctoAwesome.Basics.EntityComponents; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.SimulationComponents { [EntityFilter(typeof(ForceComponent), typeof(MoveableComponent))] - public sealed class ForceAggregatorComponent : SimulationComponent + public sealed class ForceAggregatorComponent : OSimulationComponent { private List forcedEntities = new List(); diff --git a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/MoveComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/MoveComponent.cs index 66ddc187..a0e1ca00 100644 --- a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/MoveComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/MoveComponent.cs @@ -4,17 +4,18 @@ using System.Linq; using OctoAwesome.EntityComponents; using engenious.Helper; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.SimulationComponents { [EntityFilter(typeof(MoveableComponent), typeof(PositionComponent))] - public sealed class MoveComponent : SimulationComponent + public sealed class MoveComponent : OSimulationComponent { protected override bool AddEntity(Entity entity) { var poscomp = entity.Components.GetComponent(); - var planet = entity.Cache.LoadPlanet(poscomp.Position.Planet); + var planet = entity.Cache.GetPlanet(poscomp.Position.Planet); poscomp.Position.NormalizeChunkIndexXY(planet.Size); entity.Cache.SetCenter(planet, new Index2(poscomp.Position.ChunkIndex)); return true; @@ -41,8 +42,8 @@ protected override void UpdateEntity(GameTime gameTime,Entity e, MoveableCompone var newposition = poscomp.Position + movecomp.PositionMove; newposition.NormalizeChunkIndexXY(e.Cache.Planet.Size); var result = e.Cache.SetCenter(e.Cache.Planet, new Index2(poscomp.Position.ChunkIndex)); - if (result) - poscomp.Position = newposition; + if (result) poscomp.Position = newposition; + //Direction if (movecomp.PositionMove.LengthSquared != 0) @@ -52,7 +53,7 @@ protected override void UpdateEntity(GameTime gameTime,Entity e, MoveableCompone } } - private void CheckBoxCollision(GameTime gameTime,Entity e,MoveableComponent movecomp,PositionComponent poscomp) + private void CheckBoxCollision(GameTime gameTime, Entity e, MoveableComponent movecomp, PositionComponent poscomp) { BodyComponent bc = new BodyComponent(); if (e.Components.ContainsComponent()) @@ -64,27 +65,27 @@ private void CheckBoxCollision(GameTime gameTime,Entity e,MoveableComponent move Vector3 move = movecomp.PositionMove; //Blocks finden die eine Kollision verursachen könnten - int minx = (int)Math.Floor(Math.Min( + int minx = (int) Math.Floor(Math.Min( position.BlockPosition.X - bc.Radius, - position.BlockPosition.X - bc.Radius + movecomp.PositionMove.X)); - int maxx = (int)Math.Ceiling(Math.Max( + position.BlockPosition.X - bc.Radius + movecomp.PositionMove.X)); + int maxx = (int) Math.Ceiling(Math.Max( position.BlockPosition.X + bc.Radius, position.BlockPosition.X + bc.Radius + movecomp.PositionMove.X)); - int miny = (int)Math.Floor(Math.Min( + int miny = (int) Math.Floor(Math.Min( position.BlockPosition.Y - bc.Radius, position.BlockPosition.Y - bc.Radius + movecomp.PositionMove.Y)); - int maxy = (int)Math.Ceiling(Math.Max( + int maxy = (int) Math.Ceiling(Math.Max( position.BlockPosition.Y + bc.Radius, position.BlockPosition.Y + bc.Radius + movecomp.PositionMove.Y)); - int minz = (int)Math.Floor(Math.Min( + int minz = (int) Math.Floor(Math.Min( position.BlockPosition.Z, position.BlockPosition.Z + movecomp.PositionMove.Z)); - int maxz = (int)Math.Ceiling(Math.Max( + int maxz = (int) Math.Ceiling(Math.Max( position.BlockPosition.Z + bc.Height, position.BlockPosition.Z + bc.Height + movecomp.PositionMove.Z)); //Beteiligte Flächen des Spielers - var playerplanes = CollisionPlane.GetPlayerCollisionPlanes(bc, movecomp, poscomp).ToList(); + var playerplanes = CollisionPlane.GetPlayerCollisionPlanes(bc.Radius, bc.Height, movecomp.Velocity, poscomp.Position); bool abort = false; @@ -94,7 +95,7 @@ private void CheckBoxCollision(GameTime gameTime,Entity e,MoveableComponent move { for (int x = minx; x <= maxx && !abort; x++) { - move = movecomp.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds; + move = movecomp.Velocity * (float) gameTime.ElapsedGameTime.TotalSeconds; Index3 pos = new Index3(x, y, z); Index3 blockPos = pos + position.GlobalBlockIndex; @@ -116,7 +117,7 @@ where CollisionPlane.CheckDistance(distance, move) foreach (var plane in planes) { - var subvelocity = (plane.Distance / (float)gameTime.ElapsedGameTime.TotalSeconds); + var subvelocity = (plane.Distance / (float) gameTime.ElapsedGameTime.TotalSeconds); var diff = movecomp.Velocity - subvelocity; float vx; @@ -153,7 +154,7 @@ where CollisionPlane.CheckDistance(distance, move) // TODO: Was ist für den Fall Gravitation = 0 oder im Scheitelpunkt des Sprungs? //movecomp.OnGround = Player.Velocity.Z == 0f; - movecomp.PositionMove = movecomp.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds; + movecomp.PositionMove = movecomp.Velocity * (float) gameTime.ElapsedGameTime.TotalSeconds; } } } diff --git a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/NewtonGravitatorComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/NewtonGravitatorComponent.cs index 935df6be..fe53f525 100644 --- a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/NewtonGravitatorComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/NewtonGravitatorComponent.cs @@ -6,11 +6,12 @@ using System.Threading.Tasks; using engenious; using OctoAwesome.EntityComponents; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.SimulationComponents { [EntityFilter(typeof(GravityComponent),typeof(BodyComponent))] - public class NewtonGravitatorComponent : SimulationComponent + public class NewtonGravitatorComponent : OSimulationComponent { class GravityEntity { diff --git a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/PowerAggregatorComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/PowerAggregatorComponent.cs index 064d52a3..68fadeb8 100644 --- a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/PowerAggregatorComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/PowerAggregatorComponent.cs @@ -5,11 +5,12 @@ using System.Threading.Tasks; using engenious; using OctoAwesome.Basics.EntityComponents; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.SimulationComponents { [EntityFilter(typeof(PowerComponent), typeof(MoveableComponent))] - public sealed class PowerAggregatorComponent : SimulationComponent + public sealed class PowerAggregatorComponent : OSimulationComponent { private List poweredEntities = new List(); diff --git a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/WattMoverComponent.cs b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/WattMoverComponent.cs index 74b862d3..b5bd5470 100644 --- a/OctoAwesome/OctoAwesome.Basics/SimulationComponents/WattMoverComponent.cs +++ b/OctoAwesome/OctoAwesome.Basics/SimulationComponents/WattMoverComponent.cs @@ -7,11 +7,12 @@ using System.Threading.Tasks; using engenious; using engenious.Helper; +using OctoAwesome.Entities; namespace OctoAwesome.Basics.SimulationComponents { [EntityFilter(typeof(ControllableComponent), typeof(BodyPowerComponent))] - public class WattMoverComponent : SimulationComponent + public class WattMoverComponent : OSimulationComponent { protected override bool AddEntity(Entity entity) { @@ -32,7 +33,7 @@ protected override void UpdateEntity(GameTime gameTime, Entity e, ControllableCo float lookX = (float)Math.Cos(head.Angle); float lookY = -(float)Math.Sin(head.Angle); - var velocitydirection = new Vector3(lookX, lookY, 0) * controller.MoveInput.Y; + var velocitydirection = new Vector3(lookX, lookY, 0) * controller.MoveInput.Y; float stafeX = (float)Math.Cos(head.Angle + MathHelper.PiOver2); float stafeY = -(float)Math.Sin(head.Angle + MathHelper.PiOver2); @@ -43,7 +44,7 @@ protected override void UpdateEntity(GameTime gameTime, Entity e, ControllableCo } else { - powercomp.Direction = new Vector3(controller.MoveInput.X,controller.MoveInput.Y); + powercomp.Direction = new Vector3(controller.MoveInput.X, controller.MoveInput.Y); } //Jump diff --git a/OctoAwesome/OctoAwesome.Basics/WauziPopulator.cs b/OctoAwesome/OctoAwesome.Basics/WauziPopulator.cs index ffcfba6f..4fa2c381 100644 --- a/OctoAwesome/OctoAwesome.Basics/WauziPopulator.cs +++ b/OctoAwesome/OctoAwesome.Basics/WauziPopulator.cs @@ -9,6 +9,7 @@ namespace OctoAwesome.Basics { + // TODO: populator system überarbeiten? public class WauziPopulator : IMapPopulator { @@ -34,8 +35,12 @@ public void Populate(IResourceManager resourcemanager, IPlanet planet, IChunkCol var x = r.Next(0, Chunk.CHUNKSIZE_X/2); var y = r.Next(0, Chunk.CHUNKSIZE_Y/2); + // TODO: der code wird immer redundanter :D PositionComponent position = new PositionComponent() { Position = new Coordinate(0, new Index3(x+column00.Index.X*Chunk.CHUNKSIZE_X, y + column00.Index.Y * Chunk.CHUNKSIZE_Y, 200), new Vector3(0, 0, 0)) }; wauzi.Components.AddComponent(position); + // TODO: warum wird das wauzi nicht direkt auf dem planeten gespritz... + // und findet dann seinen weg über die postion zum chunck ? + // oder noch besser in die simulation column00.Entities.Add(wauzi); } diff --git a/OctoAwesome/OctoAwesome.Basics/packages.config b/OctoAwesome/OctoAwesome.Basics/packages.config index ac2023de..1bd314fd 100644 --- a/OctoAwesome/OctoAwesome.Basics/packages.config +++ b/OctoAwesome/OctoAwesome.Basics/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/OctoAwesome/OctoAwesome.Client/Components/CameraComponent.cs b/OctoAwesome/OctoAwesome.Client/Components/CameraComponent.cs index 27ad9dab..cf701c43 100644 --- a/OctoAwesome/OctoAwesome.Client/Components/CameraComponent.cs +++ b/OctoAwesome/OctoAwesome.Client/Components/CameraComponent.cs @@ -1,7 +1,6 @@ using System; using engenious; using engenious.Helper; -using OctoAwesome.EntityComponents; namespace OctoAwesome.Client.Components { @@ -36,24 +35,23 @@ public override void Update(GameTime gameTime) if (player == null || player.CurrentEntity == null) return; + + Coordinate position = player.CurrentEntity.Position; - Entity entity = player.CurrentEntity; - HeadComponent head = player.CurrentEntityHead; - PositionComponent position = player.Position; + CameraChunk = position.ChunkIndex; - CameraChunk = position.Position.ChunkIndex; - - CameraPosition = position.Position.LocalPosition + head.Offset; + CameraPosition = position.LocalPosition + player.HeadOffset; + // TODO: braucht man das ? CameraUpVector = new Vector3(0, 0, 1f); - float height = (float)Math.Sin(head.Tilt); - float distance = (float)Math.Cos(head.Tilt); + float height = (float)Math.Sin(player.HeadTilt); + float distance = (float)Math.Cos(player.HeadTilt); - float lookX = (float)Math.Cos(head.Angle) * distance; - float lookY = -(float)Math.Sin(head.Angle) * distance; + float lookX = (float)Math.Cos(player.HeadYaw) * distance; + float lookY = -(float)Math.Sin(player.HeadYaw) * distance; - float strafeX = (float)Math.Cos(head.Angle + MathHelper.PiOver2); - float strafeY = -(float)Math.Sin(head.Angle + MathHelper.PiOver2); + float strafeX = (float)Math.Cos(player.HeadYaw + MathHelper.PiOver2); + float strafeY = -(float)Math.Sin(player.HeadYaw + MathHelper.PiOver2); CameraUpVector = Vector3.Cross(new Vector3(strafeX, strafeY, 0), new Vector3(lookX, lookY, height)); @@ -68,12 +66,12 @@ public override void Update(GameTime gameTime) MinimapView = Matrix.CreateLookAt( new Vector3(CameraPosition.X, CameraPosition.Y, 100), new Vector3( - position.Position.LocalPosition.X, - position.Position.LocalPosition.Y, + position.LocalPosition.X, + position.LocalPosition.Y, 0f), new Vector3( - (float)Math.Cos(head.Angle), - (float)Math.Sin(-head.Angle), 0f)); + (float)Math.Cos(player.HeadYaw), + (float)Math.Sin(-player.HeadYaw), 0f)); float centerX = GraphicsDevice.Viewport.Width / 2; float centerY = GraphicsDevice.Viewport.Height / 2; diff --git a/OctoAwesome/OctoAwesome.Client/Components/EntityComponent.cs b/OctoAwesome/OctoAwesome.Client/Components/EntityComponent.cs index a69cb666..03c9eef2 100644 --- a/OctoAwesome/OctoAwesome.Client/Components/EntityComponent.cs +++ b/OctoAwesome/OctoAwesome.Client/Components/EntityComponent.cs @@ -1,17 +1,16 @@ using engenious; using engenious.Graphics; using engenious.Helper; -using OctoAwesome.EntityComponents; -using System; +using OctoAwesome.Entities; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace OctoAwesome.Client.Components { internal sealed class EntityComponent : GameComponent { + public SimulationComponent Simulation { get; private set; } + public IEnumerable Entities { get { return entities; } } private struct ModelInfo { public bool render; @@ -20,25 +19,20 @@ private struct ModelInfo } private GraphicsDevice graphicsDevice; private BasicEffect effect; - public SimulationComponent Simulation { get; private set; } - - private Dictionary models = new Dictionary(); - - - public List Entities { get; set; } - + private List entities; public EntityComponent(Game game, SimulationComponent simulation) : base(game) { Simulation = simulation; - Entities = new List(); + entities = new List(); graphicsDevice = game.GraphicsDevice; effect = new BasicEffect(graphicsDevice); } public void Draw(Matrix view, Matrix projection, Index3 chunkOffset, Index2 planetSize) { + if (entities.Count() == 0) return; effect.Projection = projection; effect.View = view; effect.TextureEnabled = true; @@ -47,47 +41,45 @@ public void Draw(Matrix view, Matrix projection, Index3 chunkOffset, Index2 plan { pass.Apply(); - foreach (var entity in Entities) + foreach (var entity in entities) { - if (!entity.Components.ContainsComponent()) - { - continue; - } + //if (!entity.Components.ContainsComponent()) + //{ + // continue; + //} + //var rendercomp = entity.Components.GetComponent(); - var rendercomp = entity.Components.GetComponent(); + var drawable = entity as Entities.IDrawable; ModelInfo modelinfo; - if (!models.TryGetValue(rendercomp.Name,out modelinfo)) + if (!models.TryGetValue(drawable.Name, out modelinfo)) { modelinfo = new ModelInfo() { + model = Game.Content.Load(drawable.ModelName), + texture = Game.Content.Load(drawable.TextureName), render = true, - model = Game.Content.Load(rendercomp.ModelName), - texture = Game.Content.Load(rendercomp.TextureName), - }; + }; } if (!modelinfo.render) continue; + + Coordinate position = entity.Position; + //var body = entity.Components.GetComponent(); - var positioncomp = entity.Components.GetComponent(); - var position = positioncomp.Position; - var body = entity.Components.GetComponent(); - - HeadComponent head = new HeadComponent(); - if (entity.Components.ContainsComponent()) - head = entity.Components.GetComponent(); - - Index3 shift = chunkOffset.ShortestDistanceXY( - position.ChunkIndex, planetSize); - - var rotation = MathHelper.WrapAngle(positioncomp.Direction + MathHelper.ToRadians(rendercomp.BaseZRotation)); + Index3 shift = chunkOffset.ShortestDistanceXY(position.ChunkIndex, planetSize); + // TODO: direct aus der bewegung berechnen + // MathHelper.WrapAngle((float) Math.Atan2(movecomp.PositionMove.Y, movecomp.PositionMove.X)); + var rotation = MathHelper.WrapAngle(drawable.Azimuth + MathHelper.ToRadians(drawable.BaseRotationZ)); Matrix world = Matrix.CreateTranslation( shift.X * Chunk.CHUNKSIZE_X + position.LocalPosition.X, shift.Y * Chunk.CHUNKSIZE_Y + position.LocalPosition.Y, - shift.Z * Chunk.CHUNKSIZE_Z + position.LocalPosition.Z) * Matrix.CreateScaling(body.Radius * 2, body.Radius * 2, body.Height)*Matrix.CreateRotationZ(rotation); + shift.Z * Chunk.CHUNKSIZE_Z + position.LocalPosition.Z) * + Matrix.CreateScaling(drawable.Body.X, drawable.Body.Y, drawable.Body.Z) * + Matrix.CreateRotationZ(rotation); effect.World = world; modelinfo.model.Transform = world; modelinfo.model.Draw(effect, modelinfo.texture); @@ -97,15 +89,12 @@ public void Draw(Matrix view, Matrix projection, Index3 chunkOffset, Index2 plan public override void Update(GameTime gameTime) { - if (Simulation.Simulation == null) - return; - var simulation = Simulation.Simulation; - if (!(simulation.State == SimulationState.Running || simulation.State == SimulationState.Paused)) + if (simulation == null || !(simulation.State == SimulationState.Running || simulation.State == SimulationState.Paused)) return; - Entities = simulation.Entities.Where(i => i.Components.ContainsComponent()).ToList(); + entities = simulation.Entities.Where(i => i is Entities.IDrawable).ToList(); //base.Update(gameTime); } diff --git a/OctoAwesome/OctoAwesome.Client/Components/PlayerComponent.cs b/OctoAwesome/OctoAwesome.Client/Components/PlayerComponent.cs index 0e4b77cb..f70f5181 100644 --- a/OctoAwesome/OctoAwesome.Client/Components/PlayerComponent.cs +++ b/OctoAwesome/OctoAwesome.Client/Components/PlayerComponent.cs @@ -1,54 +1,56 @@ -using OctoAwesome.Runtime; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System; using engenious; -using OctoAwesome.EntityComponents; +using OctoAwesome.Entities; namespace OctoAwesome.Client.Components { - internal sealed class PlayerComponent : GameComponent + internal sealed class PlayerComponent : GameComponent, IController { private new OctoGame Game; private IResourceManager resourceManager; - #region External Input + public Vector3 HeadOffset { get; set; } - public Vector2 HeadInput { get; set; } + #region IController Interface - public Vector2 MoveInput { get; set; } + public float HeadTilt { get; set; } - public bool InteractInput { get; set; } + public float HeadYaw { get; set; } - public bool ApplyInput { get; set; } + public InputTrigger InteractInput { get; } - public bool JumpInput { get; set; } + public InputTrigger ApplyInput { get; } - public bool FlymodeInput { get; set; } + public InputTrigger JumpInput { get; } - public bool[] SlotInput { get; private set; } = new bool[10]; + public Vector2 HeadValue { get; set; } - public bool SlotLeftInput { get; set; } + public Vector2 MoveValue { get; set; } - public bool SlotRightInput { get; set; } + public Index3? InteractBlock { get; set; } - #endregion + public Index3? ApplyBlock { get; set; } - public Entity CurrentEntity { get; private set; } + public OrientationFlags? ApplySide { get; set; } - public HeadComponent CurrentEntityHead { get; private set; } + #endregion - public ControllableComponent CurrentController { get; private set; } + #region External Inputs + public bool FlymodeInput { get; set; } - public InventoryComponent Inventory { get; private set; } + public bool[] SlotInput { get; private set; } = new bool[10]; - public ToolBarComponent Toolbar { get; private set; } + public bool SlotLeftInput { get; set; } - public PositionComponent Position { get; private set; } + public bool SlotRightInput { get; set; } + #endregion - // public ActorHost ActorHost { get; private set; } + public Entity CurrentEntity { get; private set; } + + //TODO: wieder hinzufügen, oder anders lösen + //public InventoryComponent Inventory { get; private set; } + //public ToolBarComponent Toolbar { get; private set; } public Index3? SelectedBox { get; set; } @@ -60,127 +62,119 @@ internal sealed class PlayerComponent : GameComponent public OrientationFlags SelectedCorner { get; set; } - public PlayerComponent(OctoGame game, IResourceManager resourceManager) - : base(game) + public PlayerComponent(OctoGame game, IResourceManager resourceManager) : base(game) { this.resourceManager = resourceManager; + ApplyInput = new InputTrigger(); + InteractInput = new InputTrigger(); + JumpInput = new InputTrigger(); Game = game; } public void SetEntity(Entity entity) { CurrentEntity = entity; - - - - if (CurrentEntity == null) + if (CurrentEntity != null && CurrentEntity is IControllable current) + current.Reset(); + if (entity is IControllable controllable) + controllable.Register(this); + if (CurrentEntity is Entities.IDrawable draw) + HeadOffset = draw.Body; + else HeadOffset = new Vector3(0, 0, 3.2f); + + //CurrentEntityHead.Angle += (float)gameTime.ElapsedGameTime.TotalSeconds * HeadInput.X; + //CurrentEntityHead.Tilt += (float)gameTime.ElapsedGameTime.TotalSeconds * HeadInput.Y; + //CurrentEntityHead.Tilt = Math.Min(1.5f, Math.Max(-1.5f, CurrentEntityHead.Tilt)); + + if (CurrentEntity != null) { - CurrentEntityHead = null; - } - else - { - // Map other Components - - CurrentController = entity.Components.GetComponent(); - - CurrentEntityHead = CurrentEntity.Components.GetComponent(); - if (CurrentEntityHead == null) CurrentEntityHead = new HeadComponent(); - - Inventory = CurrentEntity.Components.GetComponent(); - if (Inventory == null) Inventory = new InventoryComponent(); - - Toolbar = CurrentEntity.Components.GetComponent(); - if (Toolbar == null) Toolbar = new ToolBarComponent(); - - Position = CurrentEntity.Components.GetComponent(); - if (Position == null) Position = new PositionComponent() { Position = new Coordinate(0, new Index3(0, 0, 0), new Vector3(0, 0, 0)) }; + // Map other Components + //CurrentEntityHead = CurrentEntity.Components.GetComponent(); + //if (CurrentEntityHead == null) CurrentEntityHead = new HeadComponent(); + + // TODO: toolbar und inventory wieder hinzufügen, oder anders lösen + //Inventory = CurrentEntity.Components.GetComponent(); + //if (Inventory == null) Inventory = new InventoryComponent(); + //Toolbar = CurrentEntity.Components.GetComponent(); + //if (Toolbar == null) Toolbar = new ToolBarComponent(); } } public override void Update(GameTime gameTime) { - if (!Enabled) - return; - - if (CurrentEntity == null) + if (!Enabled || CurrentEntity == null) return; - CurrentEntityHead.Angle += (float)gameTime.ElapsedGameTime.TotalSeconds * HeadInput.X; - CurrentEntityHead.Tilt += (float)gameTime.ElapsedGameTime.TotalSeconds * HeadInput.Y; - CurrentEntityHead.Tilt = Math.Min(1.5f, Math.Max(-1.5f, CurrentEntityHead.Tilt)); - HeadInput = Vector2.Zero; - - CurrentController.MoveInput = MoveInput; - MoveInput = Vector2.Zero; - - CurrentController.JumpInput = JumpInput; - JumpInput = false; - - if (InteractInput && SelectedBox.HasValue) - CurrentController.InteractBlock = SelectedBox.Value; - InteractInput = false; + HeadYaw += (float) gameTime.ElapsedGameTime.TotalSeconds * HeadValue.X; + HeadTilt = Math.Min(1.5f, Math.Max(-1.5f, HeadTilt + (float) gameTime.ElapsedGameTime.TotalSeconds * HeadValue.X)); - if (ApplyInput && SelectedBox.HasValue) + if (InteractInput.Value && SelectedBox.HasValue) { - CurrentController.ApplyBlock = SelectedBox.Value; - CurrentController.ApplySide = SelectedSide; + InteractBlock = SelectedBox.Value; } - ApplyInput = false; + if (ApplyInput.Value && SelectedBox.HasValue) + { + ApplyBlock = SelectedBox.Value; + ApplySide = SelectedSide; + } + //TODO: was ist damit //if (FlymodeInput) // ActorHost.Player.FlyMode = !ActorHost.Player.FlyMode; //FlymodeInput = false; - - if (Toolbar.Tools != null && Toolbar.Tools.Length > 0) - { - if (Toolbar.ActiveTool == null) Toolbar.ActiveTool = Toolbar.Tools[0]; - for (int i = 0; i < Math.Min(Toolbar.Tools.Length, SlotInput.Length); i++) - { - if (SlotInput[i]) - Toolbar.ActiveTool = Toolbar.Tools[i]; - SlotInput[i] = false; - } - } + #region Toolbar + //TODO: wieder hinzufügen, oder anders lösen + //if (Toolbar.Tools != null && Toolbar.Tools.Length > 0) + //{ + // if (Toolbar.ActiveTool == null) Toolbar.ActiveTool = Toolbar.Tools[0]; + // for (int i = 0; i < Math.Min(Toolbar.Tools.Length, SlotInput.Length); i++) + // { + // if (SlotInput[i]) + // Toolbar.ActiveTool = Toolbar.Tools[i]; + // SlotInput[i] = false; + // } + //} //Index des aktiven Werkzeugs ermitteln - int activeTool = -1; - List toolIndices = new List(); - if (Toolbar.Tools != null) - { - for (int i = 0; i < Toolbar.Tools.Length; i++) - { - if (Toolbar.Tools[i] != null) - toolIndices.Add(i); - - if (Toolbar.Tools[i] == Toolbar.ActiveTool) - activeTool = toolIndices.Count - 1; - } - } - - if (SlotLeftInput) - { - if (activeTool > -1) - activeTool--; - else if (toolIndices.Count > 0) - activeTool = toolIndices[toolIndices.Count - 1]; - } - SlotLeftInput = false; - - if (SlotRightInput) - { - if (activeTool > -1) - activeTool++; - else if (toolIndices.Count > 0) - activeTool = toolIndices[0]; - } - SlotRightInput = false; - - if (activeTool > -1) - { - activeTool = (activeTool + toolIndices.Count) % toolIndices.Count; - Toolbar.ActiveTool = Toolbar.Tools[toolIndices[activeTool]]; - } + //int activeTool = -1; + //List toolIndices = new List(); + //if (Toolbar.Tools != null) + //{ + // for (int i = 0; i < Toolbar.Tools.Length; i++) + // { + // if (Toolbar.Tools[i] != null) + // toolIndices.Add(i); + + // if (Toolbar.Tools[i] == Toolbar.ActiveTool) + // activeTool = toolIndices.Count - 1; + // } + //} + + //if (SlotLeftInput) + //{ + // if (activeTool > -1) + // activeTool--; + // else if (toolIndices.Count > 0) + // activeTool = toolIndices[toolIndices.Count - 1]; + //} + //SlotLeftInput = false; + + //if (SlotRightInput) + //{ + // if (activeTool > -1) + // activeTool++; + // else if (toolIndices.Count > 0) + // activeTool = toolIndices[0]; + //} + //SlotRightInput = false; + + //if (activeTool > -1) + //{ + // activeTool = (activeTool + toolIndices.Count) % toolIndices.Count; + // Toolbar.ActiveTool = Toolbar.Tools[toolIndices[activeTool]]; + //} + #endregion } /// @@ -188,17 +182,17 @@ public override void Update(GameTime gameTime) /// internal void AllBlocksDebug() { - var inventory = CurrentEntity.Components.GetComponent(); - if (inventory == null) - return; + //var inventory = CurrentEntity.Components.GetComponent(); + //if (inventory == null) + // return; - var blockDefinitions = resourceManager.DefinitionManager.GetBlockDefinitions(); - foreach (var blockDefinition in blockDefinitions) - inventory.AddUnit(blockDefinition); + //var blockDefinitions = resourceManager.DefinitionManager.GetBlockDefinitions(); + //foreach (var blockDefinition in blockDefinitions) + // inventory.AddUnit(blockDefinition); - var itemDefinitions = resourceManager.DefinitionManager.GetItemDefinitions(); - foreach (var itemDefinition in itemDefinitions) - inventory.AddUnit(itemDefinition); + //var itemDefinitions = resourceManager.DefinitionManager.GetItemDefinitions(); + //foreach (var itemDefinition in itemDefinitions) + // inventory.AddUnit(itemDefinition); } } } diff --git a/OctoAwesome/OctoAwesome.Client/Controls/CompassControl.cs b/OctoAwesome/OctoAwesome.Client/Controls/CompassControl.cs index 8d501b9c..6c7c2c77 100644 --- a/OctoAwesome/OctoAwesome.Client/Controls/CompassControl.cs +++ b/OctoAwesome/OctoAwesome.Client/Controls/CompassControl.cs @@ -31,7 +31,7 @@ protected override void OnDrawContent(SpriteBatch batch, Rectangle contentArea, if (Player == null || Player.CurrentEntity == null || !assets.Ready) return; - float compassValue = Player.CurrentEntityHead.Angle / (float)(2 * Math.PI); + float compassValue = Player.HeadYaw / (float)(2 * Math.PI); compassValue %= 1f; if (compassValue < 0) compassValue += 1f; diff --git a/OctoAwesome/OctoAwesome.Client/Controls/DebugControl.cs b/OctoAwesome/OctoAwesome.Client/Controls/DebugControl.cs index 6cfb5713..a709a3e5 100644 --- a/OctoAwesome/OctoAwesome.Client/Controls/DebugControl.cs +++ b/OctoAwesome/OctoAwesome.Client/Controls/DebugControl.cs @@ -1,8 +1,5 @@ using MonoGameUi; -using System.Collections.Generic; -using OctoAwesome.Runtime; using OctoAwesome.Client.Components; -using System; using engenious; using engenious.Graphics; using System.Linq; @@ -153,14 +150,14 @@ protected override void OnDrawContent(SpriteBatch batch, Rectangle contentArea, controlInfo.Text = Languages.OctoClient.ActiveControls + ": " + ScreenManager.ActiveScreen.Controls.Count; //Draw Position - string pos = "pos: " + Player.Position.Position.ToString(); + string pos = "pos: " + Player.CurrentEntity.Position.ToString(); position.Text = pos; //Draw Rotation - float grad = (Player.CurrentEntityHead.Angle / MathHelper.TwoPi) * 360; + float grad = (Player.HeadYaw / MathHelper.TwoPi) * 360; string rot = "rot: " + - (((Player.CurrentEntityHead.Angle / MathHelper.TwoPi) * 360) % 360).ToString("0.00") + " / " + - ((Player.CurrentEntityHead.Tilt / MathHelper.TwoPi) * 360).ToString("0.00"); + (((Player.HeadYaw / MathHelper.TwoPi) * 360) % 360).ToString("0.00") + " / " + + ((Player.HeadTilt / MathHelper.TwoPi) * 360).ToString("0.00"); rotation.Text = rot; //Draw Fps @@ -193,12 +190,12 @@ protected override void OnDrawContent(SpriteBatch batch, Rectangle contentArea, //if (Player.ActorHost.Player.FlyMode) flyInfo.Text = Languages.OctoClient.FlymodeEnabled; //else flyInfo.Text = ""; - IPlanet planet = manager.Game.ResourceManager.GetPlanet(Player.Position.Position.Planet); + IPlanet planet = manager.Game.ResourceManager.GetPlanet(Player.CurrentEntity.Position.Planet); // Temperature Info - temperatureInfo.Text = Languages.OctoClient.Temperature + ": " + planet.ClimateMap.GetTemperature(Player.Position.Position.GlobalBlockIndex); + temperatureInfo.Text = Languages.OctoClient.Temperature + ": " + planet.ClimateMap.GetTemperature(Player.CurrentEntity.Position.GlobalBlockIndex); // Precipitation Info - precipitationInfo.Text = "Precipitation: " + planet.ClimateMap.GetPrecipitation(Player.Position.Position.GlobalBlockIndex); + precipitationInfo.Text = "Precipitation: " + planet.ClimateMap.GetPrecipitation(Player.CurrentEntity.Position.GlobalBlockIndex); //Draw Box Information if (Player.SelectedBox.HasValue) diff --git a/OctoAwesome/OctoAwesome.Client/Controls/InventoryControl.cs b/OctoAwesome/OctoAwesome.Client/Controls/InventoryControl.cs index c1c8184e..d464b216 100644 --- a/OctoAwesome/OctoAwesome.Client/Controls/InventoryControl.cs +++ b/OctoAwesome/OctoAwesome.Client/Controls/InventoryControl.cs @@ -16,7 +16,6 @@ internal sealed class InventoryControl : Panel public InventoryControl(ScreenComponent manager, int columns = COLUMNS) : base(manager) { - ScrollContainer scroll = new ScrollContainer(manager) { @@ -32,41 +31,41 @@ public InventoryControl(ScreenComponent manager, int columns = COLUMNS) : base(m }; for (int i = 0; i < columns; i++) grid.Columns.Add(new ColumnDefinition() { ResizeMode = ResizeMode.Parts, Width = 1 }); - int rows = (int)System.Math.Ceiling((float)manager.Game.Player.Inventory.Inventory.Count / columns); - for (int i = 0; i < rows; i++) - grid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Fixed, Height = 50 }); + // TODO: wieder einfügen + //int rows = (int)System.Math.Ceiling((float)manager.Game.Player.Inventory.Inventory.Count / columns); + //for (int i = 0; i < rows; i++) + // grid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Fixed, Height = 50 }); int column = 0; int row = 0; - foreach (var item in manager.Game.Player.Inventory.Inventory) - { - Texture2D texture = manager.Game.Assets.LoadTexture(item.Definition.GetType(), item.Definition.Icon); + // TODO: wieder einfügen + //foreach (var item in manager.Game.Player.Inventory.Inventory) + //{ + // Texture2D texture = manager.Game.Assets.LoadTexture(item.Definition.GetType(), item.Definition.Icon); - var image = new Image(manager) { Texture = texture, Width = 42, Height = 42, VerticalAlignment = VerticalAlignment.Center }; - image.MouseEnter += (s, e) => { HoveredSlot = item; }; - image.MouseLeave += (s, e) => { HoveredSlot = null; }; - image.StartDrag += (e) => - { - e.Handled = true; - e.Icon = texture; - e.Content = item; - e.Sender = image; - }; - var label = new Label(manager) { Text = item.Amount.ToString(), HorizontalAlignment = HorizontalAlignment.Right, VerticalTextAlignment = VerticalAlignment.Bottom, Background = new BorderBrush(Color.White) }; - grid.AddControl(image, column, row); - grid.AddControl(label, column, row); + // var image = new Image(manager) { Texture = texture, Width = 42, Height = 42, VerticalAlignment = VerticalAlignment.Center }; + // image.MouseEnter += (s, e) => { HoveredSlot = item; }; + // image.MouseLeave += (s, e) => { HoveredSlot = null; }; + // image.StartDrag += (e) => + // { + // e.Handled = true; + // e.Icon = texture; + // e.Content = item; + // e.Sender = image; + // }; + // var label = new Label(manager) { Text = item.Amount.ToString(), HorizontalAlignment = HorizontalAlignment.Right, VerticalTextAlignment = VerticalAlignment.Bottom, Background = new BorderBrush(Color.White) }; + // grid.AddControl(image, column, row); + // grid.AddControl(label, column, row); - column++; - if (column >= columns) - { - row++; - column = 0; - } - } + // column++; + // if (column >= columns) + // { + // row++; + // column = 0; + // } + //} scroll.Content = grid; - - } } } diff --git a/OctoAwesome/OctoAwesome.Client/Controls/SceneControl.cs b/OctoAwesome/OctoAwesome.Client/Controls/SceneControl.cs index 7edb3073..7f41140b 100644 --- a/OctoAwesome/OctoAwesome.Client/Controls/SceneControl.cs +++ b/OctoAwesome/OctoAwesome.Client/Controls/SceneControl.cs @@ -106,10 +106,10 @@ public SceneControl(ScreenComponent manager, string style = "") : } } + // TODO: valid planet id should be > 0 planet = Manager.Game.ResourceManager.GetPlanet(0); // TODO: evtl. Cache-Size (Dimensions) VIEWRANGE + 1 - int range = ((int)Math.Pow(2, VIEWRANGE) - 2) / 2; localChunkCache = new LocalChunkCache(Manager.Game.ResourceManager.GlobalChunkCache,false, VIEWRANGE, range); @@ -238,8 +238,8 @@ protected override void OnUpdate(GameTime gameTime) sunPosition += (float)gameTime.ElapsedGameTime.TotalMinutes * MathHelper.TwoPi; - Index3 centerblock = player.Position.Position.GlobalBlockIndex; - Index3 renderOffset = player.Position.Position.ChunkIndex * Chunk.CHUNKSIZE; + Index3 centerblock = player.CurrentEntity.Position.GlobalBlockIndex; + Index3 renderOffset = player.CurrentEntity.Position.ChunkIndex * Chunk.CHUNKSIZE; Index3? selected = null; Axis? selectedAxis = null; @@ -332,7 +332,7 @@ protected override void OnUpdate(GameTime gameTime) player.SelectedCorner = OrientationFlags.None; } - Index2 destinationChunk = new Index2(player.Position.Position.ChunkIndex); + Index2 destinationChunk = new Index2(player.CurrentEntity.Position.ChunkIndex); // Nur ausführen wenn der Spieler den Chunk gewechselt hat if (destinationChunk != currentChunk) @@ -359,8 +359,8 @@ protected override void OnPreDraw(GameTime gameTime) float octoDaysPerEarthDay = 360f; float inclinationVariance = MathHelper.Pi / 3f; - float playerPosX = ((float)player.Position.Position.GlobalPosition.X / (planet.Size.X * Chunk.CHUNKSIZE_X)) * MathHelper.TwoPi; - float playerPosY = ((float)player.Position.Position.GlobalPosition.Y / (planet.Size.Y * Chunk.CHUNKSIZE_Y)) * MathHelper.TwoPi; + float playerPosX = ((float)player.CurrentEntity.Position.GlobalPosition.X / (planet.Size.X * Chunk.CHUNKSIZE_X)) * MathHelper.TwoPi; + float playerPosY = ((float)player.CurrentEntity.Position.GlobalPosition.Y / (planet.Size.Y * Chunk.CHUNKSIZE_Y)) * MathHelper.TwoPi; TimeSpan diff = DateTime.UtcNow - new DateTime(1888, 8, 8); @@ -424,7 +424,7 @@ protected override void OnPreDraw(GameTime gameTime) // GraphicsDevice.RasterizerState = RasterizerState.CullNone; sunEffect.Texture = sunTexture; Matrix billboard = Matrix.Invert(camera.View); - billboard.Translation = player.Position.Position.LocalPosition + (sunDirection * -10); + billboard.Translation = player.CurrentEntity.Position.LocalPosition + (sunDirection * -10); sunEffect.World = billboard; sunEffect.View = camera.View; sunEffect.Projection = camera.Projection; @@ -499,14 +499,13 @@ private void FillChunkRenderer() if (player.CurrentEntity == null) return; - Index2 destinationChunk = new Index2(player.Position.Position.ChunkIndex); + Index2 destinationChunk = new Index2(player.CurrentEntity.Position.ChunkIndex); // Nur ausführen wenn der Spieler den Chunk gewechselt hat if (destinationChunk != currentChunk) { - localChunkCache.SetCenter( - planet, - new Index2(player.Position.Position.ChunkIndex), + localChunkCache.SetCenter( planet, + new Index2(player.CurrentEntity.Position.ChunkIndex), b => { if (b) { @@ -534,7 +533,7 @@ private void FillChunkRenderer() } } - Index3 comparationIndex = player.Position.Position.ChunkIndex; + Index3 comparationIndex = player.CurrentEntity.Position.ChunkIndex; orderedChunkRenderer.Sort((x, y) => { if (!x.ChunkPosition.HasValue) return 1; diff --git a/OctoAwesome/OctoAwesome.Client/Controls/ToolbarControl.cs b/OctoAwesome/OctoAwesome.Client/Controls/ToolbarControl.cs index 7268cc5c..f540b988 100644 --- a/OctoAwesome/OctoAwesome.Client/Controls/ToolbarControl.cs +++ b/OctoAwesome/OctoAwesome.Client/Controls/ToolbarControl.cs @@ -1,24 +1,21 @@ using MonoGameUi; using OctoAwesome.Client.Components; -using OctoAwesome.Runtime; -using System; using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; using engenious; using engenious.Graphics; -using OctoAwesome.EntityComponents; namespace OctoAwesome.Client.Controls { internal class ToolbarControl : Panel { private Dictionary toolTextures; + // TODO: dynamisch aus der aktuellen toolbar generieren + //private Button[] buttons = new Button[ToolBarComponent.TOOLCOUNT]; + private Button[] buttons = new Button[10]; - private Button[] buttons = new Button[ToolBarComponent.TOOLCOUNT]; - - private Image[] images = new Image[ToolBarComponent.TOOLCOUNT]; + // TODO: dynamisch aus der aktuellen toolbar generieren + //private Image[] images = new Image[ToolBarComponent.TOOLCOUNT]; + private Image[] images = new Image[10]; private Brush buttonBackgroud; @@ -28,8 +25,7 @@ internal class ToolbarControl : Panel public Label activeToolLabel; - public ToolbarControl(ScreenComponent screenManager) - : base(screenManager) + public ToolbarControl(ScreenComponent screenManager) : base(screenManager) { Player = screenManager.Player; toolTextures = new Dictionary(); @@ -54,7 +50,9 @@ public ToolbarControl(ScreenComponent screenManager) grid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Auto, Height = 1 }); grid.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Fixed, Height = 50 }); - for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + // TODO: dynamisch aus der aktuellen toolbar generieren + //for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + for (int i = 0; i < 10; i++) { grid.Columns.Add(new ColumnDefinition() { ResizeMode = ResizeMode.Fixed, Width = 50 }); } @@ -64,9 +62,13 @@ public ToolbarControl(ScreenComponent screenManager) activeToolLabel.HorizontalAlignment = HorizontalAlignment.Center; activeToolLabel.Background = new BorderBrush(Color.Black * 0.3f); activeToolLabel.TextColor = Color.White; - grid.AddControl(activeToolLabel, 0, 0, ToolBarComponent.TOOLCOUNT); + // TODO: dynamisch aus der aktuellen toolbar generieren + //grid.AddControl(activeToolLabel, 0, 0, ToolBarComponent.TOOLCOUNT); + // TODO: bezieht sich die rowspan auf die anzahl der tools ? + grid.AddControl(activeToolLabel, 0, 0, 10); - for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + //for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + for (int i = 0; i < 10; i++) { buttons[i] = new Button(screenManager) { @@ -92,32 +94,36 @@ protected override void OnUpdate(GameTime gameTime) if (Player.CurrentEntity == null) return; - // Aktualisierung des aktiven Buttons - for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) - { - if (Player.Toolbar.Tools != null && - Player.Toolbar.Tools.Length > i && - Player.Toolbar.Tools[i] != null && - Player.Toolbar.Tools[i].Definition != null) - { - images[i].Texture = toolTextures[Player.Toolbar.Tools[i].Definition.GetType().FullName]; - - if (Player.Toolbar.ActiveTool == Player.Toolbar.Tools[i]) - buttons[i].Background = activeBackground; - else - buttons[i].Background = buttonBackgroud; - } - else - { - images[i].Texture = null; - buttons[i].Background = buttonBackgroud; - } - } - + //TODO: wieder hinzufügen, oder anders lösen + // Aktualisierung des aktiven Buttons + // TODO: dynamisch aus der aktuellen toolbar generieren + //for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + //for (int i = 0; i < 10; i++) + //{ + // if (Player.Toolbar.Tools != null && + // Player.Toolbar.Tools.Length > i && + // Player.Toolbar.Tools[i] != null && + // Player.Toolbar.Tools[i].Definition != null) + // { + // images[i].Texture = toolTextures[Player.Toolbar.Tools[i].Definition.GetType().FullName]; + + // if (Player.Toolbar.ActiveTool == Player.Toolbar.Tools[i]) + // buttons[i].Background = activeBackground; + // else + // buttons[i].Background = buttonBackgroud; + // } + // else + // { + // images[i].Texture = null; + // buttons[i].Background = buttonBackgroud; + // } + //} + + //TODO: wieder hinzufügen, oder anders lösen // Aktualisierung des ActiveTool Labels - activeToolLabel.Text = Player.Toolbar.ActiveTool != null ? - string.Format("{0} ({1})", Player.Toolbar.ActiveTool.Definition.Name, Player.Toolbar.ActiveTool.Amount) : - string.Empty; + //activeToolLabel.Text = Player.Toolbar.ActiveTool != null ? + // string.Format("{0} ({1})", Player.Toolbar.ActiveTool.Definition.Name, Player.Toolbar.ActiveTool.Amount) : + // string.Empty; activeToolLabel.Visible = !(activeToolLabel.Text == string.Empty); diff --git a/OctoAwesome/OctoAwesome.Client/Screens/GameScreen.cs b/OctoAwesome/OctoAwesome.Client/Screens/GameScreen.cs index 11d7998a..077c7ba8 100644 --- a/OctoAwesome/OctoAwesome.Client/Screens/GameScreen.cs +++ b/OctoAwesome/OctoAwesome.Client/Screens/GameScreen.cs @@ -4,6 +4,7 @@ using System; using engenious; using engenious.Input; +using engenious.Helper; namespace OctoAwesome.Client.Screens { @@ -47,6 +48,7 @@ public GameScreen(ScreenComponent manager) : base(manager) compass.Height = 50; Controls.Add(compass); + //TODO: dynamische einbindugn aus extensions toolbar = new ToolbarControl(manager); toolbar.HorizontalAlignment = HorizontalAlignment.Stretch; toolbar.VerticalAlignment = VerticalAlignment.Bottom; @@ -85,15 +87,20 @@ public GameScreen(ScreenComponent manager) : base(manager) protected override void OnUpdate(GameTime gameTime) { - if (pressedMoveUp) Manager.Player.MoveInput += new Vector2(0f, 1f); - if (pressedMoveLeft) Manager.Player.MoveInput += new Vector2(-1f, 0f); - if (pressedMoveDown) Manager.Player.MoveInput += new Vector2(0f, -1f); - if (pressedMoveRight) Manager.Player.MoveInput += new Vector2(1f, 0f); - if (pressedHeadUp) Manager.Player.HeadInput += new Vector2(0f, 1f); - if (pressedHeadDown) Manager.Player.HeadInput += new Vector2(0f, -1f); - if (pressedHeadLeft) Manager.Player.HeadInput += new Vector2(-1f, 0f); - if (pressedHeadRight) Manager.Player.HeadInput += new Vector2(1f, 0f); - + Vector2 move = Vector2.Zero; + if (pressedMoveUp) move += new Vector2(0f, 1f); + if (pressedMoveLeft) move += new Vector2(-1f, 0f); + if (pressedMoveDown) move += new Vector2(0f, -1f); + if (pressedMoveRight) move += new Vector2(1f, 0f); + Manager.Player.MoveValue = move; + + Vector2 head = Vector2.Zero; + if (pressedHeadUp) head += new Vector2(0f, 1f); + if (pressedHeadDown) head += new Vector2(0f, -1f); + if (pressedHeadLeft) head += new Vector2(-1f, 0f); + if (pressedHeadRight) head += new Vector2(1f, 0f); + Manager.Player.HeadValue = head; + HandleGamePad(); base.OnUpdate(gameTime); @@ -105,7 +112,7 @@ protected override void OnLeftMouseDown(MouseEventArgs args) { if (!IsActiveScreen) return; - Manager.Player.ApplyInput = true; + Manager.Player.ApplyInput.Set(true); args.Handled = true; } @@ -113,7 +120,7 @@ protected override void OnRightMouseDown(MouseEventArgs args) { if (!IsActiveScreen) return; - Manager.Player.InteractInput = true; + Manager.Player.InteractInput.Set(true); args.Handled = true; } @@ -123,7 +130,7 @@ protected override void OnMouseMove(MouseEventArgs args) if (args.MouseMode == MouseMode.Captured && IsActiveScreen) { - Manager.Player.HeadInput = args.GlobalPosition.ToVector2() * mouseSpeed * new Vector2(1f, -1f); + Manager.Player.HeadValue = args.GlobalPosition.ToVector2() * mouseSpeed * new Vector2(1f, -1f); args.Handled = true; } } @@ -203,22 +210,22 @@ private void RegisterKeyActions() Manager.Game.KeyMapper.AddAction("octoawesome:interact", type => { if (!IsActiveScreen || type != KeyMapper.KeyType.Down) return; - Manager.Player.InteractInput = true; + Manager.Player.InteractInput.Set(true); }); Manager.Game.KeyMapper.AddAction("octoawesome:apply", type => { if (!IsActiveScreen || type != KeyMapper.KeyType.Down) return; - Manager.Player.ApplyInput = true; + Manager.Player.ApplyInput.Set(true); }); - Manager.Game.KeyMapper.AddAction("octoawesome:flymode", type => + Manager.Game.KeyMapper.AddAction("octoawesome:jump", type => { if (!IsActiveScreen || type != KeyMapper.KeyType.Down) return; - Manager.Player.FlymodeInput = true; + Manager.Player.JumpInput.Set(true); }); - Manager.Game.KeyMapper.AddAction("octoawesome:jump", type => + Manager.Game.KeyMapper.AddAction("octoawesome:flymode", type => { if (!IsActiveScreen || type != KeyMapper.KeyType.Down) return; - Manager.Player.JumpInput = true; + Manager.Player.FlymodeInput = true; }); for (int i = 0; i < 10; i++) { @@ -269,10 +276,12 @@ private void RegisterKeyActions() Manager.Game.KeyMapper.AddAction("octoawesome:teleport", type => { if (!IsActiveScreen || type != KeyMapper.KeyType.Down) return; - Manager.NavigateToScreen(new TargetScreen(Manager, (x, y) => { - Manager.Game.Player.Position.Position = new Coordinate(0, new Index3(x, y, 300), new Vector3()); - Manager.NavigateBack(); - }, Manager.Game.Player.Position.Position.GlobalBlockIndex.X, Manager.Game.Player.Position.Position.GlobalBlockIndex.Y)); + Manager.NavigateToScreen(new TargetScreen(Manager, (x, y) => + { + Manager.Game.Player.CurrentEntity.SetPosition(new Coordinate(0, new Index3(x, y, 300), new Vector3())); + Manager.NavigateBack(); + }, + Manager.Game.Player.CurrentEntity.Position.GlobalBlockIndex.X, Manager.Game.Player.CurrentEntity.Position.GlobalBlockIndex.Y)); }); } @@ -303,19 +312,19 @@ private void HandleGamePad() if (succeeded) { - Manager.Player.MoveInput += gamePadState.ThumbSticks.Left; - Manager.Player.HeadInput += gamePadState.ThumbSticks.Right; + Manager.Player.MoveValue += gamePadState.ThumbSticks.Left; + Manager.Player.HeadValue += gamePadState.ThumbSticks.Right; if (gamePadState.Buttons.X == ButtonState.Pressed && !pressedGamepadInteract) - Manager.Player.InteractInput = true; + Manager.Player.InteractInput.Set(true); pressedGamepadInteract = gamePadState.Buttons.X == ButtonState.Pressed; if (gamePadState.Buttons.A == ButtonState.Pressed && !pressedGamepadApply) - Manager.Player.ApplyInput = true; + Manager.Player.ApplyInput.Set(true); pressedGamepadApply = gamePadState.Buttons.A == ButtonState.Pressed; if (gamePadState.Buttons.Y == ButtonState.Pressed && !pressedGamepadJump) - Manager.Player.JumpInput = true; + Manager.Player.JumpInput.Set(true); pressedGamepadJump = gamePadState.Buttons.Y == ButtonState.Pressed; if (gamePadState.Buttons.LeftStick == ButtonState.Pressed && !pressedGamepadFlymode) diff --git a/OctoAwesome/OctoAwesome.Client/Screens/InventoryScreen.cs b/OctoAwesome/OctoAwesome.Client/Screens/InventoryScreen.cs index 4d39feef..86a6377d 100644 --- a/OctoAwesome/OctoAwesome.Client/Screens/InventoryScreen.cs +++ b/OctoAwesome/OctoAwesome.Client/Screens/InventoryScreen.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using OctoAwesome.Client.Controls; using engenious; -using OctoAwesome.EntityComponents; namespace OctoAwesome.Client.Screens { @@ -101,13 +100,19 @@ public InventoryScreen(ScreenComponent manager) : base(manager) }; toolbar.Columns.Add(new ColumnDefinition() { ResizeMode = ResizeMode.Parts, Width = 1 }); - for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + + //TODO: aus der componenten später selber ziehen. + //for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + for (int i = 0; i < 10; i++) toolbar.Columns.Add(new ColumnDefinition() { ResizeMode = ResizeMode.Fixed, Width = 50 }); toolbar.Columns.Add(new ColumnDefinition() { ResizeMode = ResizeMode.Parts, Width = 1 }); toolbar.Rows.Add(new RowDefinition() { ResizeMode = ResizeMode.Parts, Height = 1 }); - images = new Image[ToolBarComponent.TOOLCOUNT]; - for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + images = new Image[10]; + //TODO: aus der componenten später selber ziehen. + //images = new Image[ToolBarComponent.TOOLCOUNT]; + //for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + for (int i = 0; i < 10; i++) { Image image = images[i] = new Image(manager) { @@ -119,17 +124,18 @@ public InventoryScreen(ScreenComponent manager) : base(manager) Padding = Border.All(2), }; - image.StartDrag += (e) => - { - InventorySlot slot = player.Toolbar.Tools[(int)image.Tag]; - if (slot != null) - { - e.Handled = true; - e.Icon = toolTextures[slot.Definition.GetType().FullName]; - e.Content = slot; - e.Sender = toolbar; - } - }; + //TODO: wieder hinzufügen, oder anders lösen + //image.StartDrag += (e) => + //{ + // InventorySlot slot = player.Toolbar.Tools[(int)image.Tag]; + // if (slot != null) + // { + // e.Handled = true; + // e.Icon = toolTextures[slot.Definition.GetType().FullName]; + // e.Content = slot; + // e.Sender = toolbar; + // } + //}; image.DropEnter += (e) => { image.Background = hoverBrush; }; image.DropLeave += (e) => { image.Background = backgroundBrush; }; @@ -137,24 +143,25 @@ public InventoryScreen(ScreenComponent manager) : base(manager) { e.Handled = true; - if (e.Sender is Grid) // && ShiftPressed - { - // Swap - int targetIndex = (int)image.Tag; - InventorySlot targetSlot = player.Toolbar.Tools[targetIndex]; - - InventorySlot sourceSlot = e.Content as InventorySlot; - int sourceIndex = player.Toolbar.GetSlotIndex(sourceSlot); - - player.Toolbar.SetTool(sourceSlot, targetIndex); - player.Toolbar.SetTool(targetSlot, sourceIndex); - } - else - { - // Inventory Drop - InventorySlot slot = e.Content as InventorySlot; - player.Toolbar.SetTool(slot, (int)image.Tag); - } + //TODO: wieder hinzufügen, oder anders lösen + //if (e.Sender is Grid) // && ShiftPressed + //{ + // // Swap + // int targetIndex = (int)image.Tag; + // InventorySlot targetSlot = player.Toolbar.Tools[targetIndex]; + + // InventorySlot sourceSlot = e.Content as InventorySlot; + // int sourceIndex = player.Toolbar.GetSlotIndex(sourceSlot); + + // player.Toolbar.SetTool(sourceSlot, targetIndex); + // player.Toolbar.SetTool(targetSlot, sourceIndex); + //} + //else + //{ + // // Inventory Drop + // InventorySlot slot = e.Content as InventorySlot; + // player.Toolbar.SetTool(slot, (int)image.Tag); + //} }; toolbar.AddControl(image, i + 1, 0); @@ -168,22 +175,24 @@ protected override void OnEndDrop(DragEventArgs args) { base.OnEndDrop(args); - if (args.Sender is Grid) - { - InventorySlot slot = args.Content as InventorySlot; - player.Toolbar.RemoveSlot(slot); - } + //TODO: wieder hinzufügen, oder anders lösen + //if (args.Sender is Grid) + //{ + // InventorySlot slot = args.Content as InventorySlot; + // player.Toolbar.RemoveSlot(slot); + //} } protected override void OnKeyDown(KeyEventArgs args) { + //TODO: wieder hinzufügen, oder anders lösen // Tool neu zuweisen - if ((int)args.Key >= (int)Keys.D0 && (int)args.Key <= (int)Keys.D9) - { - int offset = (int)args.Key - (int)Keys.D0; - player.Toolbar.SetTool(inventory.HoveredSlot, offset); - args.Handled = true; - } + //if ((int)args.Key >= (int)Keys.D0 && (int)args.Key <= (int)Keys.D9) + //{ + // int offset = (int)args.Key - (int)Keys.D0; + // player.Toolbar.SetTool(inventory.HoveredSlot, offset); + // args.Handled = true; + //} if (Manager.CanGoBack && (args.Key == Keys.Escape || args.Key == Keys.I)) { @@ -202,20 +211,24 @@ protected override void OnUpdate(GameTime gameTime) massLabel.Text = volumeLabel.Text = inventory.HoveredSlot?.Amount.ToString() ?? ""; // Aktualisierung des aktiven Buttons - for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) - { - if (player.Toolbar.Tools != null && - player.Toolbar.Tools.Length > i && - player.Toolbar.Tools[i] != null && - player.Toolbar.Tools[i].Definition != null) - { - images[i].Texture = toolTextures[player.Toolbar.Tools[i].Definition.GetType().FullName]; - } - else - { - images[i].Texture = null; - } - } + //TODO: aus der componenten später selber ziehen. + //for (int i = 0; i < ToolBarComponent.TOOLCOUNT; i++) + + //TODO: wieder hinzufügen, oder anders lösen + //for (int i = 0; i < 10; i++) + //{ + // if (player.Toolbar.Tools != null && + // player.Toolbar.Tools.Length > i && + // player.Toolbar.Tools[i] != null && + // player.Toolbar.Tools[i].Definition != null) + // { + // images[i].Texture = toolTextures[player.Toolbar.Tools[i].Definition.GetType().FullName]; + // } + // else + // { + // images[i].Texture = null; + // } + //} } protected override void OnNavigatedTo(NavigationEventArgs args) diff --git a/OctoAwesome/OctoAwesome.Network/OctoAwesome.Network.csproj b/OctoAwesome/OctoAwesome.Network/OctoAwesome.Network.csproj index c69b9879..c5fb3861 100644 --- a/OctoAwesome/OctoAwesome.Network/OctoAwesome.Network.csproj +++ b/OctoAwesome/OctoAwesome.Network/OctoAwesome.Network.csproj @@ -32,23 +32,11 @@ 4 - - ..\packages\engenious.0.1.17\lib\net40\engenious.dll - - - ..\packages\engenious.0.1.17\lib\net40\NVorbis.dll - - - ..\packages\engenious.0.1.17\lib\net40\OpenTK.dll - ..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll - - ..\packages\engenious.0.1.17\lib\net40\System.Numerics.Vectors.dll - ..\packages\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll @@ -82,11 +70,4 @@ - - - - Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}". - - - \ No newline at end of file diff --git a/OctoAwesome/OctoAwesome.Network/Settings.cs b/OctoAwesome/OctoAwesome.Network/Settings.cs index 5e8c2c6c..b24c60bc 100644 --- a/OctoAwesome/OctoAwesome.Network/Settings.cs +++ b/OctoAwesome/OctoAwesome.Network/Settings.cs @@ -1,10 +1,6 @@ -using OpenTK; -using System; +using System; using System.Collections.Generic; -using System.Configuration; using System.Linq; -using System.Reflection; -using System.Runtime.Serialization; namespace OctoAwesome.Network { diff --git a/OctoAwesome/OctoAwesome.Network/packages.config b/OctoAwesome/OctoAwesome.Network/packages.config index b28ab903..028cbd1a 100644 --- a/OctoAwesome/OctoAwesome.Network/packages.config +++ b/OctoAwesome/OctoAwesome.Network/packages.config @@ -1,6 +1,5 @@  - \ No newline at end of file diff --git a/OctoAwesome/OctoAwesome.Runtime/ExtensionLoader.cs b/OctoAwesome/OctoAwesome.Runtime/ExtensionLoader.cs index 8412c8f2..5431fb70 100644 --- a/OctoAwesome/OctoAwesome.Runtime/ExtensionLoader.cs +++ b/OctoAwesome/OctoAwesome.Runtime/ExtensionLoader.cs @@ -1,4 +1,5 @@ -using System; +using OctoAwesome.Entities; +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/OctoAwesome/OctoAwesome.Runtime/ResourceManager.cs b/OctoAwesome/OctoAwesome.Runtime/ResourceManager.cs index ae3bd4de..5c335728 100644 --- a/OctoAwesome/OctoAwesome.Runtime/ResourceManager.cs +++ b/OctoAwesome/OctoAwesome.Runtime/ResourceManager.cs @@ -1,4 +1,5 @@ -using System; +using OctoAwesome.Entities; +using System; using System.Collections.Generic; using System.Linq; diff --git a/OctoAwesome/OctoAwesome.Tests/TestPlanet.cs b/OctoAwesome/OctoAwesome.Tests/TestPlanet.cs index 095962ed..3b934548 100644 --- a/OctoAwesome/OctoAwesome.Tests/TestPlanet.cs +++ b/OctoAwesome/OctoAwesome.Tests/TestPlanet.cs @@ -44,6 +44,9 @@ public int Seed public Index3 Size { get; private set; } public Guid Universe { get; private set; } + + public float Gravity => 9.81f; + IMapGenerator IPlanet.Generator { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public void Deserialize(Stream stream) diff --git a/OctoAwesome/OctoAwesome/Chunk.cs b/OctoAwesome/OctoAwesome/Chunk.cs index 549bd35e..92927419 100644 --- a/OctoAwesome/OctoAwesome/Chunk.cs +++ b/OctoAwesome/OctoAwesome/Chunk.cs @@ -94,6 +94,16 @@ public Chunk(Index3 pos, int planet) ChangeCounter = 0; } + /// + /// Liefet den Block an der angegebenen Koordinate zurück. + /// + /// Koordinate des Blocks innerhalb des Chunkgs + /// The block will be removed + /// Die Block-ID an der angegebenen Koordinate + public ushort GetBlock(Index3 index, bool removeblock) + { + return GetBlock(index.X, index.Y, index.Z, removeblock); + } /// /// Liefet den Block an der angegebenen Koordinate zurück. /// @@ -104,6 +114,21 @@ public ushort GetBlock(Index3 index) return GetBlock(index.X, index.Y, index.Z); } + /// + /// Liefet den Block an der angegebenen Koordinate zurück. + /// + /// X-Anteil der Koordinate des Blocks + /// Y-Anteil der Koordinate des Blocks + /// Z-Anteil der Koordinate des Blocks + /// The block will be removed + /// Block-ID der angegebenen Koordinate + public ushort GetBlock(int x, int y, int z, bool removeblock) + { + int flatindex = GetFlatIndex(x, y, z); + ushort block = Blocks[flatindex]; + if (removeblock) SetBlock(flatindex, 0); + return block; + } /// /// Liefet den Block an der angegebenen Koordinate zurück. /// @@ -124,7 +149,7 @@ public ushort GetBlock(int x, int y, int z) /// (Optional) Metainformationen für den Block public void SetBlock(Index3 index, ushort block, int meta = 0) { - SetBlock(index.X, index.Y, index.Z, block); + SetBlock(index.X, index.Y, index.Z, block, meta); } /// @@ -137,13 +162,15 @@ public void SetBlock(Index3 index, ushort block, int meta = 0) /// (Optional) Die Metadaten des Blocks public void SetBlock(int x, int y, int z, ushort block, int meta = 0) { - int index = GetFlatIndex(x, y, z); - Blocks[index] = block; - MetaData[index] = meta; + SetBlock(GetFlatIndex(x, y, z), block, meta); + } + private void SetBlock(int flatindex, ushort block, int meta = 0) + { + Blocks[flatindex] = block; + MetaData[flatindex] = meta; ChangeCounter++; Changed?.Invoke(this, ChangeCounter); } - /// /// Gibt die Metadaten des Blocks an der angegebenen Koordinate zurück. /// diff --git a/OctoAwesome/OctoAwesome/ChunkColumn.cs b/OctoAwesome/OctoAwesome/ChunkColumn.cs index 2159487d..a8d6e1a2 100644 --- a/OctoAwesome/OctoAwesome/ChunkColumn.cs +++ b/OctoAwesome/OctoAwesome/ChunkColumn.cs @@ -1,4 +1,5 @@ -using System; +using OctoAwesome.Entities; +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/OctoAwesome/OctoAwesome/CodeExtensions/Vector2Extension.cs b/OctoAwesome/OctoAwesome/CodeExtensions/Vector2Extension.cs new file mode 100644 index 00000000..7bbec3ee --- /dev/null +++ b/OctoAwesome/OctoAwesome/CodeExtensions/Vector2Extension.cs @@ -0,0 +1,26 @@ +using engenious; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OctoAwesome.CodeExtensions +{ + public static class Vector2Extension + { + /// + /// Checks if the is Zero + /// + /// Vector to Check. + /// + public static bool IsZero(this Vector2 vector) + { + return vector.X > 0 && vector.Y > 0; + } + public static Vector3 ToVector3(this Vector2 vector) + { + return new Vector3(vector.X, vector.Y); + } + } +} diff --git a/OctoAwesome/OctoAwesome/CodeExtensions/Vector3Extension.cs b/OctoAwesome/OctoAwesome/CodeExtensions/Vector3Extension.cs new file mode 100644 index 00000000..bb19625f --- /dev/null +++ b/OctoAwesome/OctoAwesome/CodeExtensions/Vector3Extension.cs @@ -0,0 +1,22 @@ +using engenious; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OctoAwesome.CodeExtensions +{ + public static class Vector3Extension + { + /// + /// Checks if the values greater than zero + /// + /// Vector to check + /// + public static bool IsZero(this Vector3 vector) + { + return vector.X > 0 && vector.Y > 0 && vector.Z > 0; + } + } +} diff --git a/OctoAwesome/OctoAwesome/Component.cs b/OctoAwesome/OctoAwesome/Component.cs index be68aedd..55a99aef 100644 --- a/OctoAwesome/OctoAwesome/Component.cs +++ b/OctoAwesome/OctoAwesome/Component.cs @@ -1,19 +1,22 @@ using System.IO; - namespace OctoAwesome { /// /// Base Class for all Components. /// - public abstract class Component + public abstract class Component : ISerializable { + /// + /// Indicates that the Component is enabled or disabled. + /// public bool Enabled { get; set; } - + /// + /// Constructor + /// public Component() { Enabled = true; } - /// /// Serialisiert die Entität mit dem angegebenen BinaryWriter. /// @@ -23,7 +26,6 @@ public virtual void Serialize(BinaryWriter writer, IDefinitionManager definition { writer.Write(Enabled); } - /// /// Deserialisiert die Entität aus dem angegebenen BinaryReader. /// diff --git a/OctoAwesome/OctoAwesome/ComponentList.cs b/OctoAwesome/OctoAwesome/ComponentList.cs index 40a2108f..bf59fac6 100644 --- a/OctoAwesome/OctoAwesome/ComponentList.cs +++ b/OctoAwesome/OctoAwesome/ComponentList.cs @@ -2,65 +2,84 @@ using System.Collections; using System.Collections.Generic; using System.IO; - namespace OctoAwesome { /// /// Base Class for all Component based Entities. /// - /// Type of Component - public class ComponentList : IEnumerable where T : Component + /// of Component + public class ComponentList : ISerializable, IEnumerable where T : Component { - private Action insertValidator; - private Action removeValidator; - private Action onInserter; + private Action onAdder; private Action onRemover; - - private Dictionary components = new Dictionary(); - + private readonly Dictionary components = new Dictionary(); + /// + /// Return an of the . + /// + /// of the + /// public T this[Type type] { get { if (components.TryGetValue(type, out T result)) return result; - return null; } } - + /// + /// Default Constructor of . + /// public ComponentList() { } - - public ComponentList(Action insertValidator, Action removeValidator, Action onInserter, Action onRemover) + /// + /// Default Constructor of . + /// + /// On add delegate. + /// On remove delegate. + public ComponentList(Action onAdd, Action onRemove) { - this.insertValidator = insertValidator; - this.removeValidator = removeValidator; - this.onInserter = onInserter; - this.onRemover = onRemover; + onAdder = onAdd; + onRemover = onRemove; } - + /// + /// of the List. + /// + /// public IEnumerator GetEnumerator() => components.Values.GetEnumerator(); - + /// + /// of the List. + /// + /// IEnumerator IEnumerable.GetEnumerator() => components.Values.GetEnumerator(); - /// - /// Adds a new Component to the List. + /// Adds a new to the List. /// /// Component - public void AddComponent(V component) - where V : T + public void AddComponent(V component) where V : T { AddComponent(component, false); } - - - public void AddComponent(V component, bool replace) - where V : T + /// + /// Adds a new to the List and replace Items with the same Key. + /// + /// + /// Component + /// Replace i already inclueded. + public void AddComponent(V component, bool replace) where V : T + { + AddComponent(typeof(V), component, replace); + } + /// + /// Adds a new to the List and replace Items with the same Key. + /// + /// Type of Component. + /// Type. + /// Component. + /// Replace i already inclueded. + public void AddComponent(Type type, V component, bool replace) where V : T { - Type type = component.GetType(); - if (components.ContainsKey(type)) { if (replace) @@ -72,37 +91,74 @@ public void AddComponent(V component, bool replace) return; } } - - insertValidator?.Invoke(component); + onAdder?.Invoke(component); components.Add(type, component); - onInserter?.Invoke(component); } - /// - /// + /// Checks if the is included. /// - /// + /// of the . /// - public bool ContainsComponent() + public bool ContainsComponent() where V : T { return components.ContainsKey(typeof(V)); } - /// - /// Returns the Component of the given Type or null + /// Checks if the is included. + /// + /// of the . + /// + public bool ContainsComponent(Type type) + { + return components.ContainsKey(type); + } + /// + /// Returns the of the given Type or null /// /// Component Type /// Component public V GetComponent() where V : T { - if (components.TryGetValue(typeof(V), out T result)) - return (V)result; - + return GetComponent(typeof(V)); + } + /// + /// Returns the of the given Type or null + /// + /// Component Type + /// Type. + /// + public V GetComponent(Type type) where V : T + { + if (components.TryGetValue(type, out T result)) + return (V) result; return null; } - /// - /// Removes the Component of the given Type. + /// Try to get a . + /// + /// of + /// + /// + public bool TryGetComponent(out V component) where V : T + { + return TryGetComponent(typeof(V), out component); + } + /// + /// Try to get a . + /// + /// of + /// Type + /// Component + /// + public bool TryGetComponent(Type type, out V component) where V : T + { + T comp; + components.TryGetValue(type, out comp); + component = comp as V; + return component != null; + } + /// + /// Removes the of the given Type. /// /// Component Type /// @@ -111,17 +167,9 @@ public bool RemoveComponent() where V : T T component; if (!components.TryGetValue(typeof(V), out component)) return false; - - removeValidator?.Invoke(component); - if (components.Remove(typeof(V))) - { - onRemover?.Invoke(component); - return true; - } - - return false; + else onRemover?.Invoke(component); + return components.Remove(typeof(V)); } - /// /// Serialisiert die Entität mit dem angegebenen BinaryWriter. /// @@ -154,7 +202,6 @@ public virtual void Serialize(BinaryWriter writer, IDefinitionManager definition } } } - /// /// Deserialisiert die Entität aus dem angegebenen BinaryReader. /// diff --git a/OctoAwesome/OctoAwesome/Entities/Entity.cs b/OctoAwesome/OctoAwesome/Entities/Entity.cs new file mode 100644 index 00000000..ad10cc04 --- /dev/null +++ b/OctoAwesome/OctoAwesome/Entities/Entity.cs @@ -0,0 +1,153 @@ +using engenious; +using engenious.Helper; +using System; +using System.IO; +namespace OctoAwesome.Entities +{ + /// + /// Basisklasse für alle selbständigen Wesen + /// + public abstract class Entity : ISerializable + { + /// + /// Indicates that the need an Update. + /// + public bool NeedUpdate { get; } + /// + /// Horizontaler winkel. + /// + public float Azimuth { get; private set; } + /// + /// of the . + /// + public Coordinate Position { get; private set; } + /// + /// Contains all Components. + /// + public ComponentList Components { get; private set; } + /// + /// Temp Id + /// + public int Id { get; internal set; } + ///// + ///// Reference to the active Simulation. + ///// + //public Simulation Simulation { get; internal set; } + /// + /// LocalChunkCache für die Entity + /// + public ILocalChunkCache Cache { get; protected set; } + /// + /// Entity die regelmäßig eine Updateevent bekommt + /// + /// Indicates that need an Update. + public Entity(bool needUpdate) + { + NeedUpdate = needUpdate; + Components = new ComponentList(OnAddComponent, OnRemoveComponent); + } + /// + /// Updatemethod for the + /// + /// Time of the Simulation. + public virtual void Update(GameTime gameTime) + { + + } + // TODO: remove this method again + public void RegisterDefault() + { + + } + /// + /// Move the + /// + /// Value of position change + /// Horizontal angle + public void Move(Vector3 moved, float azimuth = 0) + { + Coordinate position = Position + moved; + position.NormalizeChunkIndexXY(Cache.Planet.Size); + SetPosition(position, azimuth); + } + /// + /// Set the Postion of the + /// + /// The new Position + /// Ignore SetCenter call on localcache + /// Horizontal angle + public void SetPosition(Coordinate position, float azimuth = 0, bool ignorecenter = false) + { + if (ignorecenter || Cache != null && Cache.SetCenter(Cache.Planet, new Index2(Position.ChunkIndex))) + { + OnSetPosition(position); + Position = position; + } + } + /// + /// Called during SetPosition (before Position is set) + /// + /// New Position + protected virtual void OnSetPosition(Coordinate position) + { + + } + /// + /// Initialize the Entity. + /// + /// + public void Initialize(IResourceManager mananger) + { + OnInitialize(mananger); + } + /// + /// Called during initialization. + /// + /// + protected virtual void OnInitialize(IResourceManager manager) + { + } + /// + /// Serialisiert die Entität mit dem angegebenen BinaryWriter. + /// + /// Der BinaryWriter, mit dem geschrieben wird. + /// Der aktuell verwendete . + public virtual void Serialize(BinaryWriter writer, IDefinitionManager definitionManager) + { + Components.Serialize(writer, definitionManager); + writer.Write(Position.Planet); + writer.Write(Position.GlobalBlockIndex.X); + writer.Write(Position.GlobalBlockIndex.Y); + writer.Write(Position.GlobalBlockIndex.Z); + writer.Write(Position.BlockPosition.X); + writer.Write(Position.BlockPosition.Y); + writer.Write(Position.BlockPosition.Z); + writer.Write(Position.ChunkIndex.X); + writer.Write(Position.ChunkIndex.Y); + writer.Write(Position.ChunkIndex.Z); + } + /// + /// Deserialisiert die Entität aus dem angegebenen BinaryReader. + /// + /// Der BinaryWriter, mit dem gelesen wird. + /// Der aktuell verwendete . + public virtual void Deserialize(BinaryReader reader, IDefinitionManager definitionManager) + { + Components.Deserialize(reader, definitionManager); + Position = new Coordinate(reader.ReadInt32(), + new Index3(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32()), + new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle())); + } + private void OnRemoveComponent(EntityComponent component) + { + //if (Simulation != null) + // throw new NotSupportedException("Can't remove components during simulation"); + + } + private void OnAddComponent(EntityComponent component) + { + //if (Simulation != null) + // throw new NotSupportedException("Can't add components during simulation"); + } + } +} diff --git a/OctoAwesome/OctoAwesome/Entities/EntityComponent.cs b/OctoAwesome/OctoAwesome/Entities/EntityComponent.cs new file mode 100644 index 00000000..04b962b0 --- /dev/null +++ b/OctoAwesome/OctoAwesome/Entities/EntityComponent.cs @@ -0,0 +1,30 @@ +using System; + +namespace OctoAwesome.Entities +{ + /// + /// Base Class for all Entity Components. + /// + public abstract class EntityComponent : Component + { + /// + /// Constructor of + /// + public EntityComponent() + { + } + // TODO: validiate -> braucht man das wirklich ? :D + //public void SetEntity(Entity entity) + //{ + // if (Entity != null) + // throw new NotSupportedException("Can not change the Entity"); + + // Entity = entity; + // OnSetEntity(); + //} + //protected virtual void OnSetEntity() + //{ + + //} + } +} diff --git a/OctoAwesome/OctoAwesome/EntityFilterAttribute.cs b/OctoAwesome/OctoAwesome/Entities/EntityFilterAttribute.cs similarity index 85% rename from OctoAwesome/OctoAwesome/EntityFilterAttribute.cs rename to OctoAwesome/OctoAwesome/Entities/EntityFilterAttribute.cs index a3738ec4..64ad6b03 100644 --- a/OctoAwesome/OctoAwesome/EntityFilterAttribute.cs +++ b/OctoAwesome/OctoAwesome/Entities/EntityFilterAttribute.cs @@ -1,10 +1,11 @@ -using System; +using engenious.Graphics; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace OctoAwesome +namespace OctoAwesome.Entities { [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public sealed class EntityFilterAttribute : Attribute diff --git a/OctoAwesome/OctoAwesome/Entities/EntityList.cs b/OctoAwesome/OctoAwesome/Entities/EntityList.cs new file mode 100644 index 00000000..9d4f3a89 --- /dev/null +++ b/OctoAwesome/OctoAwesome/Entities/EntityList.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace OctoAwesome.Entities +{ + /// + /// List of Entities. + /// + public class EntityList : IEntityList + { + private List entities; + private IChunkColumn column; + /// + /// Constructor for + /// + /// ChucnkColumn + public EntityList(IChunkColumn column) + { + entities = new List(); + this.column = column; + } + /// + /// Count of Entities. + /// + public int Count => entities.Count; + /// + /// Indicates that this List is readolny or not. + /// + public bool IsReadOnly => false; + /// + /// Adds an to the List. + /// + /// + public void Add(Entity item) + { + entities.Add(item); + column.ChangeCounter++; + } + /// + /// Clear the List. + /// + public void Clear() => entities.Clear(); + /// + /// Checks if an is included. + /// + /// + /// + public bool Contains(Entity item) => entities.Contains(item); + /// + /// Copy the internal . + /// + /// + /// + public void CopyTo(Entity[] array, int arrayIndex) => entities.CopyTo(array, arrayIndex); + /// + /// Remove an from the List. + /// + /// + /// + public bool Remove(Entity item) + { + column.ChangeCounter++; + return entities.Remove(item); + } + /// + /// + /// + /// + public IEnumerator GetEnumerator() => entities.GetEnumerator(); + /// + /// + /// + /// + IEnumerator IEnumerable.GetEnumerator() => entities.GetEnumerator(); + /// + /// Checks if an leaves a ? + /// + /// + public IEnumerable FailChunkEntity() + { + foreach (var entity in entities) + { + //if (entity.Components.ContainsComponent()) + //{ + // var position = entity.Components.GetComponent(); + // ---> cut and passted from here + //} + if (entity.Position.ChunkIndex.X != column.Index.X || entity.Position.ChunkIndex.Y != column.Index.Y) + { + yield return new FailEntityChunkArgs() + { + Entity = entity, + CurrentChunk = new Index2(column.Index), + CurrentPlanet = column.Planet, + TargetChunk = new Index2(entity.Position.ChunkIndex), + TargetPlanet = entity.Position.Planet, + }; + } + } + } + } +} diff --git a/OctoAwesome/OctoAwesome/Entities/IDrawable.cs b/OctoAwesome/OctoAwesome/Entities/IDrawable.cs new file mode 100644 index 00000000..82d2a6d4 --- /dev/null +++ b/OctoAwesome/OctoAwesome/Entities/IDrawable.cs @@ -0,0 +1,84 @@ +using System; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections.Generic; +using engenious.Graphics; +using engenious; +namespace OctoAwesome.Entities +{ + public interface IGraphicsDevice + { + // GraphicsDevice GraphicsDevice { get; } // ich würde das lieber wegkappseln... + // Nur gekappselte Methoden, weil dadurch instancing ermöglicht wird... + // möglicherweise auf vertex und indexbuffer verzichten und nur über geometrie + texture daten + // wenn aus dem model die vertecies rausgezogen werden kann auch für model LOD behandelt werden. + void Draw(int rotx, int roty, int rotz, VertexBuffer vb, IndexBuffer ib, PrimitiveType primitiveType, int verteciesCount, int primitiveCont); + void Draw(int rotz, VertexBuffer vb, IndexBuffer ib, PrimitiveType primitiveType, int verteciesCount, int primitiveCont); + void Draw(int rotx, int roty, int rotz); + void Draw(int rotz); + Texture2D LoadTexture(string name); + Model LoadModel(string name); + } + public interface IDrawable + { + #region Temporary + string Name { get; } + string ModelName { get; } + string TextureName { get; } + float BaseRotationZ { get; } + #endregion + + float Azimuth { get; } + Vector3 Body { get; } + Coordinate Position { get; } + bool NeedUpdate { get; } + void Initialize(IGraphicsDevice device); + void Draw(IGraphicsDevice graphicsDevice, GameTime gameTime); + } + + public interface IControllable + { + Coordinate Position { get; } + IController Controller { get; } + void Register(IController controller); + void Reset(); + } + + public interface IController + { + float HeadTilt { get; } + float HeadYaw { get; } + Vector2 MoveValue { get; } + Vector2 HeadValue { get; } + Index3? InteractBlock { get; } + Index3? ApplyBlock { get; } + OrientationFlags? ApplySide { get; } + InputTrigger JumpInput { get; } + InputTrigger ApplyInput { get; } + InputTrigger InteractInput { get; } + } + + public interface IInteractable + { + } + + public class InputTrigger + { + public int Setter { get; private set; } + public T Value { get; private set; } + public void Set(T value) + { + Setter++; + Value = value; + } + public void Validate() + { + Setter = 0; + Value = default; + } + } + + + +} diff --git a/OctoAwesome/OctoAwesome/Entities/IEntityDefinition.cs b/OctoAwesome/OctoAwesome/Entities/IEntityDefinition.cs new file mode 100644 index 00000000..5f95b264 --- /dev/null +++ b/OctoAwesome/OctoAwesome/Entities/IEntityDefinition.cs @@ -0,0 +1,35 @@ +using engenious; + +namespace OctoAwesome.Entities +{ + /// + /// defines an + /// + public interface IEntityDefinition : IDefinition + { + /// + /// Name of the + /// + string Name { get; } + /// + /// Modelname of + /// + string ModelName { get; } + /// + /// Texturename of + /// + string TextureName { get; } + /// + /// BaseZRotation of + /// + float BaseRotationZ { get; } + /// + /// Mass of + /// + float Mass { get; } + /// + /// Body of + /// + Vector3 Body { get; } + } +} diff --git a/OctoAwesome/OctoAwesome/Entities/IEntityList.cs b/OctoAwesome/OctoAwesome/Entities/IEntityList.cs new file mode 100644 index 00000000..35af3b21 --- /dev/null +++ b/OctoAwesome/OctoAwesome/Entities/IEntityList.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OctoAwesome.Entities +{ + /// + /// Interface for + /// + public interface IEntityList : ICollection + { + /// + /// Checks if an leaves a ? + /// + /// + IEnumerable FailChunkEntity(); + } + /// + /// . + /// + public class FailEntityChunkArgs + { + /// + /// Position of Chuck. + /// + public Index2 CurrentChunk { get; set; } + /// + /// Chunk Plante. + /// + public int CurrentPlanet { get; set; } + /// + /// Target Chunk + /// + public Index2 TargetChunk { get; set; } + /// + /// Target Planet + /// + public int TargetPlanet { get; set; } + /// + /// Entity + /// + public Entity Entity { get; set; } + } +} diff --git a/OctoAwesome/OctoAwesome/Entity.cs b/OctoAwesome/OctoAwesome/Entity.cs deleted file mode 100644 index 0e9bb08d..00000000 --- a/OctoAwesome/OctoAwesome/Entity.cs +++ /dev/null @@ -1,92 +0,0 @@ -using engenious; -using OctoAwesome.EntityComponents; -using System; -using System.IO; - -namespace OctoAwesome -{ - /// - /// Basisklasse für alle selbständigen Wesen - /// - public abstract class Entity - { - /// - /// Contains all Components. - /// - public ComponentList Components { get; private set; } - - /// - /// Temp Id - /// - public int Id { get; internal set; } - - /// - /// Reference to the active Simulation. - /// - public Simulation Simulation { get; internal set; } - - /// - /// LocalChunkCache für die Entity - /// - public ILocalChunkCache Cache { get; protected set; } - - /// - /// Entity die regelmäßig eine Updateevent bekommt - /// - public Entity() - { - Components = new ComponentList( - ValidateAddComponent, ValidateRemoveComponent,OnAddComponent,OnRemoveComponent); - } - - private void OnRemoveComponent(EntityComponent component) - { - - } - - private void OnAddComponent(EntityComponent component) - => component.SetEntity(this); - - private void ValidateAddComponent(EntityComponent component) - { - if (Simulation != null) - throw new NotSupportedException("Can't add components during simulation"); - } - - private void ValidateRemoveComponent(EntityComponent component) - { - if (Simulation != null) - throw new NotSupportedException("Can't remove components during simulation"); - } - - public void Initialize(IResourceManager mananger) - { - OnInitialize(mananger); - } - - protected virtual void OnInitialize(IResourceManager manager) - { - } - - /// - /// Serialisiert die Entität mit dem angegebenen BinaryWriter. - /// - /// Der BinaryWriter, mit dem geschrieben wird. - /// Der aktuell verwendete . - public virtual void Serialize(BinaryWriter writer, IDefinitionManager definitionManager) - => Components.Serialize(writer, definitionManager); - - /// - /// Deserialisiert die Entität aus dem angegebenen BinaryReader. - /// - /// Der BinaryWriter, mit dem gelesen wird. - /// Der aktuell verwendete . - public virtual void Deserialize(BinaryReader reader, IDefinitionManager definitionManager) - => Components.Deserialize(reader, definitionManager); - - public virtual void RegisterDefault() - { - - } - } -} diff --git a/OctoAwesome/OctoAwesome/EntityComponent.cs b/OctoAwesome/OctoAwesome/EntityComponent.cs deleted file mode 100644 index fe0ae575..00000000 --- a/OctoAwesome/OctoAwesome/EntityComponent.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; - -namespace OctoAwesome -{ - /// - /// Base Class for all Entity Components. - /// - public abstract class EntityComponent : Component - { - /// - /// Reference to the Entity. - /// - public Entity Entity { get; private set; } - - public void SetEntity(Entity entity) - { - if (Entity != null) - throw new NotSupportedException("Can not change the Entity"); - - Entity = entity; - OnSetEntity(); - } - - protected virtual void OnSetEntity() - { - - } - } -} diff --git a/OctoAwesome/OctoAwesome/EntityList.cs b/OctoAwesome/OctoAwesome/EntityList.cs deleted file mode 100644 index d53f86ec..00000000 --- a/OctoAwesome/OctoAwesome/EntityList.cs +++ /dev/null @@ -1,71 +0,0 @@ -using OctoAwesome.EntityComponents; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OctoAwesome -{ - public class EntityList : IEntityList - { - private List entities; - private IChunkColumn column; - - public EntityList(IChunkColumn column) - { - entities = new List(); - this.column = column; - } - - public int Count => entities.Count; - - public bool IsReadOnly => false; - - public void Add(Entity item) - { - entities.Add(item); - column.ChangeCounter++; - } - - public void Clear() => entities.Clear(); - - public bool Contains(Entity item) => entities.Contains(item); - - public void CopyTo(Entity[] array, int arrayIndex) => entities.CopyTo(array, arrayIndex); - - public IEnumerator GetEnumerator() => entities.GetEnumerator(); - - public bool Remove(Entity item) - { - column.ChangeCounter++; - return entities.Remove(item); - } - - IEnumerator IEnumerable.GetEnumerator() => entities.GetEnumerator(); - - public IEnumerable FailChunkEntity() - { - foreach (var entity in entities) - { - if (entity.Components.ContainsComponent()) - { - var position = entity.Components.GetComponent(); - - if (position.Position.ChunkIndex.X != column.Index.X || position.Position.ChunkIndex.Y != column.Index.Y) - { - yield return new FailEntityChunkArgs() - { - Entity = entity, - CurrentChunk = new Index2(column.Index), - CurrentPlanet = column.Planet, - TargetChunk = new Index2(position.Position.ChunkIndex), - TargetPlanet = position.Position.Planet, - }; - } - } - } - } - } -} diff --git a/OctoAwesome/OctoAwesome/Extension.cs b/OctoAwesome/OctoAwesome/Extension.cs deleted file mode 100644 index 293d8d10..00000000 --- a/OctoAwesome/OctoAwesome/Extension.cs +++ /dev/null @@ -1,30 +0,0 @@ -using engenious; -using OctoAwesome.EntityComponents; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OctoAwesome -{ - //TODO:Eventuell auslagern - - public sealed class Extension : IExtension - { - public string Description => "OctoAwesome"; - - public string Name => "OctoAwesome"; - - public void Register(IExtensionLoader extensionLoader) - { - extensionLoader.RegisterEntityExtender((p) => - { - p.Components.AddComponent(new ControllableComponent()); - p.Components.AddComponent(new HeadComponent() { Offset = new Vector3(0, 0, 3.2f) }); - p.Components.AddComponent(new InventoryComponent()); - p.Components.AddComponent(new ToolBarComponent()); - }); - } - } -} diff --git a/OctoAwesome/OctoAwesome/GlobalChunkCache.cs b/OctoAwesome/OctoAwesome/GlobalChunkCache.cs index d94b6979..e25ed816 100644 --- a/OctoAwesome/OctoAwesome/GlobalChunkCache.cs +++ b/OctoAwesome/OctoAwesome/GlobalChunkCache.cs @@ -267,8 +267,7 @@ private void BackgroundCleanup() /// /// ID des Planeten /// Planet - public IPlanet GetPlanet(int id) - => loadPlanetDelagte(id); + public IPlanet GetPlanet(int id) => loadPlanetDelagte(id); public void BeforeSimulationUpdate(Simulation simulation) { diff --git a/OctoAwesome/OctoAwesome/IChunk.cs b/OctoAwesome/OctoAwesome/IChunk.cs index 02655859..1eea2ef8 100644 --- a/OctoAwesome/OctoAwesome/IChunk.cs +++ b/OctoAwesome/OctoAwesome/IChunk.cs @@ -40,6 +40,24 @@ public interface IChunk /// int ChangeCounter { get; set; } + /// + /// Liefet den Block an der angegebenen Koordinate zurück. + /// + /// Koordinate des Blocks innerhalb des Chunkgs + /// Entfernt den Block. + /// Die Block-ID an der angegebenen Koordinate + ushort GetBlock(Index3 index, bool removeblock); + + /// + /// Liefet den Block an der angegebenen Koordinate zurück. + /// + /// X-Anteil der Koordinate des Blocks + /// Y-Anteil der Koordinate des Blocks + /// Z-Anteil der Koordinate des Blocks + /// Entfernt den Block. + /// Block-ID der angegebenen Koordinate + ushort GetBlock(int x, int y, int z, bool removeblock); + /// /// Liefet den Block an der angegebenen Koordinate zurück. /// diff --git a/OctoAwesome/OctoAwesome/IChunkColumn.cs b/OctoAwesome/OctoAwesome/IChunkColumn.cs index c8200159..b9bf7574 100644 --- a/OctoAwesome/OctoAwesome/IChunkColumn.cs +++ b/OctoAwesome/OctoAwesome/IChunkColumn.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System; using System.IO; - +using OctoAwesome.Entities; namespace OctoAwesome { /// @@ -24,6 +24,9 @@ public interface IChunkColumn /// Index2 Index { get; } + /// + /// Gibt die anzahl der Änderungen an die an dieser Instance vorgenommen wurden. + /// int ChangeCounter { get; set; } /// @@ -57,6 +60,9 @@ public interface IChunkColumn /// Block-ID der angegebenen Koordinate ushort GetBlock(int x, int y, int z); + /// + /// Wird ausgelöst wenn sich die IChunkColumn ändert. + /// event Action Changed; /// @@ -95,6 +101,8 @@ public interface IChunkColumn /// (Optional) Metainformationen für den Block void SetBlockMeta(int x, int y, int z, int meta); + // TODO: überlegen ob der Block die Ressourcen wissen muss. + // Die Ressourcen könnten auch erst ermittelt werden wenn der Block verarbeitet wurde. /// /// Liefert alle Ressourcen im Block an der angegebenen Koordinate zurück. /// @@ -104,6 +112,8 @@ public interface IChunkColumn /// Ein Array aller Ressourcen des Blocks ushort[] GetBlockResources(int x, int y, int z); + // TODO: überlegen ob der Block die Ressourcen wissen muss. + // Die Ressourcen könnten auch erst ermittelt werden wenn der Block verarbeitet wurde. /// /// Ändert die Ressourcen des Blocks an der angegebenen Koordinate /// diff --git a/OctoAwesome/OctoAwesome/IEntityList.cs b/OctoAwesome/OctoAwesome/IEntityList.cs deleted file mode 100644 index 9f0aecc9..00000000 --- a/OctoAwesome/OctoAwesome/IEntityList.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OctoAwesome -{ - public interface IEntityList : ICollection - { - IEnumerable FailChunkEntity(); - } - - public class FailEntityChunkArgs - { - public Index2 CurrentChunk { get; set; } - public int CurrentPlanet { get; set; } - - public Index2 TargetChunk { get; set; } - public int TargetPlanet { get; set; } - - public Entity Entity { get; set; } - } -} diff --git a/OctoAwesome/OctoAwesome/IExtensionLoader.cs b/OctoAwesome/OctoAwesome/IExtensionLoader.cs index bb965a88..76a97c49 100644 --- a/OctoAwesome/OctoAwesome/IExtensionLoader.cs +++ b/OctoAwesome/OctoAwesome/IExtensionLoader.cs @@ -1,4 +1,5 @@ -using System; +using OctoAwesome.Entities; +using System; using System.Collections.Generic; namespace OctoAwesome diff --git a/OctoAwesome/OctoAwesome/IExtensionResolver.cs b/OctoAwesome/OctoAwesome/IExtensionResolver.cs index a0c90663..29da7324 100644 --- a/OctoAwesome/OctoAwesome/IExtensionResolver.cs +++ b/OctoAwesome/OctoAwesome/IExtensionResolver.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using OctoAwesome.Entities; +using System.Collections.Generic; namespace OctoAwesome { diff --git a/OctoAwesome/OctoAwesome/ILocalChunkCache.cs b/OctoAwesome/OctoAwesome/ILocalChunkCache.cs index 18344426..6c934270 100644 --- a/OctoAwesome/OctoAwesome/ILocalChunkCache.cs +++ b/OctoAwesome/OctoAwesome/ILocalChunkCache.cs @@ -36,7 +36,7 @@ public interface ILocalChunkCache /// Instanz des Chunks IChunk GetChunk(int x, int y, int z); - IPlanet LoadPlanet(int id); + IPlanet GetPlanet(int id); /// /// Setzt den Zentrums-Chunk für diesen lokalen Cache. @@ -51,6 +51,24 @@ public interface ILocalChunkCache /// void Flush(); + /// + /// Liefert den Block an der angegebenen Block-Koodinate zurück. + /// + /// Block Index + /// Die Block-ID an der angegebenen Koordinate + /// Entfernt den Block. + ushort GetBlock(Index3 index, bool removeblock); + + /// + /// Liefert den Block an der angegebenen Block-Koodinate zurück. + /// + /// X-Anteil der Koordinate des Blocks innerhalb des Chunks + /// Y-Anteil der Koordinate des Blocks innerhalb des Chunks + /// Z-Anteil der Koordinate des Blocks innerhalb des Chunks + /// Entfernt den Block. + /// Die Block-ID an der angegebenen Koordinate + ushort GetBlock(int x, int y, int z, bool removeblock); + /// /// Liefert den Block an der angegebenen Block-Koodinate zurück. /// @@ -73,7 +91,8 @@ public interface ILocalChunkCache /// /// Block-Koordinate /// Die neue Block-ID. - void SetBlock(Index3 index, ushort block); + /// Die neuen Blockmetadatan + void SetBlock(Index3 index, ushort block, int meta = 0); /// /// Überschreibt den Block an der angegebenen Koordinate. @@ -82,7 +101,8 @@ public interface ILocalChunkCache /// Y-Anteil der Koordinate des Blocks innerhalb des Chunks /// Z-Anteil der Koordinate des Blocks innerhalb des Chunks /// Die neue Block-ID. - void SetBlock(int x, int y, int z, ushort block); + /// Die neuen Blockmetadatan + void SetBlock(int x, int y, int z, ushort block, int meta = 0); /// /// Gibt die Metadaten des Blocks an der angegebenen Koordinate zurück. diff --git a/OctoAwesome/OctoAwesome/IMapPopulator.cs b/OctoAwesome/OctoAwesome/IMapPopulator.cs index 7e2ce119..f9d952f6 100644 --- a/OctoAwesome/OctoAwesome/IMapPopulator.cs +++ b/OctoAwesome/OctoAwesome/IMapPopulator.cs @@ -10,6 +10,7 @@ public interface IMapPopulator /// int Order { get; } + //TODO: Kommentieren xD /// /// Versieht einen Chunk mit Items /// diff --git a/OctoAwesome/OctoAwesome/IPlanet.cs b/OctoAwesome/OctoAwesome/IPlanet.cs index 46eef0ed..5be2c3d8 100644 --- a/OctoAwesome/OctoAwesome/IPlanet.cs +++ b/OctoAwesome/OctoAwesome/IPlanet.cs @@ -28,6 +28,11 @@ public interface IPlanet /// Index3 Size { get; } + /// + /// Gravitation des Planeten. + /// + float Gravity { get; } + /// /// Die Klimakarte des Planeten /// diff --git a/OctoAwesome/OctoAwesome/IResourceManager.cs b/OctoAwesome/OctoAwesome/IResourceManager.cs index 58148040..4bb0b7ac 100644 --- a/OctoAwesome/OctoAwesome/IResourceManager.cs +++ b/OctoAwesome/OctoAwesome/IResourceManager.cs @@ -1,3 +1,4 @@ +using OctoAwesome.Entities; using System; namespace OctoAwesome diff --git a/OctoAwesome/OctoAwesome/ISerializable.cs b/OctoAwesome/OctoAwesome/ISerializable.cs new file mode 100644 index 00000000..dc3f8924 --- /dev/null +++ b/OctoAwesome/OctoAwesome/ISerializable.cs @@ -0,0 +1,22 @@ +using System.IO; +namespace OctoAwesome +{ + /// + /// Interface for Serializable classes. + /// + public interface ISerializable + { + /// + /// Serialisiert die Entität mit dem angegebenen BinaryWriter. + /// + /// Der BinaryWriter, mit dem geschrieben wird. + /// Der aktuell verwendete . + void Serialize(BinaryWriter writer, IDefinitionManager definitionManager); + /// + /// Deserialisiert die Entität aus dem angegebenen BinaryReader. + /// + /// Der BinaryWriter, mit dem gelesen wird. + /// Der aktuell verwendete . + void Deserialize(BinaryReader reader, IDefinitionManager definitionManager); + } +} diff --git a/OctoAwesome/OctoAwesome/LocalChunkCache.cs b/OctoAwesome/OctoAwesome/LocalChunkCache.cs index 432a0a87..596c618a 100644 --- a/OctoAwesome/OctoAwesome/LocalChunkCache.cs +++ b/OctoAwesome/OctoAwesome/LocalChunkCache.cs @@ -240,6 +240,14 @@ public IChunk GetChunk(int x, int y, int z) return null; } + /// + /// Liefert den Block an der angegebenen Block-Koodinate zurück. + /// + /// Block Index + /// The block will be removed + /// Die Block-ID an der angegebenen Koordinate + public ushort GetBlock(Index3 index, bool removeblock) + => GetBlock(index.X, index.Y, index.Z, removeblock); /// /// Liefert den Block an der angegebenen Block-Koodinate zurück. /// @@ -247,7 +255,19 @@ public IChunk GetChunk(int x, int y, int z) /// Die Block-ID an der angegebenen Koordinate public ushort GetBlock(Index3 index) => GetBlock(index.X, index.Y, index.Z); - + /// + /// Liefert den Block an der angegebenen Block-Koodinate zurück. + /// + /// X-Anteil der Koordinate des Blocks + /// Y-Anteil der Koordinate des Blocks + /// Z-Anteil der Koordinate des Blocks + /// The block will be removed + /// Die Block-ID an der angegebenen Koordinate + public ushort GetBlock(int x, int y, int z, bool removeblock) + { + IChunk chunk = GetChunk(x >> Chunk.LimitX, y >> Chunk.LimitY, z >> Chunk.LimitZ); + return chunk?.GetBlock(x, y, z, removeblock) ?? 0; + } /// /// Liefert den Block an der angegebenen Block-Koodinate zurück. /// @@ -258,11 +278,7 @@ public ushort GetBlock(Index3 index) public ushort GetBlock(int x, int y, int z) { IChunk chunk = GetChunk(x >> Chunk.LimitX, y >> Chunk.LimitY, z >> Chunk.LimitZ); - - if (chunk != null) - return chunk.GetBlock(x, y, z); - - return 0; + return chunk?.GetBlock(x, y, z) ?? 0; } /// @@ -270,8 +286,9 @@ public ushort GetBlock(int x, int y, int z) /// /// Block-Koordinate /// Die neue Block-ID. - public void SetBlock(Index3 index, ushort block) - => SetBlock(index.X, index.Y, index.Z, block); + /// Die neuen Metadatan + public void SetBlock(Index3 index, ushort block, int meta = 0) + => SetBlock(index.X, index.Y, index.Z, block, meta); /// /// Überschreibt den Block an der angegebenen Koordinate. @@ -280,12 +297,11 @@ public void SetBlock(Index3 index, ushort block) /// Y-Anteil der Koordinate des Blocks innerhalb des Chunks /// Z-Anteil der Koordinate des Blocks innerhalb des Chunks /// Die neue Block-ID - public void SetBlock(int x, int y, int z, ushort block) + /// Die neuen Metadatan + public void SetBlock(int x, int y, int z, ushort block, int meta = 0) { IChunk chunk = GetChunk(x >> Chunk.LimitX, y >> Chunk.LimitY, z >> Chunk.LimitZ); - - if (chunk != null) - chunk.SetBlock(x, y, z, block); + chunk?.SetBlock(x, y, z, block, meta); } /// @@ -359,10 +375,13 @@ public void Flush() /// Die X-Koordinate /// Die Y-Koordinate /// Der Abgeflachte index - private int FlatIndex(int x, int y) - => (((y & (mask)) << limit) | ((x & (mask)))); + private int FlatIndex(int x, int y) => (((y & (mask)) << limit) | ((x & (mask)))); - public IPlanet LoadPlanet(int id) - => globalCache.GetPlanet(id); + /// + /// Get the from . + /// + /// Id of the planet. + /// + public IPlanet GetPlanet(int id) => globalCache.GetPlanet(id); } } diff --git a/OctoAwesome/OctoAwesome/MapPopulator.cs b/OctoAwesome/OctoAwesome/MapPopulator.cs index dca4131f..f2129d14 100644 --- a/OctoAwesome/OctoAwesome/MapPopulator.cs +++ b/OctoAwesome/OctoAwesome/MapPopulator.cs @@ -10,6 +10,10 @@ public abstract class MapPopulator : IMapPopulator /// public int Order { get; protected set; } + // TODO: initialisieren? -> bessere skalierung der population + // enviromente parameter können besser einbezogen werden (plante ist zu kalt für lebewesen etc...) + //public abstract void Initialize(IPlanet plante, IDefinitionManager definitions); + /// /// Versieht einen Chunk mit Items /// diff --git a/OctoAwesome/OctoAwesome/OctoAwesome.csproj b/OctoAwesome/OctoAwesome/OctoAwesome.csproj index 30f630e0..8f822f61 100644 --- a/OctoAwesome/OctoAwesome/OctoAwesome.csproj +++ b/OctoAwesome/OctoAwesome/OctoAwesome.csproj @@ -43,13 +43,21 @@ false - - False - ..\libs\engenious.dll + + ..\packages\engenious.0.1.11\lib\net40\engenious.dll + + + ..\packages\engenious.0.1.11\lib\net40\NVorbis.dll + + + ..\packages\engenious.0.1.11\lib\net40\OpenTK.dll + + ..\packages\engenious.0.1.11\lib\net40\System.Numerics.Vectors.dll + @@ -64,26 +72,22 @@ + + - - - - - - - - - - - + + + + + - + @@ -102,7 +106,8 @@ - + + @@ -121,9 +126,18 @@ - + + + + + + + Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}". + + +