3 Commits

Author SHA1 Message Date
mccarreon
bbf0a765d5 added nametags 2023-08-09 17:50:57 -07:00
mccarreon
bce1f8e1f7 got racers animated and racing 2023-08-06 21:12:28 -07:00
mccarreon
3e9d64cb00 making racers 2023-08-06 18:41:00 -07:00
9 changed files with 229 additions and 24 deletions

View File

@@ -6,7 +6,7 @@ namespace LuckerGame.Components.Lucker.Cameras;
public abstract partial class AbstractCamera : EntityComponent<Entities.Lucker>, ISingletonComponent public abstract partial class AbstractCamera : EntityComponent<Entities.Lucker>, ISingletonComponent
{ {
public virtual bool ShouldShowCursor => false; public virtual bool ShouldShowCursor => false;
protected Vector3 CameraPosition { get; set; } public Vector3 CameraPosition { get; set; }
protected Rotation CameraRotation { get; set; } protected Rotation CameraRotation { get; set; }
protected float FieldOfView { get; set; } protected float FieldOfView { get; set; }
protected IEntity FirstPersonViewer { get; set; } protected IEntity FirstPersonViewer { get; set; }

View File

@@ -15,13 +15,13 @@ public class RussianRouletteMinigame : Minigame
{ {
public override string Name => "Russian Roulette"; public override string Name => "Russian Roulette";
private List<Lucker> Players { get; set; } private List<Lucker> Players { get; set; }
private Pawn Shooter { get; set; } private Entities.Pawn Shooter { get; set; }
private const float ShooterDistance = 80f; private const float ShooterDistance = 80f;
private const float TimeBetweenShots = 7f; private const float TimeBetweenShots = 7f;
private int Taunted = 0; private int Taunted = 0;
private List<Pawn> DeadVictims => Players private List<Entities.Pawn> DeadVictims => Players
.Select( player => player.Pawn as Pawn ) .Select( player => player.Pawn as Entities.Pawn )
.Where( pawn => !pawn.IsValid || pawn.LifeState != LifeState.Alive ) .Where( pawn => !pawn.IsValid || pawn.LifeState != LifeState.Alive )
.ToList(); .ToList();
@@ -30,7 +30,7 @@ public class RussianRouletteMinigame : Minigame
public override void Initialize( List<Lucker> players ) public override void Initialize( List<Lucker> players )
{ {
Players = players; Players = players;
Shooter = new Pawn(); Shooter = new Entities.Pawn();
var shooterInventory = Shooter.Components.Create<PawnInventory>(); var shooterInventory = Shooter.Components.Create<PawnInventory>();
shooterInventory.AddWeapon( new RussianPistol() ); shooterInventory.AddWeapon( new RussianPistol() );
@@ -45,7 +45,7 @@ public class RussianRouletteMinigame : Minigame
{ {
var player = pair.Player; var player = pair.Player;
var index = pair.Index; var index = pair.Index;
var pawn = new Pawn(); var pawn = new Entities.Pawn();
pawn.Name = player.Name; pawn.Name = player.Name;
pawn.Tags.Add( "victim" ); pawn.Tags.Add( "victim" );
pawn.Health = 1; pawn.Health = 1;
@@ -83,7 +83,7 @@ public class RussianRouletteMinigame : Minigame
} }
else if ( TimeSinceShot > TimeBetweenShots * .8f && Taunted == 1) else if ( TimeSinceShot > TimeBetweenShots * .8f && Taunted == 1)
{ {
var victim = Players.Select( player => player.Pawn as Pawn ) var victim = Players.Select( player => player.Pawn as Racer )
.OrderBy( _ => Guid.NewGuid() ) .OrderBy( _ => Guid.NewGuid() )
.FirstOrDefault(); .FirstOrDefault();
Shooter.LookAt( victim.Position ); Shooter.LookAt( victim.Position );

View File

@@ -0,0 +1,23 @@
using LuckerGame.UI;
using Sandbox;
public class HoveringTextCreator : Entity
{
private string Text { get; set; }
private Entity TargetEntity { get; set; }
public HoveringTextCreator()
{
}
public HoveringTextCreator(string text, Entity targetEntity)
{
Text = text;
TargetEntity = targetEntity;
}
public override void ClientSpawn()
{
base.ClientSpawn();
var hoveringText = new HoveringText(Text, TargetEntity);
}
}

View File

@@ -0,0 +1,64 @@
using LuckerGame.Entities;
using LuckerGame.UI;
using Sandbox;
using System;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection.Metadata.Ecma335;
public class Racer : Pawn
{
public float Speed;
private float SpeedModifier = 1f;
private Random Random { get; set; }
private HoveringText NameTag { get; set; }
public Racer()
{
}
public Racer( Random random )
{
Random = random;
}
public override void ClientSpawn()
{
base.ClientSpawn();
NameTag = new( Name, this );
}
public void ContinueRacing()
{
SetAnimParameter( "move_x", Speed * 500f * SpeedModifier );
ModifySpeed();
Position = Position.WithY( Position.y - (Speed * SpeedModifier) );
}
private void ModifySpeed()
{
var roll = Random.NextDouble();
switch ( roll )
{
case >= .90 and < .95:
SpeedModifier = 2f;
break;
case >= .95:
SpeedModifier = 0.5f;
break;
}
}
public void StopRacing()
{
SetAnimParameter( "move_x", 0 );
}
public void GenerateSpeed( double minSpeed, double maxSpeed )
{
Speed = (float)(RandomExtensions.NextDouble( Random, minSpeed, maxSpeed ));
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Reflection.Metadata.Ecma335;
using LuckerGame.Components.Lucker.Cameras; using LuckerGame.Components.Lucker.Cameras;
using LuckerGame.Components.Pawn; using LuckerGame.Components.Pawn;
using LuckerGame.Entities; using LuckerGame.Entities;
@@ -14,39 +15,109 @@ namespace LuckerGame.Minigames.TerryRaces;
public class TerryRaces : Minigame public class TerryRaces : Minigame
{ {
public override string Name => "Terry Races"; public override string Name => "Terry Races";
private Random random = new Random();
private HoveringTextCreator NameTag { get; set; }
private Racer WinningRacer = null;
private FixedCamera Camera;
private List<Lucker> Players { get; set; } private List<Lucker> Players { get; set; }
private FixedCamera camera; private List<Racer> Racers { get; set; } = new List<Racer>();
private List<string> RacerNames = new List<string>
{
"Terrance",
"Terrie",
"TearBear",
"Theresa"
};
private float StartingY = 290f;
private float FinishLineY = -300f;
private float StartingX = 90f;
private float RacerXOffset = 60f;
private float MinimumSpeed = .90f;
private float MaximumSpeed = 1.1f;
public override void Initialize( List<Lucker> players ) public override void Initialize( List<Lucker> players )
{ {
Players = players; Players = players;
// Setup cameras for players // Setup cameras for players
Players.ForEach( player => Players.ForEach( player =>
{ {
camera = player.Components.Create<FixedCamera>(); Camera = player.Components.Create<FixedCamera>();
camera.LookAt( new Vector3( -110f, 4f, 180f ), Rotation.FromPitch( 45 ) ); Camera.LookAt( new Vector3(0f, 0f, 400f), Rotation.FromPitch( -270 ) );
camera.FieldOfViewValue = 120f; Camera.FieldOfViewValue = 100f;
} ); } );
Players.Select( ( player, i ) => (Player: player, Index: i) ).ToList().ForEach( pair => SpawnRacers();
{
var player = pair.Player;
var index = pair.Index;
var pawn = new Pawn();
pawn.Name = player.Name;
pawn.Tags.Add( "victim" );
pawn.Health = 1;
player.Pawn = pawn;
pawn.DressFromClient( player.Client );
} );
} }
public override void Tick() public override void Tick()
{ {
Racers.ForEach( racer =>
{
if (WinningRacer == null)
{
GetWinningRacer();
racer.ContinueRacing();
}
else
{
racer.StopRacing();
}
} );
} }
public override void Cleanup() public override void Cleanup()
{ {
} }
private void SpawnRacers()
{
for ( int i = 0; i < RacerNames.Count; i++ )
{
var clothing = new ClothingContainer();
var racer = new Racer(random);
var x = StartingX - RacerXOffset * i;
clothing.Toggle( GetRandomBottomClothing() );
clothing.Toggle( GetRandomHatClothing() );
clothing.DressEntity( racer );
racer.Name = RacerNames[i];
racer.Position = new Vector3( x, StartingY, 0 );
racer.Rotation = Rotation.FromYaw( -90 );
racer.GenerateSpeed(MinimumSpeed, MaximumSpeed );
Racers.Add( racer );
}
}
private Clothing GetRandomBottomClothing()
{
return ResourceLibrary
.GetAll<Clothing>()
.Where( c => c.Category == Clothing.ClothingCategory.Bottoms )
.OrderBy( _ => Guid.NewGuid() )
.FirstOrDefault();
}
private Clothing GetRandomHatClothing()
{
return ResourceLibrary
.GetAll<Clothing>()
.Where( c => c.Category == Clothing.ClothingCategory.Hat )
.OrderBy( _ => Guid.NewGuid() )
.FirstOrDefault();
}
private void GetWinningRacer()
{
foreach ( Racer racer in Racers )
{
if ( racer.Position.y <= FinishLineY )
{
WinningRacer = racer;
Log.Info( $"Winning racer: {racer.Name}" );
}
}
}
} }

11
code/RandomExtensions.cs Normal file
View File

@@ -0,0 +1,11 @@
using System;
public static class RandomExtensions
{
public static double NextDouble(
this Random random,
double minValue,
double maxValue )
{
return random.NextDouble() * (maxValue - minValue) + minValue;
}
}

View File

@@ -0,0 +1,30 @@
@using Sandbox;
@using Sandbox.UI;
@namespace LuckerGame.UI
@attribute [StyleSheet]
@inherits WorldPanel
<root class="card">
<label class="text">@Text</label>
</root>
@code {
private Entity TargetEntity;
private string Text;
public HoveringText(string text, Entity targetEntity)
{
Text = text;
TargetEntity = targetEntity;
}
[GameEvent.Client.Frame]
private void OnFrame()
{
if (!TargetEntity.IsValid()) return;
Log.Info($"{Text} to {TargetEntity.Position}");
Position = TargetEntity.Position + Vector3.Up * 65f;
Rotation = Rotation.LookAt(-Screen.GetDirection(new Vector2(Screen.Width * 0.5f, Screen.Height * 0.5f)));
}
}

View File

@@ -0,0 +1,7 @@
hoveringtext {
white-space: nowrap;
.text {
font-size: 150px;
color: white;
}
}

View File

@@ -12,7 +12,6 @@
<VoiceList/> <VoiceList/>
<VotingLobby/> <VotingLobby/>
<Scoreboard/> <Scoreboard/>
<CameraCursor/>
</root> </root>
@code @code