Godot模拟实现多人游戏平滑移动

最近几天接触到Godot,发现是一个很不错的免费开源游戏引擎,游戏本身实现了多人对战游戏之间同步功能(好像更适合于即时对战类,而且对战人数对计算机的性能要求高);

如果要实现一个大型的游戏,1个服务端(长期运行)对应N多个客户端(随时运行)需要怎么实现,不知道是不是我接触Godot时间太短,暂时没找到解决方案。我觉得首先需要解决数据同步中的平滑移动 ,让游家体验不出现卡顿现象,实现平滑移动 前提条件就是目标位置是可预测 的;这里分享下我实现的多人游戏之间的平滑移动的思路,以下是模拟实现(第一次接触游戏类的开发,各位见笑了)。

看最终效果:

接下是功能的实现:

Sprite2DRemote:远程节点(模拟服务端)

Timer: 定时器间隔:0.5秒,用于生成随机增加的旋转度数(模拟用户提交的状态数据);

绑定的脚本内容如下:

复制代码
extends Sprite2D

var rng = RandomNumberGenerator.new()

var customTimer
var customRotation
func _ready(): 
	rng.randomize()

	# custom
	customRotation = rotation
	customTimer = get_node("Timer")
	customTimer.connect("timeout",_custom_timer_timeout,CONNECT_PERSIST)

func _process(delta):
	rotation_degrees = customRotation

func _custom_timer_timeout():
	var newRotation = customRotation + (rng.randi() % 20)
	customRotation = int(newRotation) % 360

Sprite2DLocal: 本地节点 (模拟客户端)

Timer: 定时器间隔:0.5秒,用于同步**Sprite2DRemote(模拟服务端)**旋转度数;

Sprite2DLocal1: 本地节点 (模拟客户端)

Timer: 定时器间隔:1秒,用于同步**Sprite2DRemote(模拟服务端)**旋转度数;

Sprite2DLocal2: 本地节点 (模拟客户端)

Timer: 定时器间隔:2秒,用于同步**Sprite2DRemote(模拟服务端)**旋转度数;

Sprite2DLocal、Sprite2DLoca1、Sprite2DLocal2的脚本文件绑定的是同一个文件,内容如下:

复制代码
extends Sprite2D

var sprite2DRemote
var sprite2Dlocal

var customTimer
var customRotation

func _ready(): 
	# remote
	sprite2DRemote = get_node("../Sprite2DRemote")
	sprite2Dlocal = get_node(".")
	
	# custom
	customRotation = sprite2DRemote.rotation_degrees
	customTimer = get_node("Timer")
	customTimer.connect("timeout",_custom_timer_timeout,CONNECT_PERSIST)
	

func _process(delta): 
	var localRotation = rotation_degrees
	if (localRotation==customRotation):
		return
	
	# calc diff
	var diffRotation
	var sumCustomRotation
	if (customRotation<localRotation):
		sumCustomRotation = 360+customRotation
		diffRotation = sumCustomRotation-localRotation
	else:
		diffRotation = customRotation-localRotation
		sumCustomRotation = customRotation
		

	# fix range
	localRotation += diffRotation*delta
	if (localRotation>=sumCustomRotation):
		localRotation = customRotation
	if (localRotation >= 360):
		localRotation = int(localRotation) % 360
		
	#
	# print(sprite2Dlocal.get_name(),": _process() ", localRotation)
	rotation_degrees = localRotation

func _custom_timer_timeout():
	customRotation = sprite2DRemote.rotation_degrees 
	# print(sprite2Dlocal.get_name(),": timeout() ", customRotation)
相关推荐
王家视频教程图书馆39 分钟前
2025年最新 unityHub游戏引擎开发2d手机游戏和桌面游戏教程
游戏·unity·游戏引擎
点金石游戏出海4 小时前
每周资讯 | 中国游戏市场将在2025年突破500亿美元;《恋与深空》收入突破50亿元
游戏·网易游戏·海外市场·恋与深空·手游市场
一点都不方女士5 小时前
《无畏契约》游戏报错“缺少DirectX”?5种解决方案(附DirectX修复工具)
windows·游戏·microsoft·动态链接库·directx·运行库
wanhengidc18 小时前
云手机可以息屏挂手游吗?
运维·网络·安全·游戏·智能手机
wanhengidc1 天前
云手机的空间会占用本地内存吗
科技·游戏·智能手机
wanhengidc1 天前
网页版的云手机都有哪些优势?
运维·网络·安全·游戏·智能手机
wanhengidc2 天前
云手机运行是否消耗自身流量?
运维·科技·安全·游戏·智能手机
wanhengidc2 天前
云手机将要面临的挑战有哪些?
运维·网络·安全·游戏·智能手机
gou123412342 天前
Golang之GoWorld深度解析:基于Go语言的分布式游戏服务器框架
分布式·游戏·golang
金山电脑医生2 天前
DirectX修复必备指南:解决DLL缺失与游戏崩溃的5种方法
游戏·dll丢失·dll缺失