Skip to content

Commit

Permalink
Format and make default return a new instance
Browse files Browse the repository at this point in the history
  • Loading branch information
IrishBruse committed Oct 9, 2024
1 parent fbaa766 commit a203eb8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 64 deletions.
91 changes: 48 additions & 43 deletions LDtk.Codegen/Generators/ClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public void Generate()
void GenClass(string identifier, string folder, EntityDefinition entityDefinition = null,
FieldDefinition[] fieldDefinitions = null)
{
GenHeaders(options);
GenHeaders();

string classDef = $"public partial class {identifier}";

if (entityDefinition != null)
if (entityDefinition != null && Options.EntityInterface)
{
classDef += ": ILDtkEntity";
classDef += " : ILDtkEntity";
}

Line(classDef);
Expand All @@ -54,12 +54,12 @@ void GenClass(string identifier, string folder, EntityDefinition entityDefinitio
void GenEntityFields(string identifier, EntityDefinition entityDefinition)
{
//generate the default data for fields.
Line($"public static readonly {identifier} Default = new()");
Line($"public static {identifier} Default() => new()");
StartBlock();
{
Line($"Identifier = \"{identifier}\",");
Line($"Uid = {entityDefinition.Uid},");
Line($"Size = new Vector2({entityDefinition.Width.ToString()}f, {entityDefinition.Height.ToString()}f),");
Line($"Size = new Vector2({entityDefinition.Width}f, {entityDefinition.Height}f),");
Line($"Pivot = new Vector2({entityDefinition.PivotX}f, {entityDefinition.PivotY}f),");
if (entityDefinition.TileRect != null)
{
Expand Down Expand Up @@ -138,50 +138,55 @@ void GenCustomFieldDefData(FieldDefinition[] fieldDefs)
switch (field._Type)
{
case Field.IntType:
Line($"{field.Identifier} = {defaultValue.GetInt32().ToString()},");
break;
Line($"{field.Identifier} = {defaultValue.GetInt32()},");
break;

case Field.FloatType:
Line(
$"{field.Identifier} = {defaultValue.GetSingle().ToString(CultureInfo.InvariantCulture)}f,");
break;
Line($"{field.Identifier} = {defaultValue.GetSingle().ToString(CultureInfo.InvariantCulture)}f,");
break;

case Field.BoolType:
Line($"{field.Identifier} = {defaultValue.GetBoolean().ToString().ToLower()},");
break;
Line($"{field.Identifier} = {defaultValue.GetBoolean().ToString().ToLower()},");
break;

case Field.StringType:
Line($"{field.Identifier} = {defaultValue.GetRawText()},");
break;
Line($"{field.Identifier} = {defaultValue.GetRawText()},");
break;

case Field.FilePathType:
Line($"{field.Identifier} = {defaultValue.GetString()},");
break;
Line($"{field.Identifier} = {defaultValue.GetString()},");
break;

case Field.TileType:
string[] rectValues = defaultValue.GetString()!.Split(',');
int x = int.Parse(rectValues[0]);
int y = int.Parse(rectValues[1]);
int width = int.Parse(rectValues[2]);
int height = int.Parse(rectValues[3]);
int tilesetUid = (int)field.TilesetUid!;
TilesetRectangle finalRect = new()
{
X = x,
Y = y,
W = width,
H = height,
TilesetUid = tilesetUid
};
GenTilesetRectangle(field.Identifier, finalRect);
break;
string[] rectValues = defaultValue.GetString()!.Split(',');
int x = int.Parse(rectValues[0]);
int y = int.Parse(rectValues[1]);
int width = int.Parse(rectValues[2]);
int height = int.Parse(rectValues[3]);
int tilesetUid = (int)field.TilesetUid!;
TilesetRectangle finalRect = new()
{
X = x,
Y = y,
W = width,
H = height,
TilesetUid = tilesetUid
};
GenTilesetRectangle(field.Identifier, finalRect);
break;

case Field.ColorType:
int colorValue = defaultValue.GetInt32();
int r = (colorValue >> 16) & 0xFF;
int g = (colorValue >> 8) & 0xFF;
int b = colorValue & 0xFF;
Line($"{field.Identifier} = new Color({r}, {g}, {b}, {1}),");
break;
int colorValue = defaultValue.GetInt32();
int r = (colorValue >> 16) & 0xFF;
int g = (colorValue >> 8) & 0xFF;
int b = colorValue & 0xFF;
Line($"{field.Identifier} = new Color({r}, {g}, {b}, {1}),");
break;
}
}
}

void GenHeaders(Options options)
void GenHeaders()
{
Line($"namespace {Options.Namespace};");
Blank();
Expand All @@ -198,10 +203,10 @@ void GenTilesetRectangle(string identifier, TilesetRectangle rect)
Line($"{identifier} = new TilesetRectangle()");
StartBlock();
{
Line($"X = {rect.X.ToString()},");
Line($"Y = {rect.Y.ToString()},");
Line($"W = {rect.W.ToString()},");
Line($"H = {rect.H.ToString()}");
Line($"X = {rect.X},");
Line($"Y = {rect.Y},");
Line($"W = {rect.W},");
Line($"H = {rect.H}");
}
EndBlockSeparator();
}
Expand Down
9 changes: 9 additions & 0 deletions LDtk.Example/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ protected override void Initialize()
_ = renderer.PrerenderLevel(world.Levels[i]);
}

// Default value instance example
var prefab = Enemy.Default();
prefab.Wander = new Vector2[] { Vector2.One * 180, Vector2.One * 120 };
enemies.Add(new EnemyEntity(prefab, spriteSheet, renderer));

var prefab2 = Enemy.Default();
prefab2.Wander = new Vector2[] { Vector2.One * 200, Vector2.One * 100 };
enemies.Add(new EnemyEntity(prefab2, spriteSheet, renderer));

Gun_Pickup gunData = world.GetEntity<Gun_Pickup>();
gun = new GunEntity(gunData, spriteSheet, renderer);

Expand Down
4 changes: 2 additions & 2 deletions LDtk.Example/LDtkTypes/World/Entities/Enemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace LDtkTypes;
using LDtk;
using Microsoft.Xna.Framework;

public partial class Enemy: ILDtkEntity
public partial class Enemy : ILDtkEntity
{
public static readonly Enemy Default = new()
public static Enemy Default() => new()
{
Identifier = "Enemy",
Uid = 98,
Expand Down
4 changes: 2 additions & 2 deletions LDtk.Example/LDtkTypes/World/Entities/Gun_Pickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace LDtkTypes;
using LDtk;
using Microsoft.Xna.Framework;

public partial class Gun_Pickup: ILDtkEntity
public partial class Gun_Pickup : ILDtkEntity
{
public static readonly Gun_Pickup Default = new()
public static Gun_Pickup Default() => new()
{
Identifier = "Gun_Pickup",
Uid = 107,
Expand Down
4 changes: 2 additions & 2 deletions LDtk.Example/LDtkTypes/World/Entities/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace LDtkTypes;
using LDtk;
using Microsoft.Xna.Framework;

public partial class Player: ILDtkEntity
public partial class Player : ILDtkEntity
{
public static readonly Player Default = new()
public static Player Default() => new()
{
Identifier = "Player",
Uid = 120,
Expand Down
4 changes: 2 additions & 2 deletions LDtk.Example/LDtkTypes/World/Entities/RefTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace LDtkTypes;
using LDtk;
using Microsoft.Xna.Framework;

public partial class RefTest: ILDtkEntity
public partial class RefTest : ILDtkEntity
{
public static readonly RefTest Default = new()
public static RefTest Default() => new()
{
Identifier = "RefTest",
Uid = 123,
Expand Down
11 changes: 5 additions & 6 deletions LDtk/LDtkFieldParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ namespace LDtk;

using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using Full;

using Microsoft.Xna.Framework;

using Type = System.Type;
Expand Down Expand Up @@ -33,7 +32,7 @@ public static void ParseCustomEntityFields<T>(T entity, FieldInstance[] fields,
{
ParseCustomFields(entity, fields, level);
}

/// <summary> Parses the base entity fields. </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="entity"> The entity. </param>
Expand All @@ -57,7 +56,7 @@ public static void ParseBaseEntityFields<T>(T entity, EntityInstance entityInsta
ParseBaseField(entity, nameof(ILDtkEntity.Tile), rect);
}
}

static void ParseCustomFields<T>(T classFields, FieldInstance[] fields, LDtkLevel? level)
{
foreach (FieldInstance field in fields)
Expand Down Expand Up @@ -134,7 +133,7 @@ static void ParseCustomFields<T>(T classFields, FieldInstance[] fields, LDtkLeve
}
}
}

static Color ParseStringToColor(string hex, int alpha)
{
if (uint.TryParse(hex.Replace("#", string.Empty), NumberStyles.HexNumber, null, out uint color))
Expand All @@ -150,7 +149,7 @@ static Color ParseStringToColor(string hex, int alpha)
return new Color(0xFF00FFFF);
}
}

static void HandlePoints<T>(T classFields, LDtkLevel level, FieldInstance field, PropertyInfo variableDef, JsonElement element)
{
int gridSize = GetGridSize(level);
Expand Down
8 changes: 1 addition & 7 deletions LDtk/LDtkJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ namespace LDtk;
// LDtk 1.5.3

using System;
using System.Text.Json.Serialization;
using System.Text.Json;

using Full;
using System.Text.Json.Serialization;

using Microsoft.Xna.Framework;

Expand Down Expand Up @@ -191,10 +189,6 @@ public partial class EntityDefinition
/// <summary> Pixel width </summary>
[JsonPropertyName("width")]
public int Width { get; set; }

/// <summary> the field definitions for the entity
[JsonPropertyName("fieldDefs")]
public FieldDefinition[] FieldDefinitions { get; set; }
}

/// <summary> Entity instance </summary>
Expand Down

0 comments on commit a203eb8

Please sign in to comment.