Confirmedusers
409
edits
(Created page with "----") |
No edit summary |
||
(28 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
В этом уроке мы создадим новое поведение(Behavior), которое можно будет прикреплять к блокам, чтобы сделать их подвижными, нажав правую кнопку мыши. | В этом уроке мы создадим новое поведение(Behavior), которое можно будет прикреплять к блокам, чтобы сделать их подвижными, нажав правую кнопку мыши. | ||
== Подготовка == | |||
Этот руководство по '''code mod''' требует наличия среды разработки. Если у вас её еще нет, прочтите руководство по {{ll|Modding:Setting up your Development Environment|настройке среды для разработки}}. Кроме того, вам понадобятся активы (тип блока, текстура и lang-файл). Вы можете либо создать свой собственный, либо воспользоваться готовыми: [https://wiki.vintagestory.at/images/2/2f/Moving_-_No_CS_File.zip Moving - No CS File.zip] | Этот руководство по '''code mod''' требует наличия среды разработки. Если у вас её еще нет, прочтите руководство по {{ll|Modding:Setting up your Development Environment|настройке среды для разработки}}. Кроме того, вам понадобятся активы (тип блока, текстура и lang-файл). Вы можете либо создать свой собственный, либо воспользоваться готовыми: [https://wiki.vintagestory.at/images/2/2f/Moving_-_No_CS_File.zip Moving - No CS File.zip] | ||
Line 32: | Line 32: | ||
---- | ---- | ||
Метод <code>bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)</code> выглядит идеально подходящим для нашей цели. | |||
Что метод должен делать? | |||
# | # Рассчитать новую позицию для перемещения блока, основываясь на лицевой стороне блока, на которую смотрит игрок. | ||
# | # Проверить, можно ли поместить блок в эту новую позицию. | ||
# | # Удалить блок в старой позиции. | ||
# | # Поместить блок того же типа на новую позицию. | ||
# [[Modding:Behavior_Traversal|Skip]] | # [[Modding:Behavior_Traversal|Skip]] логика по умолчанию, которая в противном случае поместит любой удерживаемый предмет на старую позицию. | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) | public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) | ||
{ | { | ||
// | // Найдите выбранную позицию | ||
BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.Opposite); | BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.Opposite); | ||
// | // Можем ли мы разместить здесь блок? | ||
if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block)) | if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block)) | ||
{ | { | ||
// | // Удалите блок в текущей позиции и поместите его в выбранную позицию | ||
world.BlockAccessor.SetBlock(0, blockSel.Position); | world.BlockAccessor.SetBlock(0, blockSel.Position); | ||
world.BlockAccessor.SetBlock(block.BlockId, pos); | world.BlockAccessor.SetBlock(block.BlockId, pos); | ||
} | } | ||
// | // Сообщите игровому движку и другим моделям поведения блоков, что мы обработали взаимодействие игрока с блоком. | ||
// | // Если бы мы не задали поле обработки, игрок все равно смог бы ставить блоки, если бы они были у него в руках. | ||
handling = EnumHandling.PreventDefault; | handling = EnumHandling.PreventDefault; | ||
return true; | return true; | ||
Line 61: | Line 61: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Регистрация == | ||
Чтобы зарегистрировать BlockBehavior, нам нужно создать класс мода, переопределив от <code>Start(ICoreAPI)</code> и зарегистрировать его с указанным именем: | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 78: | Line 78: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Распространение == | ||
Чтобы закончить все, откройте modtools и введите <code>pack <your mod id></code>. Теперь вы можете взять zip-файл и поделиться им с другими людьми. | |||
* | * Для VS 1.9: [https://wiki.vintagestory.at/images/2/2a/Moving_v1.0.0.zip Moving_v1.0.0.zip] | ||
* | * Для VS 1.6: [https://wiki.vintagestory.at/images/c/cb/Moving.zip Moving.zip] | ||
== | == Тестирование == | ||
<youtube>8eVG0uQF2xs</youtube> | <youtube>8eVG0uQF2xs</youtube> | ||
= | = Расширенное Поведение = | ||
Наше созданное поведение все еще довольно простое, но возможностей гораздо больше. Поведение может иметь особые свойства, которые могут быть определены самим типом блока. | |||
== | == Пример == | ||
Поведение жидкости поддерживает некоторые особые свойства, как показано в этом примере с типом блока "Вода": | |||
<syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
Line 111: | Line 111: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Разбор свойств == | ||
Для того чтобы разобраться со специальными свойствами, существует метод <code>Initialize(JsonObject)</code>. Каждый blocktype создает новый экземпляр поведения, поэтому этот метод можно использовать для разбора свойств. | |||
Итак, какие свойства мы можем добавить? | |||
* | * Толчок блока на расстояние | ||
* | * Тянуть блок, если игрок крадется | ||
Прежде всего, нам нужно переопределить метод в нашем классе поведения блока ... | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 128: | Line 128: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Дополнительно нам нужно добавить два поля, одно для расстояния, а другое - если игрок должен потянуть за блок, когда крадется... | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 135: | Line 135: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Теперь мы можем разобрать эти два свойства следующим образом: | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 144: | Line 144: | ||
---- | ---- | ||
Следующее, что нам нужно изменить, - это сам метод interact, чтобы он позаботился о свойствах расстояния и притяжения... | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) | public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) | ||
{ | { | ||
// | // Найдите выбранную позицию | ||
BlockPos pos = blockSel.Position.AddCopy(pull && byPlayer.WorldData.EntityControls.Sneak ? blockSel.Face : blockSel.Face.Opposite, distance); | BlockPos pos = blockSel.Position.AddCopy(pull && byPlayer.WorldData.EntityControls.Sneak ? blockSel.Face : blockSel.Face.Opposite, distance); | ||
// | // Можем ли мы разместить здесь блок? | ||
if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block)) | if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block)) | ||
{ | { | ||
// | // Удалите блок в текущей позиции и поместите его в выбранную позицию | ||
world.BlockAccessor.SetBlock(0, blockSel.Position); | world.BlockAccessor.SetBlock(0, blockSel.Position); | ||
world.BlockAccessor.SetBlock(block.BlockId, pos); | world.BlockAccessor.SetBlock(block.BlockId, pos); | ||
} | } | ||
// | // Сообщите игровому движку и другим моделям поведения блоков, что мы обработали взаимодействие игрока с блоком. | ||
// | // Если бы мы не задали поле обработки, игрок все равно смог бы ставить блоки, если бы они были у него в руках. | ||
handling = EnumHandling.PreventDefault; | handling = EnumHandling.PreventDefault; | ||
return true; | return true; | ||
Line 166: | Line 166: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Добавление другого блока == | ||
Давайте создадим еще один блок, используя это поведение, но на этот раз мы настроим некоторые дополнительные свойства ... | |||
<syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
Line 182: | Line 182: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Блок будет толкать два блока вместо одного, и игрок может тянуть его, крадясь, нажимая правую кнопку мыши. | |||
= | = Скачать Мод = | ||
* | * Для VS 1.9: [https://wiki.vintagestory.at/images/7/7b/Advancedmoving_v1.0.0.zip AdvancedMoving_v1.0.0.zip] | ||
* | * Для VS 1.6: [https://wiki.vintagestory.at/images/7/72/AdvancedMoving.zip AdvancedMoving.zip] | ||
{{Navbox/modding|Vintage Story}} | {{Navbox/modding|Vintage Story}} |