Modding:Adding Block Behavior/ru: Difference between revisions

From Vintage Story Wiki
(Created page with "Наше поведение все еще довольно просто, но возможностей гораздо больше. Поведение может иметь с...")
(Created page with "== Пример ==")
Line 91: Line 91:
Наше поведение все еще довольно просто, но возможностей гораздо больше. Поведение может иметь специальные свойства, которые могут быть определены самим типом блока.
Наше поведение все еще довольно просто, но возможностей гораздо больше. Поведение может иметь специальные свойства, которые могут быть определены самим типом блока.


== Example ==
== Пример ==


The behavior liquid supports some special properties as shown in this example of the water blocktype:
The behavior liquid supports some special properties as shown in this example of the water blocktype:

Revision as of 12:39, 14 May 2020

Other languages:

Эта страница проверялась в последний раз для версии Vintage Story 1.9.


Введение

Поведение блоков довольно полезно, если вы хотите, чтобы разные блоки действовали одинаково, поскольку вы можете прикрепить к произвольному количеству блоков одно или несколько поведений. Возможно, вы захотите взглянуть на существующие block behaviors перед реализацией своего собственного.

В этом уроке мы создадим новое поведение, которое мы можем прикрепить к блокам, чтобы сделать их подвижными, щелкнув по ним правой кнопкой мыши.

Настройка

Требуется ознакомится со статьёй - Настройка среды для разработки. Кроме того, вам понадобятся ресурсы (тип блока, текстура и файл lang). Вы можете создать свои собственные или использовать готовые: Moving - No CS File.zip

Создание поведения

Итак, прежде всего нам нужно создать само поведение, которое является классом, расширяющим BlockBehavior

class Moving : BlockBehavior
{
    public Moving(Block block) : base(block)
    {
        
    } 
}

Этот класс предоставляет несколько методов, которые мы можем переопределить. Когда вы используете Visual Studio, вы можете найти полный список методов, наведя курсор мыши на «BlockBehavior» и нажав «F12».


Метод bool OnPlayerInteract (IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) выглядит идеально подходящим для наших целей.

Что это должно сделать?

  1. Рассчитать новую позицию блока в зависимости от стороны, в которую смотрит игрок
  2. Проверить, можно ли разместить блок в этой позиции
  3. Удалить оригинальный блок
  4. Поместить новый блок, используя ранее рассчитанную позицию
    public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
    {
        // Находим целевую позицию
        BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite());

        // Можем ли мы разместить блок там?
if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block))
        {
            // Remove the block at the current position and place it at the target position
            world.BlockAccessor.SetBlock(0, blockSel.Position);
            world.BlockAccessor.SetBlock(block.BlockId, pos);
        }

        // Сообщаем игровому движку о других действиях блока, которые мы обрабатывали при взаимодействии игроков с блоком.
        // Если бы мы не установили поле обработки, игрок все равно мог бы размещать блоки, если бы он держал их в руках.
handling = EnumHandling.PreventDefault;
        return true;
 }

Регистрация

Чтобы зарегистрировать BlockBehavior, нам нужно создать класс мода, переопределить Start (ICoreAPI) и зарегистрировать его с заданным именем:

    public class MovingBlocks : ModSystem
    {

        public override void Start(ICoreAPI api)
        {
            base.Start(api);
            api.RegisterBlockBehaviorClass("Moving", typeof(Moving));
        }

    }

Распределение

Чтобы закончить все, откройте modtools и введите pack <ваш mod id> </ code>. Теперь вы можете взять zip-файл и поделиться им с другими людьми.

Тестирование

Расширенное поведение

Наше поведение все еще довольно просто, но возможностей гораздо больше. Поведение может иметь специальные свойства, которые могут быть определены самим типом блока.

Пример

The behavior liquid supports some special properties as shown in this example of the water blocktype:

	behaviors: [
		{
			name: "FiniteSpreadingLiquid", 
			properties:
			{
				spreadDelay: 150, 
				liquidCollisionSound: "hotmetal", 
				sourceReplacementCode: "obsidian", 
				flowingReplacementCode: "basalt"
			}
		}
	],

Parsing properties

In order to take care of special properties there is a method called Initialize(JsonObject). Each blocktype creates a new instance of the behavior, so the method can be used to parse the properties.

So what kind of properties could we add?

  • push distance
  • pull block if player is sneaking

First of all, we need to override the method in our block behavior class ...

        public override void Initialize(JsonObject properties)
        {
            base.Initialize(properties);
        }

Additionally we need to add two fields, one for the distance and another one if the player should pull the block while sneaking ...

        public int distance = 1;
        public bool pull = false;

Now we can parse the two properties like so:

        distance = properties["distance"].AsInt(1);
        pull = properties["pull"].AsBool(false);

The next thing we need to change is the interact method itself, so that it takes care of the distance and the pull properties ...

        public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
        {
            BlockPos pos = blockSel.Position.AddCopy(pull && byPlayer.WorldData.EntityControls.Sneak ? blockSel.Face : blockSel.Face.GetOpposite(), distance);
            if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block))
            {
                world.BlockAccessor.SetBlock(0, blockSel.Position);
                world.BlockAccessor.SetBlock(block.BlockId, pos);
            }
            handling = EnumHandling.PreventDefault;
            return true;
        }

Adding another block

Let's create another block using this behavior, but this time we will configure some additional properties ...

	behaviors: [
		{
			name: "Moving",
			properties: {
				"distance": 2,
				"pull": true
			}
		}
	],

The block will be pushed two blocks instead of one and the player can pull it by sneaking while right clicking.

Mod Download


Icon Sign.png

Wondering where some links have gone?
The modding navbox is going through some changes! Check out Navigation Box Updates for more info and help finding specific pages.