java码农是否过多,是否有很多类会降低Java的性能?
发布日期:2022-02-19 00:02:18 浏览次数:6 分类:技术文章

本文共 2398 字,大约阅读时间需要 7 分钟。

So I'm trying to make a CCG type of game using javafx (similar to Hearthstone), and I was wondering if it's a very bad idea to have one class for each card, because I've done some research and most fan made card games of Hearthstone use JSON to get the card objects. For example if the game only had 4 cards with the names:

DragonMaster

Vulcan

Pirate

Chuck Norris

I'd have 4 classes, with the names respectively. Now I though this was necessary since every card would have its own behaviour, meaning one card might do something when summoned, and in this case I have a method that gets called for that action, while others might have other methods or none at all.

Now as you can probably tell, if you have 100 cards in the game, the number of classes also gets big (and that's just for the cards package, since I'm using the MVC model, the project got really big without me realizing it).

Some other information:

All concrete card classes (e.g. Vulcan) extend an abstract Card class that contains fields such as name and cost.

Inside the cards package in the model, I have two other packages before having the actual classes, one for minions and another one for spells.

Also, If I don't have a class for each card, how can I instantiate a new specific card to add to the player's hand? Is there a different solution to this "class abundance" problem, and does this actually affect performance when the game is running or even space in the jar file?

If someone could anwser my question or any kind of help I'd be very appreciated.

解决方案

Having thousands of classes will not impact performance in any noticeable way. You can even pre-compile them ahead of time or cache them.

That said, many game engines are using a entity-component approach to represent game entities and keep things like effects as different functions.

This way you can combine different effects cards have in ways that can't be done with single-inheritance.

Below is a link that describes the approach.

You could adapt this gradually.

For example, your cards could be just one type of entity.

class Card {

int mana;

String name;

Effect[] effects;

Trigger[] triggers;

}

Effects could be pieces of code that act on the playfield and the current card.

interface Effect {

void affect(Playfield pf);

}

Triggers could be event listeners relevant to the card.

I.e. there could be a trigger that fires when the opponent plays a card.

etc.

转载地址:https://blog.csdn.net/weixin_34236334/article/details/118845673 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:放大镜旋转 css,CSS放大镜(增加缩放级别)
下一篇:ajax检测超时,jquery – 使用AJAX时检查会话超时

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月21日 01时25分17秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

SpringBoot 结合 JSR303 对前端数据进行校验 2019-04-27
SpringBoot 整合 MongoDB 之 MongoTemplate 实现 CRUD、分页接口 2019-04-27
[增删改查] SpringBoot 整合 Solr 之 SolrClient 实现 CRUD、分页接口、高亮显示 2019-04-27
[Python爬虫] 模拟浏览器、代理ip、开启日志、超时处理、异常处理、登录、下载图片 2019-04-27
在 SpringBoot 中使用 @EnableAsync、@Async 轻松实现异步任务 2019-04-27
《学习 Go 语言》学习心得 2019-04-27
[汇编语言] 带有颜色的字符串显示(hello world 级别程序) 2019-04-27
[增删改查] Python 之使用 Django + LayUI 做后台管理 2019-04-27
Docker 镜像容器 之 导出导入、上传镜像到 DockerHub 上、Nexus私库 的引入 2019-04-27
centos7 下将 Django2.0 项目部署到 阿里云 上(uwsgi3 +Nginx ) 2019-04-27
前后端分离 SpringBoot + SpringSecurity + JWT + RBAC 实现用户无状态请求验证 2019-04-27
[Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息 2019-04-27
在 Centos7 下使用 Docker 快速搭建 Hadoop 集群 2019-04-27
Python web 框架 Flask 蓝图的正确使用姿势 2019-04-27
领扣LintCode算法问题答案-1053. 至少是其他数字两倍的最大数 2019-04-27
领扣LintCode算法问题答案-1054. 最少费用的爬台阶方法 2019-04-27
领扣LintCode算法问题答案-1056. 请找出大于目标的最小字母 2019-04-27
领扣LintCode算法问题答案-1062. 洪水填充 2019-04-27
领扣LintCode算法问题答案-1068. 寻找数组的中心索引 2019-04-27
领扣LintCode算法问题答案-1071. 词典中最长的单词 2019-04-27