From 2de1a409fb0b1337cc9a85e3c2312651e4b534b0 Mon Sep 17 00:00:00 2001 From: Mootfrost777 Date: Thu, 17 Aug 2023 13:38:38 +0300 Subject: [PATCH] Fix Entity spawn position --- DangerousD/GameCore/GameObjects/Entity.cs | 1 + DangerousD/GameCore/GameObjects/LivingEntity.cs | 6 +++++- DangerousD/GameCore/Managers/GameManager.cs | 2 +- DangerousD/GameCore/Managers/MapManager.cs | 11 +++++++---- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/DangerousD/GameCore/GameObjects/Entity.cs b/DangerousD/GameCore/GameObjects/Entity.cs index 9388d31..8876e2c 100644 --- a/DangerousD/GameCore/GameObjects/Entity.cs +++ b/DangerousD/GameCore/GameObjects/Entity.cs @@ -9,6 +9,7 @@ namespace DangerousD.GameCore.GameObjects public abstract class Entity : GameObject { public Entity(Vector2 position) : base(position) {} + public virtual void SetPosition(Vector2 position) { _pos = position; } } } diff --git a/DangerousD/GameCore/GameObjects/LivingEntity.cs b/DangerousD/GameCore/GameObjects/LivingEntity.cs index 5c18831..8341080 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntity.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntity.cs @@ -11,7 +11,11 @@ public abstract class LivingEntity : Entity { acceleration = new Vector2(0, 30); } - public void SetPosition(Vector2 position) { targetPosition = position; _pos = position; } //TODO befrend targetpos and physics engine + public override void SetPosition(Vector2 position) + { + targetPosition = position; _pos = position; + + } //TODO befrend targetpos and physics engine public override void Update(GameTime gameTime) { diff --git a/DangerousD/GameCore/Managers/GameManager.cs b/DangerousD/GameCore/Managers/GameManager.cs index a83e0e8..82cf63d 100644 --- a/DangerousD/GameCore/Managers/GameManager.cs +++ b/DangerousD/GameCore/Managers/GameManager.cs @@ -30,7 +30,7 @@ namespace DangerousD.GameCore mapObjects = new List(); entities = new List(); players = new List(); - mapManager = new MapManager(1); + mapManager = new MapManager(2); physicsManager = new PhysicsManager(); } diff --git a/DangerousD/GameCore/Managers/MapManager.cs b/DangerousD/GameCore/Managers/MapManager.cs index 5a1e770..2c8a451 100644 --- a/DangerousD/GameCore/Managers/MapManager.cs +++ b/DangerousD/GameCore/Managers/MapManager.cs @@ -63,8 +63,8 @@ namespace DangerousD.GameCore.Managers { if (tiles[i] != 0) { - Vector2 pos = new((chunkX+ i % chunkW) * tileSize.X * _scale + offsetX, - (chunkY + i / chunkW) * tileSize.Y * _scale + offsetY); + Vector2 pos = new(((chunkX+ i % chunkW) * tileSize.X + offsetX) * _scale, + ((chunkY + i / chunkW) * tileSize.Y + offsetY) * _scale); //pos *= _scale; Rectangle sourceRect = new(new Point((tiles[i] -1) % _columns, (tiles[i] -1) / _columns) * tileSize.ToPoint(), tileSize.ToPoint()); Type type = Type.GetType($"DangerousD.GameCore.GameObjects.MapObjects.{tileType}"); @@ -100,11 +100,14 @@ namespace DangerousD.GameCore.Managers private void InstantiateEntities(XmlNode group) { string entityType = group.Attributes["class"].Value; + float offsetX = group.Attributes["offsetx"] is not null ? float.Parse(group.Attributes["offsetx"].Value) : 0; + float offsetY = group.Attributes["offsety"] is not null ? float.Parse(group.Attributes["offsety"].Value) : 0; foreach (XmlNode entity in group.ChildNodes) { Type type = Type.GetType($"DangerousD.GameCore.GameObjects.{entityType}"); - Activator.CreateInstance(type, new Vector2(float.Parse(entity.Attributes["x"].Value), float.Parse(entity.Attributes["y"].Value))); - } + Entity inst = (Entity)Activator.CreateInstance(type, new Vector2(float.Parse(entity.Attributes["x"].Value) + offsetX, float.Parse(entity.Attributes["y"].Value) + offsetY) * _scale); + inst.SetPosition(new Vector2(inst.Pos.X, inst.Pos.Y - inst.Height)); + } } } }