samedi 2 juin 2018

Any way to get the definition of a property from the code itself?

public abstract class Unit
{
    public abstract List<Move> allowedMoves{get;}
}

public class Javelineer : Unit
{
    public List<Move> allowedMoves =>
        new List<Move> {Move.Impale, Move.JavelinThrow, Move.ShieldBlock};
}

public class Dragon : Unit
{
    public List<Move> allowedMoves =>
        new List<Move> {Move.BreatheFire, Move.Swipe, Move.Bite, Move.Devour, Move.TailBash};
}

The X:

Given the above code, if and how can I retrieve the allowed moves of a given unit without necessarily instantiating a new object?

I know I can retrieve the property with this code:

typeof(Javelineer).GetProperty("allowedMoves")

But if and how can I retrieve the definition of this property?

The Y:

The client (web browser) must send the game server the player's unit. This includes the unit's type and moves this unit is able to perform (4 out of all available; similarily to Pokemon).

While the validation (of course) is performed on the server, the browser still needs to get a list of available unit types and allowed moves.

In order not to duplicate code, I would like to avoid hard-coding this data in Javascript.

Having read some excellent SO questions & answers I think I can retrieve all available units with code similar to this:

Assembly.GetExecutingAssembly().GetTypes().Where(
    type => type.BaseType == typeof(Unit)
).Select(type => type.Name).ToList()

I'd call this code on server startup, cache the result and send the cached result to every connecting client, because I have feeling this code is likely expensive to call.

But how can I retrieve the list of allowed moves?





Aucun commentaire:

Enregistrer un commentaire