Reduce code duplication for particles.

This commit is contained in:
UnknownShadow200 2016-05-11 19:37:31 +10:00
parent 2717458067
commit e8bd5033f3
7 changed files with 74 additions and 85 deletions

View file

@ -190,8 +190,6 @@
<Compile Include="Particles\Particle.cs" />
<Compile Include="Particles\ParticleManager.cs" />
<Compile Include="Particles\ParticleSpawner.cs" />
<Compile Include="Particles\RainParticle.cs" />
<Compile Include="Particles\TerrainParticle.cs" />
<Compile Include="Entities\Player.cs" />
<Compile Include="Game\ChatLog.cs" />
<Compile Include="Game\Game.cs" />

View file

@ -1,6 +1,7 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using ClassicalSharp.Events;
using OpenTK;
namespace ClassicalSharp.Map {
@ -123,6 +124,13 @@ namespace ClassicalSharp.Map {
return p.Y > GetLightHeight( p.X, p.Z );
}
/// <summary> Returns whether the given world coordinatse are fully not in sunlight. </summary>
public bool IsLit( Vector3 p ) {
int x = Utils.Floor( p.X ), y = Utils.Floor( p.Y ), z = Utils.Floor( p.Z );
if( !IsValidPos( x, y, z ) ) return true;
return y > GetLightHeight( x, z );
}
/// <summary> Returns the y coordinate of the highest block that is fully not in sunlight. </summary>
/// <remarks> e.g. if cobblestone was at y = 5, this method would return 4. </remarks>
public int GetLightHeight( int x, int z ) {

View file

@ -68,9 +68,9 @@ namespace ClassicalSharp.Model {
// TODO: using 'is' is ugly, but means we can avoid creating
// a string every single time held block changes.
if( p is FakePlayer ) {
Vector3I eyePos = Vector3I.Floor( game.LocalPlayer.EyePosition );
FastColour baseCol = game.World.IsLit( eyePos ) ? game.World.Env.Sunlight : game.World.Env.Shadowlight;
col = FastColour.Scale( baseCol, 0.8f );
col = game.World.IsLit( game.LocalPlayer.EyePosition )
? game.World.Env.Sunlight : game.World.Env.Shadowlight;
col = FastColour.Scale( col, 0.8f );
block = ((FakePlayer)p).Block;
} else {
block = Utils.FastByte( p.ModelName );

View file

@ -67,7 +67,7 @@ namespace ClassicalSharp.Model {
pos = p.Position;
if( Bobbing ) pos.Y += p.anim.bobYOffset;
World map = game.World;
col = game.World.IsLit( Vector3I.Floor( p.EyePosition ) ) ? map.Env.Sunlight : map.Env.Shadowlight;
col = game.World.IsLit( p.EyePosition ) ? map.Env.Sunlight : map.Env.Shadowlight;
uScale = 1 / 64f; vScale = 1 / 32f;
scale = p.ModelScale;

View file

@ -1,5 +1,6 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using ClassicalSharp.Map;
using OpenTK;
namespace ClassicalSharp.Particles {
@ -16,9 +17,70 @@ namespace ClassicalSharp.Particles {
public abstract void Render( Game game, double delta, float t,
VertexP3fT2fC4b[] vertices, ref int index );
protected void DoRender( Game game, ref Vector2 size, ref TextureRec rec,
VertexP3fT2fC4b[] vertices, ref int index ) {
Vector3 p111, p121, p212, p222;
Utils.CalcBillboardPoints( size, Position, ref game.View,
out p111, out p121, out p212, out p222 );
World map = game.World;
FastColour col = map.IsLit( Position ) ? map.Env.Sunlight : map.Env.Shadowlight;
vertices[index++] = new VertexP3fT2fC4b( p111, rec.U1, rec.V2, col );
vertices[index++] = new VertexP3fT2fC4b( p121, rec.U1, rec.V1, col );
vertices[index++] = new VertexP3fT2fC4b( p222, rec.U2, rec.V1, col );
vertices[index++] = new VertexP3fT2fC4b( p212, rec.U2, rec.V2, col );
}
public virtual bool Tick( Game game, double delta ) {
Lifetime -= (float)delta;
return Lifetime < 0;
}
}
public sealed class RainParticle : CollidableParticle {
static Vector2 bigSize = new Vector2( 1/16f, 1/16f );
static Vector2 smallSize = new Vector2( 0.75f/16f, 0.75f/16f );
static Vector2 tinySize = new Vector2( 0.5f/16f, 0.5f/16f );
static TextureRec rec = new TextureRec( 2/128f, 14/128f, 3/128f, 2/128f );
public RainParticle() { throughLiquids = false; }
public bool Big, Tiny;
public override bool Tick( Game game, double delta ) {
bool dies = Tick( game, 3.5f, delta );
return hitTerrain ? true : dies;
}
public override int Get1DBatch( Game game ) { return 0; }
public override void Render( Game game, double delta, float t,
VertexP3fT2fC4b[] vertices, ref int index ) {
Position = Vector3.Lerp( lastPos, nextPos, t );
Vector2 size = Big ? bigSize : (Tiny ? tinySize : smallSize);
DoRender( game, ref size, ref rec, vertices, ref index );
}
}
public sealed class TerrainParticle : CollidableParticle {
static Vector2 terrainSize = new Vector2( 1/8f, 1/8f );
internal TextureRec rec;
internal int texLoc;
public override bool Tick( Game game, double delta ) {
return Tick( game, 5.4f, delta );
}
public override int Get1DBatch( Game game ) {
return game.TerrainAtlas1D.Get1DIndex( texLoc );
}
public override void Render( Game game, double delta, float t,
VertexP3fT2fC4b[] vertices, ref int index ) {
Position = Vector3.Lerp( lastPos, nextPos, t );
DoRender( game, ref terrainSize, ref rec, vertices, ref index );
}
}
}

View file

@ -1,42 +0,0 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using ClassicalSharp.Map;
using OpenTK;
namespace ClassicalSharp.Particles {
public sealed class RainParticle : CollidableParticle {
static Vector2 bigSize = new Vector2( 1/16f, 1/16f );
static Vector2 smallSize = new Vector2( 0.75f/16f, 0.75f/16f );
static Vector2 tinySize = new Vector2( 0.5f/16f, 0.5f/16f );
static TextureRec rec = new TextureRec( 2/128f, 14/128f, 3/128f, 2/128f );
public RainParticle() { throughLiquids = false; }
public bool Big, Tiny;
public override bool Tick( Game game, double delta ) {
bool dies = Tick( game, 3.5f, delta );
return hitTerrain ? true : dies;
}
public override int Get1DBatch( Game game ) { return 0; }
public override void Render( Game game, double delta, float t,
VertexP3fT2fC4b[] vertices, ref int index ) {
Position = Vector3.Lerp( lastPos, nextPos, t );
Vector3 p111, p121, p212, p222;
Vector2 size = Big ? bigSize : (Tiny ? tinySize : smallSize );
Utils.CalcBillboardPoints( size, Position, ref game.View,
out p111, out p121, out p212, out p222 );
World map = game.World;
FastColour col = map.IsLit( Vector3I.Floor( Position ) ) ? map.Env.Sunlight : map.Env.Shadowlight;
vertices[index++] = new VertexP3fT2fC4b( p111, rec.U1, rec.V2, col );
vertices[index++] = new VertexP3fT2fC4b( p121, rec.U1, rec.V1, col );
vertices[index++] = new VertexP3fT2fC4b( p222, rec.U2, rec.V1, col );
vertices[index++] = new VertexP3fT2fC4b( p212, rec.U2, rec.V2, col );
}
}
}

View file

@ -1,37 +0,0 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using ClassicalSharp.Map;
using OpenTK;
namespace ClassicalSharp.Particles {
public sealed class TerrainParticle : CollidableParticle {
static Vector2 terrainSize = new Vector2( 1/8f, 1/8f );
internal TextureRec rec;
internal int texLoc;
public override bool Tick( Game game, double delta ) {
return Tick( game, 5.4f, delta );
}
public override int Get1DBatch( Game game ) {
return game.TerrainAtlas1D.Get1DIndex( texLoc );
}
public override void Render( Game game, double delta, float t,
VertexP3fT2fC4b[] vertices, ref int index ) {
Position = Vector3.Lerp( lastPos, nextPos, t );
Vector3 p111, p121, p212, p222;
Utils.CalcBillboardPoints( terrainSize, Position, ref game.View,
out p111, out p121, out p212, out p222 );
World map = game.World;
FastColour col = map.IsLit( Vector3I.Floor( Position ) ) ? map.Env.Sunlight : map.Env.Shadowlight;
vertices[index++] = new VertexP3fT2fC4b( p111, rec.U1, rec.V2, col );
vertices[index++] = new VertexP3fT2fC4b( p121, rec.U1, rec.V1, col );
vertices[index++] = new VertexP3fT2fC4b( p222, rec.U2, rec.V1, col );
vertices[index++] = new VertexP3fT2fC4b( p212, rec.U2, rec.V2, col );
}
}
}