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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

前言

Godot 官方 教程

Godot 2d 官方案例C#重构 专栏

Godot 2d 重构 github地址

实现效果

在这里插入图片描述

难点介绍

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

文件导入

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

2D属性赋值PhysicsServer

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

Shape = PhysicsServer2D.CircleShapeCreate();
PhysicsServer2D.ShapeSetData(Shape,8);

刷新UI

在_Process里面调用QueueRedraw

public override void _Process(double delta)
{
	QueueRedraw();
}

绘制UI

生成2D贴图

DrawTexture(Texture2D img, Vector2 position);

重载_Draw函数

    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();
    }

离开页面销毁

销毁

PhysicsServer2D.FreeRid(item.body);

离开页面销毁

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

定位到鼠标位置

重载 _Input函数获取鼠标参数

public override void _Input(InputEvent @event)
{
	if(@event is InputEventMouseMotion)
	{
		var mouseEvent = (@event as InputEventMouseMotion);
		Position = mouseEvent.Position;
	}
    base._Input(@event);
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: go