「模组:常用方法」修訂間的差異
小 |
|||
第23行: | 第23行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 参数parentSheetIndex表示 该物品的ID(储存在 ObjectInformation.xnb 文件中)。 | |
===在地上生成物品=== | ===在地上生成物品=== | ||
第57行: | 第57行: | ||
===获取所有地点=== | ===获取所有地点=== | ||
− | < | + | <code>Game1.locations</code>属性中虽然储存着主要的地点,但是不包括''建筑的室内''(constructed building interiors)。以下这个方法提供了主玩家的所有地点。 |
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
/// <summary>Get all game locations.</summary> | /// <summary>Get all game locations.</summary> |
於 2021年6月9日 (三) 14:24 的修訂
← 目錄
頁面仍需完善
該頁面不完善且缺少信息。您可以通過擴充內容來幫助我們。 |
此頁面展示了製作SMAPI模組時常見的任務。在閱讀時,請結合參考模組製作入門和遊戲基本架構。
基礎技巧
追蹤一個值的變化
在寫模組時,你可能常常需要了解一個值的變化(什麼時候變,變化前後的值分別是多少,等等)。如果該值沒有包括在SMAPI內置的事件 (event)中,那麼你可以為該值創建一個私有變量,然後在SMAPI的update tick事件中刷新此變量,以達到追蹤值變化的目的。
示例見:[模組:製作指南/APIs/Events#變化監控]
物品 (Items)
物品 代表那些能夠放在背包里的東西,比如說工具、農作物等等。
創建一個物品的實例(Object)
Object中所有的構造函數:
public Object(Vector2 tileLocation, int parentSheetIndex, int initialStack);
public Object(Vector2 tileLocation, int parentSheetIndex, bool isRecipe = false);
public Object(int parentSheetIndex, int initialStack, bool isRecipe = false, int price = -1, int quality = 0);
public Object(Vector2 tileLocation, int parentSheetIndex, string Givenname, bool canBeSetDown, bool canBeGrabbed, bool isHoedirt, bool isSpawnedObject);
參數parentSheetIndex表示該物品的ID(儲存在 ObjectInformation.xnb 文件中)。
在地上生成物品
public virtual bool dropObject(Object obj, Vector2 dropLocation, xTile.Dimensions.Rectangle viewport, bool initialPlacement, Farmer who = null);
// 调用:
Game1.getLocationFromName("Farm").dropObject(new StardewValley.Object(itemId, 1, false, -1, 0), new Vector2(x, y) * 64f, Game1.viewport, true, (Farmer)null);
添加物品到背包 (Inventory)
// You can add items found in ObjectInformation using:
Game1.player.addItemByMenuIfNecessary((Item)new StardewValley.Object(int parentSheetIndex, int initialStack, [bool isRecipe = false], [int price = -1], [int quality = 0]));
例2:
// Add a weapon directly into player's inventory
const int WEAP_ID = 19; // Shadow Dagger -- see Data/weapons
Item weapon = new MeleeWeapon(WEAP_ID); // MeleeWeapon is a class in StardewValley.Tools
Game1.player.addItemByMenuIfNecessary(weapon);
// Note: This code WORKS.
從背包移除物品
取決於你背包的具體情況。很少有情況需要你親自來調用,因為相關的方法在Farmer類中已經有了。
在大多數情況下,僅需調用 .removeItemFromInventory(Item) 方法。
地點 (Locations)
見 遊戲基本架構#地點。
獲取所有地點
Game1.locations
屬性中雖然儲存着主要的地點,但是不包括建築的室內(constructed building interiors)。以下這個方法提供了主玩家的所有地點。
/// <summary>Get all game locations.</summary>
public static IEnumerable<GameLocation> GetLocations()
{
return Game1.locations
.Concat(
from location in Game1.locations.OfType<BuildableGameLocation>()
from building in location.buildings
where building.indoors.Value != null
select building.indoors.Value
);
}
遍歷:
foreach (GameLocation location in this.GetLocations())
{
// ...
}
注意:在聯機模式中,客機是拿不到上述所有地點的。要解決這一問題,見獲取有效的地點。
編輯地圖
見模組:地圖數據。
玩家 (Player)
自定義精靈 (Custom Sprite)
位置 (Position)
角色(Character) 的位置(Position) 表示他在當前地點(Location) 的坐標。
相對於地圖 (Map)
每個地點(location) 都有一個對應的xTile地圖(map)。如果以像素(pixel) 為單位,地圖左上角坐標代表(0, 0),坐下角則代表(location.Map.DisplayWidth, location.Map.DisplayHeight)。 角色在當前地點的位置有兩種表達方式:
- 以像素(pixel) 為單位的絕對(absoulte) 坐標:
Position.X
與Position.Y
。 - 以圖塊(tile) 為單位的圖塊(tile) 坐標:
getTileX()
與getTileY()
。
常量Game1.tileSize
規定,遊戲內每個圖塊(tile) 大小為64x64像素。於是有以下單位換算:
// 绝对坐标 → 图块坐标
Math.Floor(Game1.player.Position.X / Game1.tileSize)
Math.Floor(Game1.player.Position.Y / Game1.tileSize)
// 图块坐标 → 绝对坐标
Game1.player.getTileX() * Game1.tileSize
Game1.player.getTileY() * Game1.tileSize
// 地图大小(以图块为单位)
Math.Floor(Game1.player.currentLocation.Map.DisplayWidth / Game1.tileSize)
Math.Floor(Game1.player.currentLocation.Map.DisplayHeight / Game1.tileSize)
相對於視野 (Viewport)
視野、視口、視窗(Viewport) 代表在當前屏幕上的區域。若以像素計算,其寬高應該與遊戲的屏幕解像度相等,分別為Game1.viewport.Width
和Game1.viewport.Height
。
玩家相對於視野的位置(像素)可表示為:
Game1.player.Position.X - Game1.viewport.X
Game1.player.Position.Y - Game1.viewport.Y