fieryhenry / tbcml

A python library designed to make modding The Battle Cats easier, more automated, and more powerful
MIT License
7 stars 3 forks source link

Add ModLoader class to make managing project easier #8

Closed enderelijas closed 4 months ago

enderelijas commented 4 months ago

The ModLoader class aims at reducing boilerplate, making it easier to manage larger scale projects.

In addition to this I propose a new way of organizing the project by using classes any time you want to create or add anything. For example, the code below displays how to add a custom cat using this new approach.

#cats/seal.py
from tbcml.core import Cat, CatForm, CatFormType, GamePacks, Cats, Enemy, Enemies, EnemyStatsData, EnemyNames, EnemyModel, EnemyStats, CatModel
class SealCat(Cat):
    def __init__(self, game_data: GamePacks):
        self.id = 0
        self.game_data = game_data
        self.name = "Seal"
        self.description = "A friendly red fellow that does not resemble a cat."
        super().__init__(cat_id=self.id)

    def initialize(self):
        self.set_first_form()

    def set_first_form(self):
        enemy = Enemies.from_game_data(self.game_data).get_enemy(8)
        enemy_model = enemy.get_anim().model
        enemy_icon = enemy.get_enemy_icon()

        cat_model = CatModel.create_empty(self.id, CatFormType.FIRST)
        cat_model.model = enemy_model.copy()
        cat_model.model.flip_x()
        cat_model.model.set_unit_form(cat_model.form.value)
        cat_model.set_cat_id(self.id)

        form = CatForm(self.id, CatFormType.FIRST, anim=cat_model)
        form.import_enemy(enemy)

        form.stats.cost = 0
        form.deploy_icon = enemy_icon
        # form.format_deploy_icon()

        self.set_form(CatFormType.FIRST, form)
#main.py
from tbcml.core import CountryCode, Mod, Cat, CatForm, CatFormType, ModEdit, ModLoader

from cats.seal import SealCat

mod = Mod("TestMod", "test_author", "Test Mod!", Mod.create_mod_id(), mod_version="0.0.1", password="test")
loader = ModLoader(CountryCode.EN, "12.3", mod)

loader.initialize()

seal_cat = SealCat(game_data=loader.game_packs)
seal_cat.initialize()

loader.add_cat(seal_cat)
loader.compile(True)
fieryhenry commented 4 months ago

This looks great. When I get home I'll play around with it and merge it

enderelijas commented 4 months ago

Fixed ModLoader not being included in core / __init__.py so you should now be able to import it from core with no issues.

fieryhenry commented 4 months ago

ok i'll merge it and i'll make some minor changes myself