Skyrim style item system

Asked

Viewed 144 times

2

I am developing a 2D RPG in C# Skyrim style, and I am on the part of implementing the game items, but with many difficulties. I wanted to do a mechanics similar to Skyrim in which you have a range of items sorted in various ways like for example armaduras, armas, poções, anéis, amuletos, etc. Some items may be encantados and desencantados, but some items are restricted to specific incantations. I created a class diagram to represent the system but I think it’s not very flexible. As it stands, it seems that if I want to create more types of items in the future, new classes will have to be created. If there are 1000 items in the game and I have to create a class for each one I’m, as they say, "chipped".

Another problem I’m facing is the implementation of the incantations. Who played Skyrim knows very well how it works. Suppose you want to charm a ring with an enchantment that enhances the player’s HP. For this it is necessary to use a charmer and possess the charm of HP. I’m not gonna use the soul-stone scheme. The effectiveness of the spell will depend on the current attributes of the player. The more skilled in incantation, the greater the amount of HP added by the incantation. But I end up falling in the same case of the items; for each enchantment a class?

Here is the diagram: Diagrama de classes

To circumvent the excessive class creation I created an example just with the class Item and the interfaces Consumable and Equipable but I don’t know if it was a good choice. How do I identify an item? How do I differentiate one item from another? Enums maybe? I had to pass a Player to the builder of Item to be able to modify their fields in equip(), unequip() and consume() and it sounds very much like a gambiarra

public class Player {

    int hp;

    public Player(int hp) {
        this.hp = hp;
    }

}
public abstract class Item implements Equipable, Consumable{
    Player possesor;
    String name = "";
    int price = 0;
    boolean questItem = false;
    boolean equiped = false;

    public Item(Player p) {
        possesor = p;
    }
}
public interface Equipable {
    void equip();
    void unequip();
}
public interface Consumable {
    void consume();
}
package br.com.interfaces;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class Tester {

    public static void main(String[] args) {

        Item ringOfLife = new Item(new Player(0)) {

            @Override
            public void consume() {
                throw new NotImplementedException();
            }

            @Override
            public void unequip() {
                if (equiped) {
                    possesor.hp -= 10;
                    equiped = false;
                }
            }

            @Override
            public void equip() {
                // Ok, eu sei, isso deveria ser um encantamento ,-,
                if (!equiped) {
                    possesor.hp += 10;
                    equiped = true;
                }
            }
        };

        assert(ringOfLife.possesor.hp == 0);
        assert(ringOfLife.equiped == false);
        ringOfLife.equip();
        assert(ringOfLife.possesor.hp == 10);
        assert(ringOfLife.equiped == true);
        ringOfLife.unequip();
        assert(ringOfLife.possesor.hp == 0);
        assert(ringOfLife.equiped == false);
    }
}

I think I’ve provided all the necessary information. What do you think? Advise any Design Pattern?

  • 1

    You don’t need to create a class for each item. By its diagram "Armor", "Weapon" and "Potion" already contains the information you need, the values of each property can be loaded via some external file such as an xml or an Sqlite database.

1 answer

2

I recommend only that you work with Interfaces, instead of just inheritances. In this article has a legal example, including with characters and weapons, about Injection of Dependency.

I do not recommend using Ninject specifically as your DI. I recommend the Simpleinjector, because it is also simple to implement, it is lighter and also the fastest on the market.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.