跳至正文

简单绘制

你们好,我是papyrus,也可以叫我小天使(

我主要是来搬运旧教程的内容的,以后也许有可能会更新转1.4的教程

世界坐标与屏幕坐标

要来了解屏幕坐标与世界坐标的区别

世界坐标是整个世界的左上角(就是你到世界左边后再飞到顶上的那个点),玩家和npc的位置都是基于世界坐标的,但是屏幕坐标是基于屏幕左上角的,那么绘制的时候,我们需要把世界坐标转换成为屏幕坐标

我们需要把世界坐标转换成屏幕坐标,那么我们会用到这个

Vector2 screenPosition;

那么转换就只用减去 Main.screenPosition(这个是屏幕在世界的坐标),减去后就会得到屏幕坐标了

绘制流程

一定不要在没有SpriteBatch为参数的重写函数里写绘制函数!!!

以npc的Draw举例子

注意那个SpriteBatch,SpriteBatch是一个绘制接口,让你可以在屏幕上绘制一些图案,但是使用要求较为苛刻,所以初学者不要在其他没有SpriteBatch参数的重写函数

那么在这个ModNPC里面,可以供你正常更改的重写函数就这两个

PreDraw会在npc自带的Draw之前调用,PostDraw是在Draw后调用。PreDraw返回false可以阻止原版Draw的调用

接下来我们就开始绘制(spriteBatch.Draw),比较难用好

spriteBatch.Draw 的重载很多

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth);
public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color);
public void Draw(Texture2D texture, Rectangle destinationRectangle, Color color);
public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color);
public void Draw(Texture2D texture, Vector2 position, Color color);

在这里我推荐初学者使用的函数重载是

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);

在VS中,它的自动补全是第6个重载

那么接下来看看这个参数的用法

Texture2D texuture 是Draw出来的图片,这个图片的格式必须是 Texture2D,获取方法如下

     Texture2D tex = mod.GetTexture("XXX/XXX");//图片所在位置,不需要加mod名字

Vector2 position注意,这是屏幕坐标。图像要被绘制位置的屏幕坐标,在我推荐在这里填写类似XXX.Center-Main.screenPosition 的代码,绘制位置就是那个物体的中心

Rectangle? sourceRectangle,这个参数代表需要绘制被绘制贴图(texuture),的哪一部分,如果全部绘制就是 null,一般情况下是 null,绘制帧图的时候才需要设置其他值

Color color,要把绘制图片染成什么颜色,一般来说 Color.White(不染色)就行了,这个染色系统跟你们认知的染色方式不太一样,不推荐设置为其他值,除非你知道你在做什么

float rotation,图像的旋转弧度

Vector2 origin,大概就是图像从哪个位置开始扩散,从中间开始扩散就填 tex.Size() / 2f,左上角就填 Vector2.Zero

float scale,图像缩放倍数

SpriteEffects effects,图像的翻转,可以水平翻转也可以垂直翻转,有需要的时候会有用的,其他时候 SpriteEffects.None 足矣

最后一个填0,不用管那么多

绘制文字

文字绘制的函数建议使用 Terraria.Utils.DrawBorderStringFourWay() 而不是 spriteBatch.DrawString()

public static void DrawBorderStringFourWay(SpriteBatch sb, DynamicSpriteFont font, string text, float x, float y, Color textColor, Color borderColor, Vector2 origin, float scale = 1);

SpriteBatch sb 是传入Draw的spriteBatch,给它传就是了

要注意的是 DynamicSpriteFont font 这个参数,也就是文字的字体,一般来说设置为 Main.fontMouseText。也就是原版鼠标字体

float xfloat y 是绘制在屏幕上的位置

Color textColorColor borderColor 是字体的颜色,具体什么效果就不展示

《简单绘制》有6个想法

  1. Pingback: TeddyTerri:使用绘制来实现影子拖尾 - 裙中世界

  2. Pingback: TeddyTerri:使用绘制实现影子拖尾 – TeddyTerri's Blog

  3. Pingback: 通过 HookEndpointManager 动态挂钩子 - 裙中世界

发表回复