编辑“︁
Stendhal NPC Coding
”︁
跳转到导航
跳转到搜索
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
{{Navigation for Stendhal Top|Contributing}} {{Navigation for Stendhal Extenders}} {{Stendhal NPCs}} Usually we add NPCs (non-player characters) to make world more alive and to use them in Quests. This is how NPCs are added to the world. A java file should define the path they walk on, basic dialog and how they look. It can also define the selling, buying, healing or producing behaviour of an NPC. The NPC is only loaded into a zone if you also edit the zone's xml file, to configure the zone using that java file. More complicated dialog for NPCs, such as you'd find in a quest, is covered in [[Stendhal Quest Coding]]. But the basics for any NPC used in a quest should still be written as below. == Before you start == This page describes how to code an NPC. You don't need to know a lot about Java. You should, however, already have [[Configure a development environment (IDE)|setup an IDE]] and be able to compile and start a local Stendhal server. == Define NPC with Java == First you need to decide what region your NPC will be in, so that we can create the Java file in the correct place. The location for the file will be: src/games/stendhal/server/maps/''region''/''subregion'' You should name the NPC file after the function of the NPC. Examples are GreeterNPC, HealerNPC, BuyerNPC, ChefNPC, SellerNPC, LifeguardNPC In this example we'll make a wizard in a magician's hut. So we will create a file <code>src/games/stendhal/server/maps/ados/magician_house/WizardNPC.java</code> The start of the file will need to specify some standard things to make it work, don't worry about these just copy them for now. Notice that the ''class'' is the same as our ''filename'' and the ''package'' is related to where we put the file. <source lang="java"> package games.stendhal.server.maps.ados.magician_house; import games.stendhal.server.core.config.ZoneConfigurator; import games.stendhal.server.core.engine.SingletonRepository; import games.stendhal.server.core.engine.StendhalRPZone; import games.stendhal.server.core.pathfinder.FixedPath; import games.stendhal.server.core.pathfinder.Node; // this one is just because our NPC is a seller import games.stendhal.server.entity.npc.ShopList; import games.stendhal.server.entity.npc.SpeakerNPC; // this one is just because our NPC is a seller import games.stendhal.server.entity.npc.behaviour.adder.SellerAdder; // this one is just because our NPC is a seller import games.stendhal.server.entity.npc.behaviour.impl.SellerBehaviour; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Map; public class WizardNPC implements ZoneConfigurator { // this is just because he has a 'shop' to sell potions private final ShopList shops = SingletonRepository.getShopList(); /** * Configure a zone. * * @param zone The zone to be configured. * @param attributes Configuration attributes. */ public void configureZone(final StendhalRPZone zone, final Map<String, String> attributes) { buildNPC(zone); } </source> Now we have set up this standard stuff we can build the actual NPC. We'll call the method which builds the NPC, ''buildNPC''. <source lang="java"> private void buildNPC(final StendhalRPZone zone) { final SpeakerNPC npc = new SpeakerNPC("Mr Healer") { protected void createPath() { List<Node> nodes=new LinkedList<Node>(); nodes.add(new Path.Node(9,5)); nodes.add(new Path.Node(14,5)); setPath(nodes,true); } protected void createDialog() { // Lets the NPC reply with "Hallo" when a player greets him. But we could have set a custom greeting inside the () addGreeting(); // Lets the NPC reply when a player says "job" addJob("I have healing abilities and I heal wounded players. I also sell #potions and antidotes."); // Lets the NPC reply when a player asks for help addHelp("Ask me to #heal you and I will help you or ask me #offer and I will show my shop's stuff."); // Makes the NPC sell potions and antidote addSeller(new SellerBehaviour(shops.get("healing"))); // Lets the NPC heal players for free addHealer(0); // respond about a special trigger word addReply("potions","Please ask for my #offer."); // use standard goodbye, but you can also set one inside the () addGoodbye(); } }); // This determines how the NPC will look like. welcomernpc.png is a picture in data/sprites/npc/ npc.setEntityClass("welcomernpc"); // set a description for when a player does 'Look' npc.setDescription("You see Mr Healer, he looks a a bit busy at the moment but perhaps he can help you anyway."); // Set the initial position to be the first node on the Path you defined above. npc.setPosition(9, 5); npc.initHP(100); zone.add(npc); } } </source> The first thing defined is the name. The NPC is added to a list of NPCs so you can later fetch it for adding more dialogues for quests. So, it is very important to make sure the name you give to your NPC is unique. Next define a path that the NPC will follow on that area. Just used your tiled map or walk around in game and check coordinates to choose your nodes. Each node is where the NPC turns. Make sure you right down every turning point and don't miss one (don't go from one corner, diagonally to another corner.) You can also make the NPC stand still by using this instead: <source lang="java"> protected void createPath() { // NPC does not move setPath(null); } </source> In that case your NPC will stand still at whatever point you set as the initial position with <code>npc.setPosition(x, y);</code>. And then create a dialog. Dialogs and such are explained in Quest sections, but you should find the example above covers most options and the options like addHelp are quite self-explanatory. There are: :addGreeting :addOffer :addHelp :addJob :addQuest :addGoodbye :addReply ''specify a '''trigger''' and a '''reply''''' We set its outfit either by: * setting its class to a [[StendhalRefactoringGraphics#NPCs|PNG image]] that exists at ''data/sprites/npc'', e.g. <code>npc.setEntityClass("welcomernpc");</code> * setting its outfit with setOutfit method e.g. <code>npc.setOutfit(new Outfit(0, 05, 01, 06, 01));</code> - see [http://stendhalgame.org/hudson/job/stendhal_HEAD/javadoc/games/stendhal/server/entity/Outfit.html Outfit javadoc] It is nice to set a description for when the player does ''Look''. If you don't set one, it will just say: ''You see [NPC Name].'' Finally set its initial position and its HP. Don't worry for your NPC. It can't be attacked nor killed. Once that is done add the NPC to zone using ''zone.add()'' method. == Configure zone xml == We need to tell a zone to load this configuration class file, so the NPC is added to the world somewhere. The NPC in the example lives in the ados area, so the information is stored in data/conf/zones/ados.xml. The map she's on is called int_ados_magician_house, so the information is inside the <zone name="int_ados_magician_house" file="interiors/ados/magician_house.tmx"> </zone> section. To load the java class file you created just add the line <source lang="xml"> <configurator class-name="games.stendhal.server.maps.ados.magician_house.WizardNPC" /> </source> right after the <zone ... tmx"> Check the location of the file matches the path set in the class name!
摘要:
请注意,所有对gamedev的贡献均可能会被其他贡献者编辑、修改或删除。如果您不希望您的文字作品被随意编辑,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源(详情请见
Gamedev:著作权
)。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
该页面使用的模板:
Template:Br
(
编辑
)
Template:Navigation for Stendhal Extenders
(
编辑
)
Template:Navigation for Stendhal Top
(
编辑
)
Template:Stendhal NPCs
(
编辑
)
Template:Stendhal menu for Builders and Hosters
(
编辑
)
Template:Stendhal menu for Contributors
(
编辑
)
Template:Stendhal menu for Developers
(
编辑
)
Template:Stendhal menu for Extenders
(
编辑
)
Template:Stendhal menu for GMs
(
编辑
)
Template:Stendhal menu for Players
(
编辑
)
Template:Stendhal menu for World
(
编辑
)
导航菜单
个人工具
未登录
讨论
贡献
创建账号
登录
命名空间
页面
讨论
不转换
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
臺灣正體
查看
阅读
编辑
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息