上一篇, 我们讲解了游戏的大概背景, 知道了要做什么内容. 现在已经可以开始搭建游戏的代码框架.
整体架构
我们将整个项目命名为 idlewow, 目前包含以下几个模块 idlewow-facade, idlewow-core, idlewow-hessianserver, idlewow-rms, idlewow-util, idlewow-game, idlewow-doc.
其中, facade 模块是 jar 包项目, 供内部其他项目引用, 主要定义模型类, 接口类. core 模块是 jar 包项目, 实现系统的主要业务逻辑以及对数据库的访问. hessianserver 模块是 war 包项目, 将系统的接口暴露给其他内部项目. rms 模块是 war 包项目, 是后台运营系统或叫资源管理系统, 主要给 GM 使用, 进行配置, rms 模块本身不访问数据库, 而是通过调用 hessian 接口, 与底层数据进行交互. game 模块是 war 包项目, 是 web 形式的游戏客户端, 部分游戏逻辑写在此模块中, 也通过 hessian 接口访问系统数据. util 模块是 jar 包项目, 主要实现一些工具类, 供其它模块引用. doc 项目可有可无, 我拿来提交一些 sql 脚本, 策划表格等内容. 整个项目的架构大致如下图 (其中 REST 模块目前并没有, 仅占位示意):
项目架构图
操作步骤
1. 新建项目
打开 Idea, 左上角菜单 File --> New --> Project, 选择 Maven 项目, 不使用模板, GroupId,ArtifactId 和 Projec name 都填写 idlewow 即可, JDK 选择 1.8.(如没有 1.8 版本, 可在前言章节中查看下载地址)
新建项目
新建完毕后, 因为是 maven 项目, IDE 检测到 pom.xml 会在右下角弹出提示, 如下图, 选择 "Enable Auto-Import", 开启自动包下载, 后面修改 pom.xml 后, 按 Ctrl+S 保存即可自动更新包.
maven 提示框
2. 添加模块
idlewow 作为根目录下面不需要写代码, src 文件夹可以删掉. 右键根目录, 选择 New --> Module 来添加模块. 同样选择 maven 项目, 不使用模板. 因为是一个模块, GroupId 默认为 idlewow, 以 core 项目为例, ArtifactId 填写 idlewow-core. 点击下一步, 填写 module name 时, 会默认没有中划线, 需要手动加上, 然后点击完成即可.
然后依次创建各个模块. 完成后如下图:
各项目模块
3. 添加依赖关系
maven 项目通过 pom.xml 管理依赖, 类似于 C# 里添加 DLL 引用, 这里根目录下的 pom.xml 暂时无需改动.
首先, 在各个模块的 pom 里, artifactId 节点后面添加 packaging 节点, 如 < packaging>war</packaging>, 是 war 包项目的填写 war,jar 包项目填写 jar.
在 core 模块中, 添加依赖如下:
- <dependencies>
- <dependency>
- <groupId>idlewow</groupId>
- <artifactId>idlewow-facade</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>idlewow</groupId>
- <artifactId>idlewow-util</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
pom.xml
在 hessian 模块中, 添加依赖如下:
<dependencies> <dependency> <groupId>idlewow</groupId> <artifactId>idlewow-core</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>idlewow</groupId> <artifactId>idlewow-facade</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
pom.xml
在 rms 模块中, 添加依赖如下:
<dependencies> <dependency> <groupId>idlewow</groupId> <artifactId>idlewow-facade</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>idlewow</groupId> <artifactId>idlewow-core</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
pom.xml
注意: 前面的架构图中, rms 通过 hessian 接口访问数据层, 此处应不必添加对 core 的依赖. 但为了开发和部署方便, 实际开发时, 我在 rms 模块直接引用 core 层访问数据.
在 game 模块中, 添加依赖如下:
<dependencies> <dependency> <groupId>idlewow</groupId> <artifactId>idlewow-facade</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
pom.xml
其他模块目前无需添加依赖.
4. 构造 Web 目录
因为有 3 个模块需要打成 war 包, 因此必须先构造 Web 项目的目录结构, 否则打包命令无法成功执行.
在 game, hessian, rms 模块的 src 目录下分别创建 webapp/Web-INF 文件夹, 并在该目录下添加 Web.xml(空文件即可). 完成后目录结构如下图:
文件结构
另外, 因为不做单元测试, 所有 src 目录下的 test 文件夹均可删除.
5. 编译打包
在左上角菜单选择 "Run --> Edit Configurations"(快捷按钮里直接点也可以), 点击左上角绿色的加号, 选择 Maven(代表使用 maven 命令或插件来编译, 打包, 启动项目). 然后填写名称 "idlewow 打包", 选择根目录, 填写执行命令 "clean install",
其中, clean 表示清空之前的编译目录, install 表示把项目编译, 打包, 并更新到本地 maven 仓库.
其实编译命令是 compile, 打包命令 package,maven 命令有一个优先级, 执行 install 时, 会自动执行 compile, package 等等.
如下图:
点击 OK 后, 我们执行一遍. 结果如下图, 各模块均编译打包成功.
结语
本章简要搭建整个系统的框架, 构造了各模块的文件结构, 并成功编译打包.
源码下载地址: https://idlestudio.ctfile.com/fs/14960372-383321231
后面将开始对各模块的内部框架进行搭建.
1 元催更
来源: https://www.cnblogs.com/lyosaki88/p/11041698.html