Skip to content

Commit

Permalink
Refactoring entities/base, RPG-7 weapon, fixed several bugs, BlastUti…
Browse files Browse the repository at this point in the history
…l explosion helper class, other improvements
  • Loading branch information
timmybo5 committed Jun 26, 2021
1 parent 5439f62 commit a41b425
Show file tree
Hide file tree
Showing 155 changed files with 129,750 additions and 199 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.vmdl
*.vmat
*.sound
*.wav

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@ Anyone can contribute by creating a pull request to their branch. Contributors c

## Planned Updates
* Proper viewbob animations
* More base-included entities (e.g. rpg, bomb)
* More base-included entities (e.g. bomb)
* Physical bullets
* Attachment base

## Current Issues
* While running looking up or down will force weird rotations on the run animation

## Deatchmatch Elements
For now some deathmatch dependencies are included as the base uses the inventory and hud elements from the deathmatch gamemode.

Expand Down
4 changes: 4 additions & 0 deletions code/ExamplePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public override void Respawn()
Inventory.Add( new SWB_CSS.M249() );
Inventory.Add( new SWB_CSS.AWP() );

Inventory.Add( new SWB_CSS.AWP() );

Inventory.Add( new SWB_EXPLOSIVES.RPG7() );

//Inventory.Add( new SWB_CSS.M249HE() );
//Inventory.Add( new SWB_CSS.DeagleDual() );
//Inventory.Add( new SWB_CSS.AK47Dual() );
Expand Down
41 changes: 41 additions & 0 deletions code/swb_base/CarriableBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Sandbox;
using Sandbox.UI;
using System;

namespace SWB_Base
{
/// <summary>
/// An entity that can be carried in the player's inventory and hands.
/// </summary>
public class CarriableBase : BaseCarriable
{
public override void ActiveStart( Entity ent )
{
//base.ActiveStart( ent );

EnableDrawing = true;

if ( ent is Player player )
{
var animator = player.GetActiveAnimator();
if ( animator != null )
{
SimulateAnimator( animator );
}
}

//
// If we're the local player (clientside) create viewmodel
// and any HUD elements that this weapon wants
//
if ( IsLocalPawn )
{
DestroyViewModel();
DestroyHudElements();

CreateViewModel();
CreateHudElements();
}
}
}
}
46 changes: 37 additions & 9 deletions code/swb_base/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ private void AddIdleAnimations( ref CameraSetup camSetup )
// Viewbob animations
private void AddViewbobAnimations( ref CameraSetup camSetup )
{
/* WIP
float speedMod = 1;
if ( weapon.IsZooming )
speedMod = 0.7f;
var realTime = RealTime.Now;
var len = Owner.Velocity.Length;
var maxWalkSpeed = 190;
var mul = Math.Clamp( len / maxWalkSpeed, 0, 1 );
var sin = MathF.Sin( realTime * speedMod ) * mul;
var cos = MathF.Cos( realTime * speedMod ) * mul;
var tan = MathF.Atan2( cos * sin, cos * sin ) * mul;
//var viewbobAngles = Angles.Zero;
//viewbobAngles.pitch = tan;
//viewbobAngles.yaw = cos;
//viewbobAngles.roll = sin;
//Rotation *= Rotation.From( viewbobAngles );
var left = camSetup.Rotation.Left;
var up = camSetup.Rotation.Up;
var forward = camSetup.Rotation.Forward;
Position += left * sin * 0.1f;
Position += up * tan * 0.1f;
Position += forward * tan * 0.4f;
*/

var speed = Owner.Velocity.Length.LerpInverse( 0, 320 );
speed = speed * weapon.WalkAnimationSpeedMod;

Expand All @@ -117,10 +147,10 @@ private void AddViewbobAnimations( ref CameraSetup camSetup )

Position += up * MathF.Sin( walkBob ) * speed * sideSwingMod;
Position += left * MathF.Sin( walkBob * upSwingMod ) * speed * -0.5f;
}
}

// Movement animations
private void AddMovementAnimations( ref CameraSetup camSetup )
// Movement animations
private void AddMovementAnimations( ref CameraSetup camSetup )
{
// Angles
// var xSpeed = Math.Clamp( Owner.Velocity.x * 0.1f, -maxSideSway, maxSideSway ); // Is not predicted yet so looks weird
Expand Down Expand Up @@ -173,16 +203,14 @@ private void LerpToPosition( Angles angles, Vector3 pos, ref Angles lerpTargAngl
{
// Angles
lerpTargAngle = MathZ.FILerp( lerpTargAngle, angles, 10f );
var targAngles = Rotation.Angles() + lerpTargAngle;

Rotation = Rotation.From( targAngles );
Rotation *= Rotation.From( lerpTargAngle );

// Position
lerpTargPos = MathZ.FILerp( lerpTargPos, pos, 10f );

var right = camSetup.Rotation.Right;
var up = camSetup.Rotation.Up;
var forward = camSetup.Rotation.Forward;
var forward = camSetup.Rotation.Forward;

var posOffset = Vector3.Zero;
posOffset += right * lerpTargPos.x;
Expand Down Expand Up @@ -264,8 +292,8 @@ private void AddActionAnimations( ref CameraSetup camSetup )
if ( liveEditing )
{
FieldOfView = weapon.ZoomFOV;
testAngles = new Angles( 0f, 0.5f, 0f );
testPos = new Vector3( -5.5f, 2f, 4f );
testAngles = new Angles( -2.85f, -0.34f, 6f );
testPos = new Vector3( -5.245f, -1.05f, 0f );
}

LerpToPosition( testAngles, testPos, ref lerpRunAngle, ref lerpRunPos, ref camSetup );
Expand Down
93 changes: 20 additions & 73 deletions code/swb_base/WeaponBase.Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,88 +29,35 @@ public override void Spawn()

public virtual void Start()
{
if ( UseGravity )
{
// Initialize physics
MoveType = MoveType.Physics;
PhysicsEnabled = true;
UsePhysicsCollision = true;
SetupPhysicsFromModel( PhysicsMotionType.Dynamic );
PhysicsGroup.AddVelocity( StartVelocity * Speed );

// Delete entity
if ( RemoveDelay > 0 )
_ = DeleteAsync( RemoveDelay );
}
else
{
canThink = true;
}
// Initialize physics
MoveType = MoveType.Physics;
PhysicsEnabled = true;
UsePhysicsCollision = true;
SetupPhysicsFromModel( PhysicsMotionType.Dynamic );
PhysicsGroup.AddVelocity( StartVelocity * Speed );

// Delete entity
if ( RemoveDelay > 0 )
_ = DeleteAsync( RemoveDelay );
}

protected override void OnPhysicsCollision( CollisionEventData eventData )
{
base.OnPhysicsCollision( eventData );
}

public virtual void OnCollision( TraceResult traceResult )
{
if ( RemoveDelay <= 0 )
Delete();
if ( IsSticky && eventData.Entity.IsValid() )
{
Velocity = Vector3.Zero;
Parent = eventData.Entity;
}
}

[Event.Tick.Server]
public virtual void Tick()
{
if ( !canThink || !IsServer || isStuck )
return;

var velocity = Rotation.Forward * Speed;
var start = Position;
var end = start + velocity * Time.Delta;

var tr = Trace.Ray( start, end )
.UseHitboxes()
.Ignore( Owner )
.Ignore( this )
.Size( 1.0f )
.Run();


if ( tr.Hit )
if ( !UseGravity )
{
if ( IsSticky )
isStuck = true;

Position = tr.EndPos + Rotation.Forward * -1;

if ( tr.Entity.IsValid() )
{
var damageInfo = DamageInfo.FromBullet( tr.EndPos, tr.Direction * 200, Damage )
.UsingTraceResult( tr )
.WithAttacker( Owner )
.WithWeapon( this );

tr.Entity.TakeDamage( damageInfo );
}

SetParent( tr.Entity, tr.Bone );
Owner = null;

// Surface impact effect
tr.Normal = Rotation.Forward * -1;
tr.Surface.DoBulletImpact( tr );
velocity = default;

// Delete entity
if ( RemoveDelay > 0 )
_ = DeleteAsync( RemoveDelay );

OnCollision( tr );
}
else
{
Position = end;
PhysicsBody.Velocity = PhysicsBody.Velocity.WithZ( 0 );
}
}
}
Expand All @@ -120,13 +67,13 @@ public partial class WeaponBaseEntity : WeaponBase
public virtual Func<ClipInfo, bool, FiredEntity> CreateEntity => null; // Function that creates an entity and returns it ( to use custom entities in the base )
public virtual string EntityModel => ""; // Path to the model of the entity
public virtual Vector3 EntityVelocity => new Vector3( 0, 0, 100 ); // Velocity ( right, up, forward )
public virtual Vector3 EntitySpawnOffset => Vector3.Zero; // Entity spawn offset ( right, up, forward )
public virtual Angles EntityAngles => new Angles( 0, 0, 0 ); // Spawn angles
public virtual Vector3 EntitySpawnOffset => Vector3.Zero; // Spawn offset ( right, up, forward )
public virtual float PrimaryEntitySpeed => 100f; // Primary velocity speed
public virtual float SecondaryEntitySpeed => 50f; // Secondary velocity Speed
public virtual float RemoveDelay => -1; // Delay that the entity should be removed after
public virtual bool UseGravity => true; // Should gravity affect the entity
public virtual bool IsSticky => false; // Should the entity stick to the surface it hits

public virtual void FireEntity( ClipInfo clipInfo, bool isPrimary )
{
FiredEntity firedEntity;
Expand All @@ -145,7 +92,7 @@ public virtual void FireEntity( ClipInfo clipInfo, bool isPrimary )

firedEntity.Owner = Owner;
firedEntity.Position = MathZ.RelativeAdd( Position, EntitySpawnOffset, Owner.EyeRot );
firedEntity.Rotation = Owner.EyeRot;
firedEntity.Rotation = Owner.EyeRot * Rotation.From( EntityAngles );

firedEntity.RemoveDelay = RemoveDelay;
firedEntity.UseGravity = UseGravity;
Expand Down
96 changes: 0 additions & 96 deletions code/swb_base/WeaponBase.Throwable.cs

This file was deleted.

Loading

0 comments on commit a41b425

Please # to comment.