| 680 | } |
| 681 | |
| 682 | int GameObjectPool::New(lua_State* L)LNOEXCEPT |
| 683 | { |
| 684 | // ������ |
| 685 | if (!lua_istable(L, 1)) |
| 686 | return luaL_error(L, "invalid argument #1, luastg object class required for 'New'."); |
| 687 | lua_getfield(L, 1, "is_class"); // t(class) ... b |
| 688 | if (!lua_toboolean(L, -1)) |
| 689 | return luaL_error(L, "invalid argument #1, luastg object class required for 'New'."); |
| 690 | lua_pop(L, 1); // t(class) ... |
| 691 | |
| 692 | // ����һ������ |
| 693 | size_t id = 0; |
| 694 | if (!m_ObjectPool.Alloc(id)) |
| 695 | return luaL_error(L, "can't alloc object, object pool may be full."); |
| 696 | |
| 697 | // ���ö��� |
| 698 | GameObject* p = m_ObjectPool.Data(id); |
| 699 | LASSERT(p); |
| 700 | p->Reset(); |
| 701 | p->status = STATUS_DEFAULT; |
| 702 | p->id = id; |
| 703 | p->uid = m_iUid++; |
| 704 | |
| 705 | // ���������� |
| 706 | LIST_INSERT_BEFORE(&m_pObjectListTail, p, Object); // Object����ֻ��uid�йأ��������ĩβ���� |
| 707 | LIST_INSERT_BEFORE(&m_pRenderListTail, p, Render); // Render�����ڲ������Ҫ�������� |
| 708 | LIST_INSERT_BEFORE(&m_pCollisionListTail[p->group], p, Collision); // Ϊ��֤�����ԣ���CollisionҲ������ |
| 709 | LIST_INSERT_SORT(p, Render, RenderListSortFunc); |
| 710 | LIST_INSERT_SORT(p, Collision, ObjectListSortFunc); |
| 711 | |
| 712 | GETOBJTABLE; // t(class) ... ot |
| 713 | lua_createtable(L, 2, 0); // t(class) ... ot t(object) |
| 714 | lua_pushvalue(L, 1); // t(class) ... ot t(object) class |
| 715 | lua_rawseti(L, -2, 1); // t(class) ... ot t(object) ����class |
| 716 | lua_pushinteger(L, (lua_Integer)id); // t(class) ... ot t(object) id |
| 717 | lua_rawseti(L, -2, 2); // t(class) ... ot t(object) ����id |
| 718 | lua_getfield(L, -2, METATABLE_OBJ); // t(class) ... ot t(object) mt |
| 719 | lua_setmetatable(L, -2); // t(class) ... ot t(object) ����Ԫ�� |
| 720 | lua_pushvalue(L, -1); // t(class) ... ot t(object) t(object) |
| 721 | lua_rawseti(L, -3, id + 1); // t(class) ... ot t(object) ���õ�ȫ�ֱ� |
| 722 | lua_insert(L, 1); // t(object) t(class) ... ot |
| 723 | lua_pop(L, 1); // t(object) t(class) ... |
| 724 | lua_rawgeti(L, 2, LGOBJ_CC_INIT); // t(object) t(class) ... f(init) |
| 725 | lua_insert(L, 3); // t(object) t(class) f(init) ... |
| 726 | lua_pushvalue(L, 1); // t(object) t(class) f(init) ... t(object) |
| 727 | lua_insert(L, 4); // t(object) t(class) f(init) t(object) ... |
| 728 | lua_call(L, lua_gettop(L) - 3, 0); // t(object) t(class) ִ�й��캯�� |
| 729 | lua_pop(L, 1); // t(object) |
| 730 | |
| 731 | p->lastx = p->x; |
| 732 | p->lasty = p->y; |
| 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | int GameObjectPool::Del(lua_State* L)LNOEXCEPT |
| 737 | { |