Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projectile based bullets #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions code/swb_base/BulletBase.Projectile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using SWB.Shared;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace SWB.Base;

public class ProjectileBasedBullet : IBulletBase
{

public async void Shoot(Weapon weapon, ShootInfo shootInfo, Vector3 spreadOffset)
{
if (!weapon.IsValid) return;

var player = weapon.Owner;
if (player is null) return;


var forward = player.EyeAngles.Forward + spreadOffset;
//disable aimcone
//var forward = player.EyeAngles.Forward;
forward = forward.Normal;

var startPos = player.EyePos;
var velocity = forward * shootInfo.BulletSpeed;
//var gravity = new Vector3(0, 0, -0.6f); // Simulated gravity
var gravity = new Vector3(0, 0, -50f);
// Simulate the bullet over time
float timeStep = 0.016f; // Use smaller time steps for smoother movement (equivalent to ~60 FPS)
var currentPos = startPos;
var distanceLeft = shootInfo.BulletRange;

// Create the projectile object once
GameObject projectileObject = SceneUtility.GetPrefabScene(ResourceLibrary.Get<PrefabFile>("bullet.prefab"));
//Log.Info(projectileObject.Name);

var projectile = projectileObject.Clone();
projectile.WorldPosition = currentPos;

// Set the projectile's rotation to match the player's facing direction
projectile.WorldRotation = Rotation.LookAt(forward);

// Iterate through the bullet's path over time
while (distanceLeft > 0)
{
var nextPos = currentPos + velocity * timeStep;
var bulletTr = weapon.TraceBullet(currentPos, nextPos);

if (bulletTr.Hit)
{
var hitObj = bulletTr.GameObject;

// Impact
weapon.CreateBulletImpact(bulletTr);

// Apply damage if we hit a valid target (like a player)
if (!weapon.IsProxy && hitObj is not null && hitObj.Tags.Has(TagsHelper.Player))
{
var target = hitObj.Components.GetInAncestorsOrSelf<IPlayerBase>();
if (target?.IsAlive == true)
{
var hitTags = bulletTr.Hitbox?.Tags.TryGetAll().ToArray() ?? Array.Empty<string>();

var dmgInfo = Shared.DamageInfo.FromBullet(
weapon.Owner.Id,
weapon.ClassName,
shootInfo.Damage,
bulletTr.HitPosition,
velocity * shootInfo.Force, // Force based on velocity
hitTags
);

target?.TakeDamage(dmgInfo);
}
}
// Apply damage to bots that implement IDamageable
else if (hitObj is not null && hitObj.Tags.Has(TagsHelper.Bot))
{
var botTarget = hitObj.Components.Get<IDamageable>();

if (botTarget != null && botTarget.IsAlive)
{
var hitTags = bulletTr.Hitbox?.Tags.TryGetAll().ToArray() ?? Array.Empty<string>();

var dmgInfo = Shared.DamageInfo.FromBullet(
weapon.Owner.Id,
weapon.ClassName,
shootInfo.Damage,
bulletTr.HitPosition,
velocity * shootInfo.Force,
hitTags
);

botTarget.TakeDamage(dmgInfo); // Apply damage to the bot
}
}


projectile.Destroy(); // Destroy the projectile upon impact
return; // Stop once we hit something
}

// Update position and apply gravity
currentPos = nextPos;
velocity += gravity * timeStep;
distanceLeft -= velocity.Length * timeStep;

// Move the projectile smoothly
projectile.WorldPosition = currentPos;

// Wait for the next frame (to simulate real-time movement)
await Task.Delay((int)(timeStep * 1000)); // Convert timeStep (in seconds) to milliseconds
}

// Destroy the projectile after it has traveled the full distance
projectile.Destroy();
}


public Vector3 GetRandomSpread(float spread)
{
return (Vector3.Random + Vector3.Random + Vector3.Random + Vector3.Random) * spread * 0.25f;
}

void TracerEffects(Weapon weapon, ShootInfo shootInfo, Vector3 currentPos, Vector3 endPos)
{


}


}
4 changes: 4 additions & 0 deletions code/swb_base/structures/ShootInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public class ShootInfo : Component

/// <summary>Weapon firing type</summary>
[Property, Group( "Bullets" )] public FiringType FiringType { get; set; } = FiringType.semi;

[Property, Group( "Bullets" )] public float BulletSpeed { get; set; } = 4000f;

[Property, Group( "Bullets" )] public float BulletRange { get; set; } = 30000f;

// Animations //

Expand Down