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);
}
相关推荐
mudtools9 小时前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz15 小时前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫17 小时前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务1 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther1 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间1 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec1 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
Tiger_shl1 天前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#
mudtools1 天前
.NET驾驭Word之力:COM组件二次开发全攻略之连接Word与创建你的第一个自动化文档
后端·c#
王维志1 天前
LiteDB详解
数据库·后端·mongodb·sqlite·c#·json·database