Hi everyone, I’m happy to join the community!
I want to create my first 2d game in Panda3d, also it’s a good oportunity to practise my C++ skills, so that’s why I decided to Panda.
I’ve met some first issues, that I probably don’t understand…
I’ve created a 2d map using the CardMaker, it’s a png file with 2048x2048pixels map.
In the same way I’ve created a hero.
Now I want to create a camera which will follow the hero and will show some limited part of map (let’s say 1/3 of map), but I can’t achive that.
Can you help me with understanding what I’m doing wrong?
Code:
Map initialization.
void
InitMap(PandaFramework& framework, WindowFramework* window)
{
PT(Texture)
mapTexture = TexturePool::load_texture(MAP_PATH);
if (!mapTexture) {
std::cerr << "Failed to load map texture!" << std::endl;
return;
}
CardMaker cm("map-card");
cm.set_frame(-1.0f, 1.0f, -1.0f, 1.0f);
mapNode = window->get_render_2d().attach_new_node(cm.generate());
mapNode.set_texture(mapTexture);
mapNode.set_scale(1.0f, 1.0f, 1.0f);
}
Hero
static NodePath player;
static void
SetPlayerTexture(WindowFramework* window)
{
PT(Texture)
heroTexture = TexturePool::load_texture(PLAYER_PATH);
if (!heroTexture) {
/** @todo: Error handling. */
std::cerr << "Failed to load hero texture!" << std::endl;
return;
}
CardMaker cmHero("hero-card");
static constexpr float HERO_SIZE{ 128.0f / 2048.0f };
cmHero.set_frame(-HERO_SIZE, HERO_SIZE, -HERO_SIZE, HERO_SIZE);
player = window->get_render_2d().attach_new_node(cmHero.generate());
player.set_texture(heroTexture);
player.set_scale(1, 1, 1);
player.set_transparency(TransparencyAttrib::M_alpha);
}
static void
SetupPlayerCamera(WindowFramework* window)
{
Camera* camera = window->get_camera(0);
PT(OrthographicLens) lens = new OrthographicLens();
lens->set_film_size(0.3f, 0.3f);
lens->set_near_far(-1000, 1000);
camera->set_lens(lens);
NodePath cameraNP = window->get_camera_group();
cameraNP.reparent_to(player);
cameraNP.set_pos(player.get_pos().get_x(), 0, player.get_pos().get_z());
AsyncTaskManager::get_global_ptr()->add(
new GenericAsyncTask("Update Camera Task", UpdateCamera, window));
}
AsyncTask::DoneStatus
UpdateCamera(GenericAsyncTask* task, void* data)
{
WindowFramework* window = static_cast<WindowFramework*>(data);
LPoint3f hero_pos = player.get_pos();
NodePath cameraNP = window->get_camera_group();
cameraNP.set_pos(hero_pos.get_x(), 0, hero_pos.get_z());
return AsyncTask::DS_cont;
}
void
InitPlayer(PandaFramework& framework, WindowFramework* window)
{
SetPlayerTexture(window);
RegisterKeys(framework);
framework.get_task_mgr().add(
new GenericAsyncTask("UpdatePlayerTask", UpdatePlayerTask, (void*)window));
SetupPlayerCamera(window);
}
Main
int
main(int argc, char* argv[])
{
PandaFramework framework;
framework.open_framework(argc, argv);
framework.set_window_title("Panda Game");
WindowFramework* window = framework.open_window();
window->enable_keyboard();
InitMap(framework, window);
InitPlayer(framework, window);
framework.main_loop();
framework.close_framework();
return 0;
}