C # character clothing system

I am trying to create a flexible system for the clothes of my character in my game. Right now I have a base class from which all classes of children's clothes will be distributed, and additional interfaces for casting the body parts that they actually impose on the equation. Here is my code:

public class Clothing { public EClothingBind[] Types; public int ID; public int Priority; public bool HidesUnderlying; public AnimatedSpriteMap Graphic; public Color Blend = Color.White; public virtual void Update(GameTime gameTime) { Graphic.Update(gameTime); } public virtual void Draw(Vector2 position) { Graphic.Tint = Blend; Graphic.Draw(position); } } public interface IClothingHead { ... } public interface IClothingChest { ... } public interface IClothingLegs { ... } public interface IClothingFeet { ... } 

This does not currently work, because the design does not really limit the implementation of my IClothing interfaces. Is there a way to limit the implementation of an interface to certain types? I don’t want to turn my interfaces into classes, because I would like the clothes of the clothes, say, a bathrobe, to cover the whole body (all four). I lost a little what the best way to do this, and all the materials are very appreciated. Thanks.

+4
source share
3 answers

I think you are in the mood for some difficult times in this regard. Of course, the dressing gown covers everything, and graphically it will be and, possibly, will affect the “armor” of the meaning of the four areas. But the implementation of this seems to be very cumbersome. You probably just need one IClothing interface and some definitions of where it can be installed (e.g. Hands / Feet, etc.). Traditionally, mantles usually represent a chest, but they visually cover the entire character.

 enum PositionType { Head, Chest, Hand, Feet, Finger, Neck } public interface IClothing { PositionType Type { get; } // Other things you need IClothing-classed to implement } 

I think this may be the best approach, rather than trying to fine-tune exactly those parts of the body that a particular piece of clothing does by implementing multiple interfaces.

+6
source

This extends moo-Juice's answer (just do not put it in a comment as formatting is not allowed).

Your PositionType has the [Flags] attribute, which you can do:

 [Flags] enum PositionType { Head, Chest, Hand, Feet, Finger, Neck } Armor robe = new Armor(); //sure why not. robe.ArmorValue = 125.3; robe.Appeal = 100; robe.PositionType = PositionType.Chest | PositionType.Arms | PositionType.BellyButton; 

and etc.

+3
source

Instead of dividing the interfaces by the class of clothes, this may be more useful, as others noted that each piece of clothing indicates that it is (headgear, bathrobe, etc.). Although this approach, as a rule, is not related to object-oriented programming, there are probably not many methods that would be applicable to one type of clothing that could not be applied to others (even if in some cases the effect may be “no action” "). For example, if the AnimateResponseToHeadMovement method existed, then TopHat could do something, but LeftSock would not, but the behavior of LeftSock would still be clearly defined. Please note that defining actions is quite expansive, allowing you to take into account situations in which, for example, a human hat may need to be animated in response to their hand movements, because it hung especially low. If the "AnimateResponseToArmMovement" method was not present in the IHeadWear interface, such an answer may not be possible.

The big problem that I suspect will determine how clothing items interact. You can define the "CanBeWornInCombinationWith" method, which contains a list of clothes and reports of conflicts. It is also possible to define a list of “resources” required for each piece of clothing and confirm that there are enough “resources” to cover all the required clothes. Note that this approach can be a little complicated if some clothes choose which resources they require than others (for example, some can only be worn on the right side, while others can be worn). Even defining an interface that allows clothes to determine whether this combination can be worn would be a problem.

Before creating an interface design, I suggest you understand what you are really trying to do; How universal is the need for you to allow interactions between types of clothes and what functions clothes should have.

+1
source

Source: https://habr.com/ru/post/1385604/


All Articles