Added Granade

Currently damages enemies but based on client side -> need fixing + HOW TO UPDATE POSITION? Need to rethink granade generation anyway
This commit is contained in:
SergoDobro 2024-08-26 17:50:51 +03:00
parent 7426a68c8e
commit 685011fd61
6 changed files with 126 additions and 22 deletions

View file

@ -229,7 +229,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
}
return players.ToArray();
}
public Entity[] GetEntities(Rectangle rectangle, Entity entity)
public Entity[] GetEntities(Rectangle rectangle, Entity entityToIgnore = null)
{
List<Entity> entities = new List<Entity>();
@ -240,8 +240,11 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
entities.Add(item);
}
}
if (entities.Contains(entity))
entities.Remove(entity);
if (entityToIgnore!= null)
{
if (entities.Contains(entityToIgnore))
entities.Remove(entityToIgnore);
}
return entities.ToArray();
}
@ -257,7 +260,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
public static SerializableVector2 Serialize(this Vector2 vector) => new SerializableVector2(vector);
public static Vector2 RandomVector()
{
return new Vector2((float)Random.Shared.NextDouble() - 0.5f, (float)Random.Shared.NextDouble() - 0.5f);
return new Vector2((float)Random.Shared.NextDouble() - 0.5f, (float)Random.Shared.NextDouble() - 0.5f)*2;
}
}

View file

@ -32,10 +32,10 @@ namespace ZoFo.GameCore.GameObjects
public virtual void Update()
{
}
public override void UpdateLogic()
public override void UpdateLogic_OnServer()
{
Update();
base.UpdateLogic();
base.UpdateLogic_OnServer();
}
public void StartAnimation(string animationId)

View file

@ -270,13 +270,12 @@ public class Player : LivingEntity
{
for (int i = 3; i <= 3; i++)
{
Instantiate(new Explosion(
Instantiate(new Granade(
((position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * (3 - i) / 3f) +
((entity.position - graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2) * i / 3f) + ExtentionClass.RandomVector() * 3
));
}
(entity as Enemy).TakeDamage(1);
}
}
}

View file

@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
@ -24,41 +25,143 @@ namespace ZoFo.GameCore.GameObjects
public Explosion(Vector2 position) : base(position)
{
if (AppManager.Instance.client != null)//remove
{
AppManager.Instance.client.AddShaking(0.05f);
}
graphicsComponent.ObjectDrawRectangle = new Rectangle(-30, -30, 60, 60).SetOrigin(position);
AppManager.Instance.SoundManager.StartSound("gun-gunshot-01", Vector2.Zero, Vector2.Zero, 0.5f, (float)(Random.Shared.NextDouble() * 2 - 1));
(graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += _ => {
(graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += _ =>
{
Delete_OnClient(this);
};
}
}
public class Granade : Particle
/// <summary>
/// TODO: change from particle to throwable, it is not a particle anymore
/// </summary>
public class Granade : GameObject
{
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "explosion_1" }, "explosion_1");
public Granade(Vector2 positionTo, Vector2? positionFrom = null) : base(positionTo)
/// <summary>
/// TODO updates with directed effect
/// </summary>
/// <param name="positionTo"></param>
/// <param name="positionFrom"></param>
public Granade(Vector2 positionTo) : base(positionTo)
{
if (AppManager.Instance.client.myPlayer != null)
this.positionFrom = AppManager.Instance.client.myPlayer.position;
this.positionTo = positionTo;
graphicsComponent.ObjectDrawRectangle = new Rectangle(-30, -30, 60, 60).SetOrigin(position);
AppManager.Instance.SoundManager.StartSound("gun-gunshot-01", Vector2.Zero, Vector2.Zero, 0.5f, (float)(Random.Shared.NextDouble() * 2 - 1));
(graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += _ => {
(graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += _ =>
{
Delete_OnClient(this);
//Delete_OnClient(this);
};
}
Vector2 positionFrom;
Vector2 positionTo;
float dt = 0;
public override void UpdateLogic_OnServer()
{
if (dt >= 1)
{
FlightEndedOnServer();
return;
}
float m = Math.Min(positionFrom.Y, positionTo.Y) - 40;
position.X = (1 - dt) * positionFrom.X + dt * positionTo.X;
position.Y = (2 * positionTo.Y + 2 * positionFrom.Y - 4 * m) * dt * dt +
(4 * m - positionTo.Y - 3 * positionFrom.Y) * dt + positionFrom.Y;
dt += 0.05f;
base.UpdateLogic_OnServer();
}
private void FlightEndedOnServer()
{
var rect = GetDamageRectangle();
var entities = (AppManager.Instance.server.collisionManager.GetEntities(rect).ToList());
foreach (var item in entities)
{
if (item is Enemy)
{
(item as Enemy).TakeDamage(1);
}
}
}
public override void Update_OnClient()
{
if (dt >= 1)
{
//Granade Finished the flight
Instantiate_OnClient(new Explosion(position + ExtentionClass.RandomVector()*10));
Delete_OnClient(this);
base.Update_OnClient();
for (int i = 0; i < 10; i++)
{
if (Random.Shared.NextDouble() < 0.1) continue;
float angl = i / 10f * (float)Math.PI * 2;
Instantiate_OnClient(new Explosion(position +
new Vector2((float)Math.Cos(angl), (float)Math.Sin(angl)
) * 30));
}
var rect = GetDamageRectangle();
var entities = (AppManager.Instance.server.collisionManager.GetEntities(rect).ToList());
foreach (var item in entities)
{
if (item is Enemy)
{
(item as Enemy).TakeDamage(1);
}
}
return;
}
float m = Math.Min(positionFrom.Y, positionTo.Y)-40;
position.X = (1 - dt) * positionFrom.X + dt * positionTo.X;
position.Y = (1 - dt) * positionFrom.X + dt * positionTo.X;
position.Y = (2 * positionTo.Y + 2 * positionFrom.Y - 4 * m) * dt * dt +
(4 * m - positionTo.Y - 3 * positionFrom.Y) * dt + positionFrom.Y;
dt += 0.05f;
//position =
base.Update_OnClient();
//base.Update_OnClient();
graphicsComponent.ObjectDrawRectangle.X = (int)position.X; //Move To place where Updates Sets your position
graphicsComponent.ObjectDrawRectangle.Y = (int)position.Y;
PlayAnimation_OnClient();
}
public override void Draw(SpriteBatch spriteBatch)
{
DrawDebugRectangle(spriteBatch, GetDamageRectangle(), Color.Red);
base.Draw(spriteBatch);
}
public Rectangle GetDamageRectangle()
{
var rect = graphicsComponent.ObjectDrawRectangle;
int size = 10;
rect.X -= size;
rect.Y -= size;
rect.Width += 2 * size;
rect.Height += 2 * size;
return rect;
}
}
}

View file

@ -26,7 +26,7 @@ public abstract class GameObject
positionDraw = position;
}
public virtual void UpdateLogic()
public virtual void UpdateLogic_OnServer()
{
PlayAnimation_OnServer();
@ -130,7 +130,7 @@ public abstract class GameObject
}
public void DrawDebugRectangle(SpriteBatch spriteBatch, Rectangle _rectangle, Nullable<Color> color = null)
{
return;
//return; TODO normal disable and enable via debug modder
if (color is null) color = new Color(1, 0, 0, 0.1f);
if (color.Value.A == 255) color = new Color(color.Value, 0.25f);
spriteBatch.Draw(debugTexture,
@ -139,7 +139,6 @@ public abstract class GameObject
_rectangle.Width * GraphicsComponent.scaling,
_rectangle.Height * GraphicsComponent.scaling), color.Value);
//TODO: debugTexture
}
#endregion
}

View file

@ -170,7 +170,7 @@ namespace ZoFo.GameCore
{
for (int i = 0; i < gameObjects.Count; i++)
{
gameObjects[i].UpdateLogic();
gameObjects[i].UpdateLogic_OnServer();
}
collisionManager.ResolvePhysics();
ticks = 0;