当前位置: 首页 > news >正文

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

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

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

看最终效果:

接下是功能的实现:

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

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

绑定的脚本内容如下:

extends Sprite2Dvar rng = RandomNumberGenerator.new()var customTimer
var customRotation
func _ready(): rng.randomize()# customcustomRotation = rotationcustomTimer = get_node("Timer")customTimer.connect("timeout",_custom_timer_timeout,CONNECT_PERSIST)func _process(delta):rotation_degrees = customRotationfunc _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 Sprite2Dvar sprite2DRemote
var sprite2Dlocalvar customTimer
var customRotationfunc _ready(): # remotesprite2DRemote = get_node("../Sprite2DRemote")sprite2Dlocal = get_node(".")# customcustomRotation = sprite2DRemote.rotation_degreescustomTimer = get_node("Timer")customTimer.connect("timeout",_custom_timer_timeout,CONNECT_PERSIST)func _process(delta): var localRotation = rotation_degreesif (localRotation==customRotation):return# calc diffvar diffRotationvar sumCustomRotationif (customRotation<localRotation):sumCustomRotation = 360+customRotationdiffRotation = sumCustomRotation-localRotationelse:diffRotation = customRotation-localRotationsumCustomRotation = customRotation# fix rangelocalRotation += diffRotation*deltaif (localRotation>=sumCustomRotation):localRotation = customRotationif (localRotation >= 360):localRotation = int(localRotation) % 360## print(sprite2Dlocal.get_name(),": _process() ", localRotation)rotation_degrees = localRotationfunc _custom_timer_timeout():customRotation = sprite2DRemote.rotation_degrees # print(sprite2Dlocal.get_name(),": timeout() ", customRotation)

相关文章:

  • SpringData基础学习
  • vue前端更新后需要清空缓存
  • MySQL中 EXPLAIN 的使用介绍
  • oracle共享池(shared pool):一、工作原理、组成部分 二、软硬解析过程
  • 边界dp注意重叠边界
  • Java使用Tesseract进行OCR图片文字识别
  • 老师是怎么分班的?用什么工具比较好?
  • 实战OpenCV之绘制图形
  • JVM 在GC 时的根对象都有那些
  • day_49
  • 代码断点调试
  • LLM 直接偏好优化(DPO)的一些研究
  • springboot框架中filter过滤器的urlPatterns的匹配源码
  • Oracle(81)如何生成AWR报告?
  • 链动 2+1 模式小程序 AI 智能名片商城源码培训邀约策略研究
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • canvas 高仿 Apple Watch 表盘
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • ES学习笔记(12)--Symbol
  • Hibernate最全面试题
  • java小心机(3)| 浅析finalize()
  • Less 日常用法
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • React Transition Group -- Transition 组件
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • Vue实战(四)登录/注册页的实现
  • 编写高质量JavaScript代码之并发
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 使用 @font-face
  • 大数据全解:定义、价值及挑战
  • #QT(一种朴素的计算器实现方法)
  • (42)STM32——LCD显示屏实验笔记
  • (8)STL算法之替换
  • (AngularJS)Angular 控制器之间通信初探
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (笔试题)合法字符串
  • (分布式缓存)Redis持久化
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (三十)Flask之wtforms库【剖析源码上篇】
  • (图)IntelliTrace Tools 跟踪云端程序
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .net core 6 集成 elasticsearch 并 使用分词器
  • .NET Framework 服务实现监控可观测性最佳实践
  • .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化
  • .NET8 动态添加定时任务(CRON Expression, Whatever)
  • /etc/X11/xorg.conf 文件被误改后进不了图形化界面
  • [20150321]索引空块的问题.txt
  • [30期] 我的学习方法
  • [7] CUDA之常量内存与纹理内存
  • [Armbian] 部署Docker版Home Assistent,安装HACS并连接米家设备
  • [BZOJ 1040] 骑士
  • [C#]winform制作圆形进度条好用的圆环圆形进度条控件和使用方法