打开主菜单
首页
随机
登录
设置
关于Stardew Valley Wiki
免责声明
Stardew Valley Wiki
搜索
查看“模组:常用方法”的源代码
←
模组:常用方法
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
← [[模组:目录|目录]] {{stub}} {{翻译}} 此页面展示了制作SMAPI模组时常见的任务。在阅读时,请结合参考'''[[模组:创建 SMAPI 模组|模组制作入门]]'''和'''[[模组:制作指南/游戏基本架构|游戏基本架构]]'''。 ==基础技巧== ===追踪一个值的变化=== 在写模组时,你可能常常需要了解一个值的变化(什么时候变,变化前后的值分别是多少,等等)。如果该值没有包括在SMAPI内置的'''事件''' (event)中,那么你可以为该值创建一个私有变量,然后在SMAPI的update tick事件中刷新此变量,以达到追踪值变化的目的。 示例见:[模组:制作指南/APIs/Events#变化监控] ==物品 (Items)== 物品 代表那些能够放在背包里的东西,比如说工具、农作物等等。 ===创建一个物品 (Object) 的实例=== Object中所有的构造函数: <syntaxhighlight lang="c#"> 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); </syntaxhighlight> 参数parentSheetIndex表示该物品的ID(储存在 ObjectInformation.xnb 文件中)。 ===在地上生成物品=== <syntaxhighlight lang="c#"> 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); </syntaxhighlight> ===添加物品到背包 (Inventory)=== <syntaxhighlight lang="c#"> // 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])); </syntaxhighlight> 例2: <syntaxhighlight lang="c#"> // 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. </syntaxhighlight> ===从背包移除物品=== 取决于你背包的具体情况。很少有情况需要你亲自来调用,因为相关的方法在Farmer类中已经有了。 在大多数情况下,仅需调用 .removeItemFromInventory(Item) 方法。 ==地点 (Locations)== 见 [[模组:制作指南/游戏基本架构#地点|游戏基本架构#地点]]。 ===获取所有地点=== <code>Game1.locations</code>属性中虽然储存着主要的地点,但是不包括''建筑的室内''(constructed building interiors)。以下这个方法提供了主玩家的所有地点。 <syntaxhighlight lang="c#"> /// <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 ); } </syntaxhighlight> 遍历: <syntaxhighlight lang="c#"> foreach (GameLocation location in this.GetLocations()) { // ... } </syntaxhighlight> 注意:在联机模式中,客机是拿不到上述所有地点的。要解决这一问题,见[[模组:制作指南/APIs/Multiplayer#Get active locations|获取有效的地点]]。 ===编辑地图=== 见[[模组:地图数据]]。 ==玩家 (Player)== ===自定义精灵 (Custom Sprite)=== ===位置 (Position)=== 角色(Character) 的位置(Position) 表示他在当前地点(Location) 的坐标。 ====相对于地图 (Map)==== 每个地点(location) 都有一个对应的xTile地图(map)。如果以像素(pixel) 为单位,地图左上角坐标代表''(0, 0)'',坐下角则代表<tt>(location.Map.DisplayWidth, location.Map.DisplayHeight)</tt>。 角色在当前地点的位置有两种表达方式: * 以像素(pixel) 为单位的绝对(absoulte) 坐标:<code>Position.X</code>与<code>Position.Y</code>。 * 以图块(tile) 为单位的图块(tile) 坐标:<code>getTileX()</code>与<code>getTileY()</code>。 常量<code>Game1.tileSize</code>规定,游戏内每个图块(tile) 大小为64x64像素。于是有以下单位换算: <syntaxhighlight lang='c#'> // 绝对坐标 → 图块坐标 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) </syntaxhighlight> ====相对于视野 (Viewport)==== 视野、视口、视窗(Viewport) 代表在当前屏幕上的区域。若以像素计算,其宽高应该与游戏的屏幕分辨率相等,分别为<code>Game1.viewport.Width</code>和<code>Game1.viewport.Height</code>。 玩家相对于视野的位置(像素)可表示为: <syntaxhighlight lang='c#'> Game1.player.Position.X - Game1.viewport.X Game1.player.Position.Y - Game1.viewport.Y </syntaxhighlight> ==NPC== ===自定义NPC=== 想要自定义NPC,你得修改或添加以下文件: 格式: '''''具体操作''''':'''''(往)目标文件夹或文件名''''' * 添加新文件:<tt>Characters\Dialogue\<文件名></tt> * 添加新文件:<tt>Characters\schedules\<文件名></tt> * 添加新文件:<tt>Portraits\<文件名></tt> * 添加新文件:<tt>Characters\<文件名></tt> * 在已有文件中添加新的条目:<tt>Data\EngagementDialogue</tt>(可结婚NPC) * 在已有文件中添加新的条目:<tt>Data\NPCDispositions</tt> * 在已有文件中添加新的条目:<tt>Data\NPCGiftTastes</tt> * 在已有文件中添加新的条目:<tt>Characters\Dialogue\rainy</tt> * 在已有文件中添加新的条目:<tt>Data\animationDescriptions</tt>(在行程(schedule) 中增加自定义的动画) 以上所有的操作都可以通过<code>IAssetLoaders</code>/<code>IAssetEditors</code>或者<code>Content Patcher</code>做到。 最后,你需要调用<code>NPC</code>的构造器来创建实例。 <syntaxhighlight lang='c#'> public NPC(AnimatedSprite sprite, Vector2 position, int facingDir, string name, LocalizedContentManager content = null); public NPC(AnimatedSprite sprite, Vector2 position, string defaultMap, int facingDir, string name, Dictionary<int, int[]> schedule, Texture2D portrait, bool eventActor); public NPC(AnimatedSprite sprite, Vector2 position, string defaultMap, int facingDirection, string name, bool datable, Dictionary<int, int[]> schedule, Texture2D portrait); // 调用: Game1.getLocationFromName("Town").addCharacter(npc); </syntaxhighlight> ==用户界面 (UI)== 用户界面(User-interface、UI) 是指一系列有关界面元素(如按钮、列表框、下拉框等等)以及它们组合起来呈现的画面(如菜单、HUD等等)。 //TODO:欢迎补充UI方面的内容。 ===HUD消息=== HUD消息是指你屏幕左下角时常弹出来的消息框。以下是它的构造函数(不包括一部分无关的): <syntaxhighlight lang='c#'> public HUDMessage(string message); public HUDMessage(string message, int whatType); public HUDMessage(string type, int number, bool add, Color color, Item messageSubject = null); public HUDMessage(string message, string leaveMeNull) public HUDMessage(string message, Color color, float timeLeft, bool fadeIn) </syntaxhighlight> [[File:HUDMessage-Types-InGame.png|200px|thumb|right|样式一览]] 可选的'''样式'''(type): # 成就 <code>HUDMessage.achievement_type</code> # 新任务 <code>HUDMessage.newQuest_type</code> # 错误 <code>HUDMessage.error_type</code> # 体力值 <code>HUDMessage.stamina_type</code> # 生命值 <code>HUDMessage.health_type</code> '''颜色'''(color): 第1、2个构造器并没有给出表示颜色的参数,此时颜色默认为<code>Color.OrangeRed</code>。 若使用第4个构造器,颜色则与游戏内文字颜色一样。 特别: * ''public HUDMessage(string type, int number, bool add, Color color, Item messageSubject = null);'' 支持消息内容扩展。常用于金钱相关。 * ''public HUDMessage(string message, string leaveMeNull);'' 左侧没有图标框。 * ''public HUDMessage(string message, Color color, float timeLeft, bool fadeIn);'' 文字渐入效果。 示例1: 弹出一个带有[[File:Error-image-ingame.png]]的消息框。 <syntaxhighlight lang='c#'> Game1.addHUDMessage(new HUDMessage("MESSAGE", 3)); </syntaxhighlight> 示例2: 弹出一个不带纯文字的消息框。 <syntaxhighlight lang='c#'> Game1.addHUDMessage(new HUDMessage("MESSAGE", "")); // second parameter is the 'leaveMeNull' parameter </syntaxhighlight> [[en:Modding:Common tasks]] [[ru:Модификации:Основные возможности]]
该页面使用的模板:
Template:Key
(
查看源代码
)
Template:Stub
(
查看源代码
)
返回至
模组:常用方法
。