camera
scaling
This commit is contained in:
SergoDobro 2024-08-19 01:20:28 +03:00
parent 57f130e6f1
commit ec275c7584
9 changed files with 95 additions and 54 deletions

View file

@ -93,6 +93,7 @@ namespace ZoFo.GameCore
#endregion
Player myPlayer;
List<MapObject> mapObjects = new List<MapObject>();
List<GameObject> gameObjects = new List<GameObject>();
List<Player> players = new List<Player>();
@ -111,6 +112,8 @@ namespace ZoFo.GameCore
}
networkManager.SendData();//set to ticks
if (myPlayer!=null)
GraphicsComponent.CameraPosition = (myPlayer.position + myPlayer.graphicsComponent.ObjectDrawRectangle.Size.ToVector2()/2 - AppManager.Instance.CurentScreenResolution.ToVector2()/(2*GraphicsComponent.scaling)).ToPoint();
}
internal void Draw(SpriteBatch spriteBatch)
{
@ -140,16 +143,17 @@ namespace ZoFo.GameCore
(update as UpdateTileCreated).tileSetName
));
}
//else if (update is UpdateStopObjectCreated)
//{
// stopObjects.Add(
// new StopObject(
// (update as UpdateStopObjectCreated).Position,
// (update as UpdateStopObjectCreated).Size.ToVector2(),
// (update as UpdateStopObjectCreated).sourceRectangle,
// (update as UpdateStopObjectCreated).tileSetName
// ));
//}
else if (update is UpdateStopObjectCreated)
{
stopObjects.Add(
new StopObject(
(update as UpdateStopObjectCreated).Position,
(update as UpdateStopObjectCreated).Size.GetPoint().ToVector2(),
(update as UpdateStopObjectCreated).sourceRectangle.GetRectangle(),
(update as UpdateStopObjectCreated).tileSetName,
(update as UpdateStopObjectCreated).collisions.Select(x =>x.GetRectangle()).ToArray()
));
}
else if (update is UpdateGameObjectCreated)
{
GameObject created_gameObject;
@ -159,6 +163,7 @@ namespace ZoFo.GameCore
{
created_gameObject = new Player((update as UpdateGameObjectCreated).position);
players.Add(created_gameObject as Player);
myPlayer = players[0];
gameObjects.Add(created_gameObject);
}
if ((update as UpdateGameObjectCreated).GameObjectType == "Ammo")

View file

@ -211,8 +211,8 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
{
public static Rectangle SetOrigin(this Rectangle rectangle, Vector2 origin)
{
rectangle.X = (int)origin.X;
rectangle.Y = (int)origin.Y;
rectangle.X += (int)origin.X;
rectangle.Y += (int)origin.Y;
return rectangle;
}
}

View file

@ -169,7 +169,31 @@ namespace ZoFo.GameCore.GameManagers
#endregion // Cheats
#region Обработка состояния объекта. Задает значение полю scopeState.
if (keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.W))
if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Up) ||
keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.W))
{
currentScopeState = ScopeState.StraightRight;
InputMovementDirection = new Vector2(1, -1);
}
else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Up) ||
keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.W))
{
currentScopeState = ScopeState.StraightLeft;
InputMovementDirection = new Vector2(-1, -1);
}
else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Down) ||
keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.S))
{
currentScopeState = ScopeState.BackRight;
InputMovementDirection = new Vector2(1, 1);
}
else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Down) ||
keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.S))
{
currentScopeState = ScopeState.BackLeft;
InputMovementDirection = new Vector2(-1, 1);
}
else if (keyBoardState.IsKeyDown(Keys.Up) || keyBoardState.IsKeyDown(Keys.W))
{
currentScopeState = ScopeState.Straight;
InputMovementDirection = new Vector2(0, -1);
@ -189,30 +213,6 @@ namespace ZoFo.GameCore.GameManagers
currentScopeState = ScopeState.Right;
InputMovementDirection = new Vector2(1, 0);
}
else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Up) ||
keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.W))
{
currentScopeState = ScopeState.StraightRight;
InputMovementDirection = new Vector2(1, 1);
}
else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Up) ||
keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.W))
{
currentScopeState = ScopeState.StraightLeft;
InputMovementDirection = new Vector2(-1, 1);
}
else if(keyBoardState.IsKeyDown(Keys.Right) && keyBoardState.IsKeyDown(Keys.Down) ||
keyBoardState.IsKeyDown(Keys.D) && keyBoardState.IsKeyDown(Keys.S))
{
currentScopeState = ScopeState.BackRight;
InputMovementDirection = new Vector2(1, -1);
}
else if(keyBoardState.IsKeyDown(Keys.Left) && keyBoardState.IsKeyDown(Keys.Down) ||
keyBoardState.IsKeyDown(Keys.A) && keyBoardState.IsKeyDown(Keys.S))
{
currentScopeState = ScopeState.BackLeft;
InputMovementDirection = new Vector2(-1, -1);
}
#endregion
#region Обработка нажатия выстрела. Вызывает событие ShootEvent

View file

@ -0,0 +1,22 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
{
internal class UpdateStopObjectCreated : UpdateData
{
public UpdateStopObjectCreated() { UpdateType = "UpdateStopObjectCreated"; }
public Texture2D TextureTile { get; set; }
public Vector2 Position { get; set; }
public SerializablePoint Size { get; set; }
public SerializableRectangle sourceRectangle { get; set; }
public string tileSetName { get; set; }
public SerializableRectangle[] collisions { get; set; }
}
}

View file

@ -11,21 +11,21 @@ namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies
{
class Zombie : Enemy
{
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "zombie_damaged","zombie_walk","zombie_idle","zombie_attack","zombie_death" }, "zombie_walk");
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "zombie_damaged", "zombie_walk", "zombie_idle", "zombie_attack", "zombie_death" }, "zombie_walk");
public Zombie(Vector2 position) : base(position)
{
health = 5;
speed =2;
collisionComponent.stopRectangle = new Rectangle(0, 0, 52, 100);
graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100);
health = 5;
speed = 2;
graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 30, 30);
collisionComponent.stopRectangle = new Rectangle(0, 10, 30, 20);
}
public override void Update()
{
Vector2 duration = Vector2.Normalize(
AppManager.Instance.server.players[0].position - position
);
velocity+=new Vector2(duration.X * speed, duration.Y*speed);
velocity += new Vector2(duration.X * speed, duration.Y * speed);
if (Random.Shared.NextDouble() > 0.9)
{

View file

@ -33,7 +33,7 @@ public class LivingEntity : Entity
public void OnCollision(CollisionComponent component)
{
}
public override void UpdateAnimations()
@ -43,9 +43,9 @@ public class LivingEntity : Entity
Vector2 prevPosition_forClient;
public override void Draw(SpriteBatch spriteBatch)
{
if ((position - prevPosition_forClient).X< 0)
if ((position - prevPosition_forClient).X < 0)
graphicsComponent.Flip = SpriteEffects.FlipHorizontally;
else if((position - prevPosition_forClient).X > 0)
else if ((position - prevPosition_forClient).X > 0)
graphicsComponent.Flip = SpriteEffects.None;
base.Draw(spriteBatch);
prevPosition_forClient = position;

View file

@ -26,8 +26,8 @@ public class Player : LivingEntity
//public bool isTryingToInteract { get; set; }
public Player(Vector2 position) : base(position)
{
graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100);
collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100);
graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 30, 30);
collisionComponent.stopRectangle = new Rectangle(0, 20, 30, 10);
speed = 10;
//isTryingToInteract = false;
//IsTryingToShoot = false;

View file

@ -6,7 +6,7 @@ namespace ZoFo.GameCore.Graphics;
public abstract class GraphicsComponent
{
public Rectangle ObjectDrawRectangle;
public static int scaling = 1;
public static int scaling = 3;
public string mainTextureName;//TODO костыль - пофиксить
public SpriteEffects Flip = SpriteEffects.None;

View file

@ -183,6 +183,22 @@ namespace ZoFo.GameCore
{
gameObjects.Add(gameObject);
if (gameObject is StopObject)
{
AddData(new UpdateStopObjectCreated()
{
Position = (gameObject as StopObject).position,
sourceRectangle = new SerializableRectangle((gameObject as StopObject).sourceRectangle),
Size = new SerializablePoint((gameObject as StopObject).graphicsComponent.ObjectDrawRectangle.Size),
tileSetName = ((gameObject as StopObject).graphicsComponent as StaticGraphicsComponent)._textureName,
collisions = (gameObject as StopObject).collisionComponents.Select(x=>new SerializableRectangle(x.stopRectangle)).ToArray()
});//TODO
foreach (var col in (gameObject as StopObject).collisionComponents)
{
collisionManager.Register(col);
}
return;
}
if (gameObject is MapObject)
{
AddData(new UpdateTileCreated()
@ -191,7 +207,7 @@ namespace ZoFo.GameCore
sourceRectangle = new SerializableRectangle((gameObject as MapObject).sourceRectangle),
Size = new SerializablePoint((gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size),
tileSetName = ((gameObject as MapObject).graphicsComponent as StaticGraphicsComponent)._textureName
});//TODO
});
return;
}
if (gameObject is Entity entity)
@ -207,9 +223,7 @@ namespace ZoFo.GameCore
if (gameObject is Player)
{
players.Add(gameObject as Player);
}
players.Add(gameObject as Player);
////var elems = gameObject.GetType().GetProperties(System.Reflection.BindingFlags.Public);
////if (elems.Count()>0) TODO
////{