Godot 官方2D C#重构(1):雪花碰撞

前言

Godot 官方 教程
Godot 2d 官方案例C#重构 专栏
Godot 2d 重构 github地址

实现效果

难点介绍

Godot GDScript和C# 对应关系大部分靠猜

文件导入

  • 资源地址:默认为res://开头
  • 2D贴图导入类型:Texture2D
csharp 复制代码
public Texture2D Bullet_Image = new Texture2D();
Bullet_Image = GD.Load<Texture2D>("res://bullet.png");

2D属性赋值,PhysicsServer

PhysicsServer 用于给赋值属性和属性初始化

csharp 复制代码
Shape = PhysicsServer2D.CircleShapeCreate();
PhysicsServer2D.ShapeSetData(Shape,8);

刷新UI

在_Process里面调用QueueRedraw

csharp 复制代码
public override void _Process(double delta)
{
	QueueRedraw();
}

绘制UI

生成2D贴图

csharp 复制代码
DrawTexture(Texture2D img, Vector2 position);

重载_Draw函数

csharp 复制代码
    public override void _Draw()
    {
		var offset = -Bullet_Image.GetSize();
		//Godot重载了对应的运算符
		offset = offset /2;
		
        foreach (var item in bullets)
		{
			DrawTexture(Bullet_Image, item.position + offset);
		}
        base._Draw();
    }

离开页面销毁

销毁

csharp 复制代码
PhysicsServer2D.FreeRid(item.body);

离开页面销毁

csharp 复制代码
    public override void _ExitTree()
    {
		foreach (var item in bullets)
		{
			PhysicsServer2D.FreeRid(item.body);
		}
		PhysicsServer2D.FreeRid(Shape);
        base._ExitTree();
    }

定位到鼠标位置

重载 _Input函数,获取鼠标参数

csharp 复制代码
public override void _Input(InputEvent @event)
{
	if(@event is InputEventMouseMotion)
	{
		var mouseEvent = (@event as InputEventMouseMotion);
		Position = mouseEvent.Position;
	}
    base._Input(@event);
}
相关推荐
yi碗汤园1 小时前
【一文了解】C#反射
开发语言·unity·c#
T.Ree.3 小时前
汇编_读写内存
开发语言·汇编·c#
czhc11400756633 小时前
C#1114 枚举
开发语言·c#
曹牧4 小时前
C#中,GetValueOrDefault方法
c#
云雾J视界5 小时前
碳中和终极武器——嵌入式AI重构能源管理战局
人工智能·重构·算力·碳中和·能源管理·嵌入式ai·低功耗硬件
ZKNOW甄知科技5 小时前
重构企业运维智慧:低代码 ITSM 知识管理平台的创新与实践
大数据·运维·人工智能·程序人生·低代码·重构·it
RPA 机器人就找八爪鱼7 小时前
RPA 重构财务新生态:自动化驱动的转型革命
重构·自动化·rpa
SunnyDays10118 小时前
使用 C# 实现 Excel 与 DataTable 相互转换
c#·excel转datatable·datatable转excel
碰大点9 小时前
数据库“Driver not loaded“错误,单例模式重构方案
数据库·sql·qt·单例模式·重构
獨枭17 小时前
C# 本地项目引用失效与恢复全攻略
开发语言·c#·visual studio