From 82a460ec90b4537926f31603219504bce8817ac2 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sun, 27 Nov 2011 04:31:05 +0200 Subject: Improve luaentity sprite functionality (and add some random stuff) --- src/content_cao.cpp | 189 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 143 insertions(+), 46 deletions(-) (limited to 'src/content_cao.cpp') diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 984a216d8..ba8739df7 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -899,8 +899,8 @@ void MobV2CAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc) /*infostream<<"MobV2CAO::addToScene using texture_name="<< m_texture_name<getRootSceneNode(), smgr, -1, v3f(0,0,0), v2f(1,1)); @@ -1281,21 +1281,16 @@ private: float m_yaw; struct LuaEntityProperties *m_prop; SmoothTranslator pos_translator; + // Spritesheet/animation stuff + v2f m_tx_size; + v2s16 m_tx_basepos; + bool m_tx_select_horiz_by_yawpitch; + int m_anim_frame; + int m_anim_num_frames; + float m_anim_framelength; + float m_anim_timer; public: - u8 getType() const - { - return ACTIVEOBJECT_TYPE_LUAENTITY; - } - core::aabbox3d* getSelectionBox() - { - return &m_selection_box; - } - v3f getPosition() - { - return pos_translator.vect_show; - } - CLuaEntityCAO(IGameDef *gamedef): LuaEntityCAO(gamedef), m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.), @@ -1305,11 +1300,52 @@ public: m_velocity(v3f(0,0,0)), m_acceleration(v3f(0,0,0)), m_yaw(0), - m_prop(new LuaEntityProperties) + m_prop(new LuaEntityProperties), + m_tx_size(1,1), + m_tx_basepos(0,0), + m_tx_select_horiz_by_yawpitch(false), + m_anim_frame(0), + m_anim_num_frames(1), + m_anim_framelength(0.2), + m_anim_timer(0) { ClientActiveObject::registerType(getType(), create); } + void initialize(const std::string &data) + { + infostream<<"CLuaEntityCAO: Got init data"<deSerialize(prop_is); + + infostream<<"m_prop: "<dump()<collisionbox; + m_selection_box.MinEdge *= BS; + m_selection_box.MaxEdge *= BS; + + pos_translator.init(m_position); + + m_tx_size.X = 1.0 / m_prop->spritediv.X; + m_tx_size.Y = 1.0 / m_prop->spritediv.Y; + m_tx_basepos.X = m_tx_size.X * m_prop->initial_sprite_basepos.X; + m_tx_basepos.Y = m_tx_size.Y * m_prop->initial_sprite_basepos.Y; + + updateNodePos(); + } + ~CLuaEntityCAO() { delete m_prop; @@ -1320,6 +1356,19 @@ public: return new CLuaEntityCAO(gamedef); } + u8 getType() const + { + return ACTIVEOBJECT_TYPE_LUAENTITY; + } + core::aabbox3d* getSelectionBox() + { + return &m_selection_box; + } + v3f getPosition() + { + return pos_translator.vect_show; + } + void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc) { if(m_meshnode != NULL || m_spritenode != NULL) @@ -1327,7 +1376,7 @@ public: //video::IVideoDriver* driver = smgr->getVideoDriver(); - if(m_prop->visual == "single_sprite"){ + if(m_prop->visual == "sprite"){ infostream<<"CLuaEntityCAO::addToScene(): single_sprite"<getRootSceneNode(), smgr, -1, v3f(0,0,0), v2f(1,1)); @@ -1339,7 +1388,7 @@ public: m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true); m_spritenode->setColor(video::SColor(255,0,0,0)); m_spritenode->setVisible(false); /* Set visible when brightness is known */ - m_spritenode->setSize(v2f(1,1)*1.0*BS); + m_spritenode->setSize(m_prop->visual_size*BS); { const float txs = 1.0 / 1; const float tys = 1.0 / 1; @@ -1387,6 +1436,9 @@ public: for(u32 i=0; i<24; ++i){ vertices[i].Pos *= BS; + vertices[i].Pos.Y *= m_prop->visual_size.Y; + vertices[i].Pos.X *= m_prop->visual_size.X; + vertices[i].Pos.Z *= m_prop->visual_size.X; } u16 indices[6] = {0,1,2,2,3,0}; @@ -1487,6 +1539,66 @@ public: pos_translator.translate(dtime); updateNodePos(); } + + m_anim_timer += dtime; + if(m_anim_timer >= m_anim_framelength){ + m_anim_timer -= m_anim_framelength; + m_anim_frame++; + if(m_anim_frame >= m_anim_num_frames) + m_anim_frame = 0; + } + + updateTexturePos(); + } + + void updateTexturePos() + { + if(m_spritenode){ + scene::ICameraSceneNode* camera = + m_spritenode->getSceneManager()->getActiveCamera(); + if(!camera) + return; + v3f cam_to_entity = m_spritenode->getAbsolutePosition() + - camera->getAbsolutePosition(); + cam_to_entity.normalize(); + + int row = m_tx_basepos.Y; + int col = m_tx_basepos.X; + + if(m_tx_select_horiz_by_yawpitch) + { + if(cam_to_entity.Y > 0.75) + col += 5; + else if(cam_to_entity.Y < -0.75) + col += 4; + else{ + float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / PI * 180.; + float dir = mob_dir - m_yaw; + dir = wrapDegrees_180(dir); + //infostream<<"id="<deSerialize(prop_is); - - infostream<<"m_prop: "<dump()<collisionbox; - m_selection_box.MinEdge *= BS; - m_selection_box.MaxEdge *= BS; + else if(cmd == 2) // set sprite + { + v2s16 p = readV2S16(is); + int num_frames = readU16(is); + float framelength = readF1000(is); + bool select_horiz_by_yawpitch = readU8(is); - pos_translator.init(m_position); - - updateNodePos(); + m_tx_basepos = p; + m_anim_num_frames = num_frames; + m_anim_framelength = framelength; + m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch; + + updateTexturePos(); + } } }; -- cgit v1.2.3