wmwpy.classes.objectpack package¶
Submodules¶
wmwpy.classes.objectpack.pack module¶
- class wmwpy.classes.objectpack.pack.ObjectPack[source]¶
Bases:
object
An Object Pack is a pack of object types for a single game.
There are two parts to an Object Pack, the pack, and the object types. To get started, create an Object Pack.
from wmwpy.classes.objectpacks import ObjectPack pack = ObjectPack() `` Next, define the object types. ```python from wmwpy.classes.objectpacks import ObjectPack, Type pack = ObjectPack() class spout(Type): ... pack.register_type(spout)
To learn how to define a Type, see
objectpack.Type
.- get_type(object_type: str, obj: Object = None) type[wmwpy.classes.objectpack.type.Type] [source]¶
Get the object Type class that is registed inside this object pack. If the Type cannot be found, it defaults to an empty object type.
- register_type(object_type: type[wmwpy.classes.objectpack.type.Type])[source]¶
Register an object type. The type must inherit from
classes.objectpack.Type
.- Parameters:
object_type (type[Type]) – Object Type constructor
- Raises:
TypeError – type must inherit from the classes.objectpack.type.Type class
wmwpy.classes.objectpack.type module¶
- class wmwpy.classes.objectpack.type.Type(obj: Object | None = None)[source]¶
Bases:
object
The Type class is an object used to modify an object image / properties on a case-by-case basis.
- NAME¶
The name of the Type property in objects that this is for.
- Type:
str
- PROPERTIES¶
All the default properties that this object type has.
- Type:
dict
Defining the type¶
To get started, the new type must inherit from the
Type
class. There are also a few contants that need to be defined.from wmwpy.classes.objectpacks import Type class yswitch(Type): NAME = 'yswitch' PROPERTIES = { 'YSwitchPosition' : { 'type' : 'bit', 'default' : '0', }, 'ToggleSpriteIndex' : { 'type' : 'int', 'default' : '1', }, }
The
PROPERTIES
attribute¶The
PROPERTIES
attribute is a dictionary of the properties, which are also dictionaries containing the value type, default value, and options, if any. For examplePROPERTIES = { "ExpulsionAngle": { 'type' : 'float', 'default' : '0.0', }, "FluidType": { 'type' : 'string', 'default' : 'water', 'options' : [ "water", "contaminatedwater", "lava", "steam", "mud", "drymud", "wetmud", ], }, }
The possible types are explained in
Type.value
.Getting the sprites ready¶
After you’ve defined the
NAME
andPROPERTIES
, you can then get the sprites ready for the image.class yswitch(Type): NAME = 'yswitch' PROPERTIES = { 'YSwitchPosition' : { 'type' : 'bit', 'default' : '0', }, 'ToggleSpriteIndex' : { 'type' : 'int', 'default' : '1', }, } def ready_sprites(self): YSwitchPosition = self.get_property('YSwitchPosition') ToggleSpriteIndex = self.get_property('ToggleSpriteIndex') if YSwitchPosition != 1: YSwitchPosition = 0 self.obj.sprites[ToggleSpriteIndex].angle = (360 / -3) * (YSwitchPosition + 1)
The sprites and object is in a safe mode when this is called, meaning, you can modify the properties without worrying about them carrying onto the output xml.
Required properties in the level xml¶
If the object requires some properties to always be in the level xml, even if they are different from their default property, you can specify it.
from wmwpy.classes.objectpacks import Type class fluidconverter(Type): NAME = 'fluidconverter' def ready_properties(self): return super().ready_properties(include = [ 'FluidType', 'FluidType#', 'StartingFluidType', 'ConverterType', ])
- DEFAULT_PROPERTY = {'default': '', 'type': 'string'}¶
- NAME: str = ''¶
- PROPERTIES: dict[str, dict[Literal['type', 'default', 'options'], str | list[str]]] = {}¶
- VALUE_TYPES = ['string', 'float', 'int', 'bit', 'Vector2', 'Vector2,...']¶
- get_property(property: str) str | float | int | list[str | float | int] | list[list[str | float | int]] [source]¶
Get the property from the object. If the object doesn’t have the property it looks for the default property, then on the Type object itself.
- Parameters:
property (str) – The name of the property.
- Returns:
The resulting value as python built-in data type.
- Return type:
str | float | int | list[str | float | int], list[list[str | float | int]]
- ready_properties(include: list[str] = []) dict[str, str] [source]¶
Ready the properties before they are put into the level xml. By default, properties that are equal to their default property counterpart are removed, except ‘Type’, ‘Angle’, and ‘Filename’.
- Parameters:
include (list[str], optional) – List of properties to always keep. Defaults to [].
- Returns:
The new properties.
- Return type:
dict[str,str]
To use this inside a custom Type, just call the super() with the include argument.
from wmwpy.classes.objectpacks import Type class fluidconverter(Type): NAME = 'fluidconverter' def ready_properties(self): return super().ready_properties(include = [ 'FluidType', 'FluidType#', 'StartingFluidType', 'ConverterType', ])
- ready_sprites()[source]¶
Get the sprites ready for generating the object image.
In this method, you can modify any object or sprites properties to generate the correct image based on the object properties. There are also many methods that can be used to make the process easier.
from wmwpy.classes.objectpacks import Type class yswitch(Type): NAME = 'yswitch' def ready_sprites(self): YSwitchPosition = self.get_property('YSwitchPosition') ToggleSpriteIndex = self.get_property('ToggleSpriteIndex') if YSwitchPosition != 1: YSwitchPosition = 0 self.obj.sprites[ToggleSpriteIndex].angle = (360 / -3) * (YSwitchPosition + 1)
You can also modify sprite images directly.
from wmwpy.classes.objectpacks import Type from wmwpy.utils import imageprocessing class star(Type): NAME = 'star' def ready_sprites(self): StarType = self.get_property('StarType').lower() if StarType == 'note': color = tuple(self.get_property('Color')) try: self.obj.sprites[2].image = imageprocessing.recolor_image( self.obj.sprites[2].image, color ) except: pass
- split_property_num(property) tuple[str, str] [source]¶
Split a property name and number, such as, ‘ConnectedSpout0’ returns
('ConnectedSpout', '0')
.- Parameters:
string (str) – Input property
- Raises:
TypeError – Property must be str
- Returns:
(name, number)
- Return type:
tuple[str,str]
- value(value: str, type: Literal['string', 'float', 'int', 'bit', '<Vector>', '<Vector ...>', '<Vector,...>'] = 'string') str | float | int | list[str | float | int] | list[list[str | float | int]] [source]¶
Convert this value from a string to a python build-in data type.
- Parameters:
value (str) – The value
type (Literal['string', 'float', 'int', 'bit', '', '<Vector ...>', '<Vector,...>' ], optional) – The value type. Defaults to ‘string’.
‘string’
‘float’
‘int’
‘bit’ (0 or 1)
‘’ (list of types seperated by spaces, e.g. ‘string int’)
‘ …’ (list of retypes seperated by spaces, but also can be repeated, e.g. ‘string int …’)
‘,…’ (list of types seperated by spaces, but also repeated by commas, e.g. ‘string int,…’)
Example
>> Type.value('foo', 'string') 'foo' >> Type.value('2.5', 'float') 2.5 >> Type.value('5', 'int') 5 >> Type.value('text 5', 'string int') ['text', 5] >> Type.value('0.5 2.2,-2.5 5.2', 'float float,...') [[0.5,2.2],[-2.5,5.2]]