如何在c++主机游戏中呈现世界和动态对象
How do I represent a world and dynamic objects in a C++ console game?
如果你能帮助我完成一个小项目,我将非常感激。它应该是一款主机游戏,使用了roguelike, rpg,沙盒和生存游戏的元素。
到目前为止,我已经尝试了大约三次来获得一个工作架构。每一次,我遇到的问题都是我无法解决的,除非我想办法绕过它。
我已经看了很多博客和教程,但没有一个完全符合我的目的。
1) http://trystans.blogspot.co.at/2011/08/roguelike-tutorial-01-java-eclipse.html
2) http://www.kathekonta.com/rlguide/index.html
我已经关注了Trystan的博客——他使用Java——并在Java和AS3中重现了他的设计。
然而,我对他把课程联系在一起的方式不太满意。像World.dig()这样的命令-对我来说,调用Player.dig()更有意义-或者将地图表示为3d(!)数组使我相信一定有更好的方法来做到这一点。
我发布的第二个链接是用c++编写的,然而,只有一个中心地图。我的目标是能够有多个地图,我可以输入字符等
我将把我最新尝试的代码贴在下面。它根本没有任何移动或交互功能,因为我想从一开始就把设计做好,而不是在项目投入数小时后失败(我已经经常遇到这种情况)。
main.cpp:
# include <iostream>
# include "World.h"
# include "Entity.h"
using namespace std;
int main(){
int testFloor[] = {
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 1, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 0, 0
};
World world(testFloor, 5, 5);
//Creating an entity at the top left corner
Entity nine(9, 0);
world.addEntity(nine);
//Draw the entire world
world.draw(0,0,5,5);
system("PAUSE");
return 0;
}
World.h:
# ifndef WORLD_H
# define WORLD_H
# include <vector>
# include "Entity.h"
using namespace std;
class World {
private:
int* _floor;
int _width;
int _height;
vector<Entity> _entities;
public:
World(int* floor, int width, int height) :
_floor(floor), _width(width), _height(height){}
/*Pushes an Entity to the _entities-vector*/
void addEntity(Entity e){
_entities.push_back(e);
}
/*Displays the floor at a position, or the glyph of an entity, if applicable*/
//This "glyph" is just a number here, similar to the contents of the _floor array
int glyph(int ID){
int out = _floor[ID];
for(Entity e : _entities){
if(e.pos() == ID){
out = e.glyph();
break;
}
}
return out;
}
//Prints the World as seen from "above" - entities will cover the floor
void draw(int left, int top, int width, int height){
system("cls");
int ID;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
ID = (x+left)+(y+top)*width;
cout << glyph(ID);
}
cout << endl;
}
}
};
# endif
Entity.h:
# ifndef ENTITY_H
# define ENTITY_H
class Entity{
private:
int _glyph; //Will be a char later, integer for now, since the glyph function returns
//an int for simplicity
int _pos;
public:
Entity(int glyph, int pos) : _glyph(glyph), _pos(pos) {}
int glyph(){return _glyph;}
int pos(){return _pos;}
};
# endif
代码可以编译并运行。在左上角生成一个新的实体,用"9"表示,地图按预期绘制……但我现在就看到问题了。
1)当我创建一个新的实体时,我需要给它ID(世界位置)来生成-但是它没有关于它在世界中的实际位置的线索。
2)当实体移动时,它需要一个指向它所在地图的引用,以便知道它要去哪里。
要解决这些问题,在实体中存储对世界的引用可能是有益的。然而,我希望能够执行像World.update()这样的命令,它会自动更新所有实体,让它们有机会移动、攻击、开门、建造路障等等。然后遍历所有实体,调用它们各自的更新函数。
我从之前的尝试中发现,在实体类中存储对世界的引用,并在世界类中存储实体的数组/向量会导致问题。
以下是我的实际问题:-我如何设计我的类,以便在实体和世界之间有一个有意义的关系?
-我把我的引用放在哪里-直接到函数调用,也许(这将成本性能,我猜)?
编辑:请注意,我不期望代码样本,我宁愿自己写代码。对我最有帮助的是提示,比如哪个类应该保存哪些变量。
感谢大家的宝贵时间!
1)如果我没有理解错你的问题,为了从你的一维坐标找到你在地图上的二维位置,你可以使用下面的东西:
Point Entity::pos2D(World& world) const {
int y = _pos / world.width();
int x = _pos % world.width();
return Point(x, y);
}
假设你有一个Point类来聚合x和y。同时在这里为World对象添加width()和height()访问器函数。
你可能会发现,最好先存储一个2d的位置——从它计算1d的位置需要乘法而不是除法。
2)您可以在各种实体操作成员函数中传递对世界的引用。例如
Entity::update(World& world) {
// do some stuff
}
Entity::move(World& world) {
// do some stuff
}
这将节省在每个实体中存储一个指向世界的指针。这将减少内存使用,并提供更好的引用局部性。例如,如果你有很多实体,那么在处理器缓存中拥有更完整的实体可能会提高性能,这取决于它们是如何存储的。但是,在这个阶段,我不会太担心性能。
- 激励'inline'说明符的真实世界示例?
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- C++中的动态铸造故障
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 使用Unreal C++获取VR耳机的世界位置/方向
- 内联映射初始化的动态atexit析构函数崩溃
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 控制允许动态运行c++的并发操作数
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 输出没有重复元素的动态数组(收缩数组)C++
- C++为线程工作动态地分割例程
- 正在插入动态数组
- 在c++中使用动态分配的问题
- C++中的动态对象与非动态对象
- 如何在动态数组上使用搜索函数
- 在没有动态内存的世界中,我是否需要虚拟析构函数?
- Box2d SDL无法在动态实体上应用世界设置
- 如何在c++主机游戏中呈现世界和动态对象