/******************************************************************************* * This file is part of the "Enduro2D" * For conditions of distribution and use, see copyright notice in LICENSE.md * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) ******************************************************************************/ #include "../common.hpp" using namespace e2d; namespace { class game_system final : public ecs::system { public: game_system(gobject_iptr raptor) : raptor_gobj_(raptor) {} ~game_system() noexcept override {} void process(ecs::registry& owner) override { E2D_UNUSED(owner); const keyboard& k = the().keyboard(); if ( k.is_key_just_released(keyboard_key::f12) ) { the().toggle_visible(!the().visible()); } if ( k.is_key_just_released(keyboard_key::escape) ) { the().set_should_close(true); } if ( k.is_key_pressed(keyboard_key::lsuper) && k.is_key_just_released(keyboard_key::enter) ) { the().toggle_fullscreen(!the().fullscreen()); } // use keys R, J, G to start animations if ( raptor_gobj_ ) { if ( k.is_key_just_pressed(keyboard_key::r) ) { auto player = raptor_gobj_->get_component(); if ( player ) { (*player).set_animation(0, "roar") .add_animation(0, "walk", true); } } if ( k.is_key_just_pressed(keyboard_key::j) ) { auto player = raptor_gobj_->get_component(); if ( player ) { (*player).set_animation(0, "jump") .add_animation(0, "walk", true); } } if ( k.is_key_just_pressed(keyboard_key::g) ) { auto player = raptor_gobj_->get_component(); if ( player ) { (*player).set_animation(1, "gun-grab") .add_animation(1, "gun-holster", secf(3.0f)); } } } } private: gobject_iptr raptor_gobj_; }; class camera_system final : public ecs::system { public: void process(ecs::registry& owner) override { owner.for_joined_components( [](const ecs::const_entity&, camera& cam){ if ( !cam.target() ) { cam.viewport( the().real_size()); cam.projection(math::make_orthogonal_lh_matrix4( the().real_size().cast_to(), 0.f, 1000.f)); } }); } }; class game final : public starter::application { public: bool initialize() final { return create_scene() && create_camera() && create_systems(); } private: bool create_scene() { auto spine_raptor = the().load_asset("spine_raptor.json"); auto spine_coin = the().load_asset("spine_coin.json"); auto spine_mat = the().load_asset("spine_material.json"); if ( !spine_raptor || !spine_coin || !spine_mat ) { return false; } auto scene_i = the().instantiate(); scene_i->entity_filler() .component() .component(node::create(scene_i)); node_iptr scene_r = scene_i->get_component().get().node(); auto coin_i = the().instantiate(); coin_i->entity_filler() .component(node::create(coin_i, scene_r)) .component(renderer() .materials({spine_mat})) .component(spine_player(spine_coin) .set_animation(0, "animation", true)); node_iptr coin_n = coin_i->get_component().get().node(); coin_n->scale(v3f(0.25f)); coin_n->translation(v3f{200.0f, 180.0f, 0.0f}); raptor_gobj_ = the().instantiate(); raptor_gobj_->entity_filler() .component(node::create(raptor_gobj_, scene_r)) .component(renderer() .materials({spine_mat})) .component(spine_player(spine_raptor) .set_animation(0, "walk", true)); node_iptr raptor_n = raptor_gobj_->get_component().get().node(); raptor_n->scale(v3f(0.25f)); raptor_n->translation(v3f{-80.f, -100.f, 0.0f}); return true; } bool create_camera() { auto camera_i = the().instantiate(); camera_i->entity_filler() .component(camera() .background({1.f, 0.4f, 0.f, 1.f})) .component(node::create(camera_i)); return true; } bool create_systems() { ecs::registry_filler(the().registry()) .system(world::priority_update, raptor_gobj_) .system(world::priority_pre_render); return true; } private: gobject_iptr raptor_gobj_; }; } int e2d_main(int argc, char *argv[]) { const auto starter_params = starter::parameters( engine::parameters("sample_07", "enduro2d") .timer_params(engine::timer_parameters() .maximal_framerate(100))); modules::initialize(argc, argv, starter_params).start(); modules::shutdown(); return 0; }