Example #1
0
//------------------------------------------------------------------------------
// Private Functions:
//------------------------------------------------------------------------------
GameObj* gameObjCreate(unsigned long type, float scale, Vector2D* pPos, Vector2D* pVel, float dir)
{
	unsigned long i;
	Vector2D zero = { 0.0f, 0.0f };

	AE_ASSERT_PARM(type < sGameObjBaseNum);

	// 找游戏对象列表中没用过的位置
	for (i = 0; i < GAME_OBJ_NUM_MAX; i++)
	{
		GameObj* pInst = sGameObjList + i;

		// 找非活动对象的位置
		if (pInst->flag == 0)
		{
			// 找到了
			pInst->pObject = sGameObjBaseList + type;
			pInst->flag = FLAG_ACTIVE;
			pInst->scale = scale;
			pInst->posCurr = pPos ? *pPos : zero;
			pInst->velCurr = pVel ? *pVel : zero;
			pInst->dirCurr = dir;

			// 返回新创建的对象实例
			return pInst;
		}
	}

	// 位置满了
	return 0;
}
GameObjInst* gameObjInstCreate(u32 type, f32 scale, AEVec2* pPos, AEVec2* pVel, f32 dir, bool forceCreate, GameObjInst *parent )
{
    AEVec2 zero = { 0.0f, 0.0f };

    AE_ASSERT_PARM(type < sGameObjNum);

    // loop through the object instance list to find a non-used object instance
    for (u32 i = 0; i < GAME_OBJ_INST_NUM_MAX; i++)
    {
        GameObjInst* pInst = sGameObjInstList + i;

        // check if current instance is not used
        if (pInst->flag == 0)
        {
            // it is not used => use it to create the new instance
            pInst->pObject	 = sGameObjList + type;
            pInst->type      = type;
            pInst->score     = 0;
			      pInst->flag		   = FLAG_ACTIVE;
			      pInst->life		   = 1.0f;
			      pInst->scale	   = scale;
			      pInst->posCurr	 = pPos ? *pPos : zero;
			      pInst->velCurr	 = pVel ? *pVel : zero;
			      pInst->dirCurr	 = dir;
            pInst->parent    = parent;
			      pInst->pUserData = NULL;

            // keep track the number of asteroid
            if (pInst->pObject->type == TYPE_ASTEROID)
                sAstCtr++;

            // return the newly created instance
            return pInst;
        }
    }

    if (forceCreate)
    {
        f32          scaleMin = FLT_MAX;
        GameObjInst* pDst     = 0;

        // loop through the object instance list to find the smallest particle
        for (u32 i = 0; i < GAME_OBJ_INST_NUM_MAX; i++)
        {
            GameObjInst* pInst = sGameObjInstList + i;

            // check if current instance is a red particle
            if ((TYPE_PTCL_RED <= pInst->pObject->type) && (pInst->pObject->type <= TYPE_PTCL_WHITE) && (pInst->scale < scaleMin))
            {
                scaleMin = pInst->scale;
                pDst     = pInst;
            }
        }

        if (pDst)
        {
            pDst->pObject	 = sGameObjList + type;
            pDst->type       = type;
            pDst->score      = 0;
			pDst->flag		 = FLAG_ACTIVE;
			pDst->life		 = 1.0f;
			pDst->scale	 = scale;
			pDst->posCurr	 = pPos ? *pPos : zero;
			pDst->velCurr	 = pVel ? *pVel : zero;
			pDst->dirCurr	 = dir;
            pDst->parent     = parent;
			pDst->pUserData  = NULL;

            // keep track the number of asteroid
            if (pDst->pObject->type == TYPE_ASTEROID)
                sAstCtr++;

            // return the newly created instance
            return pDst;
        }
    }

    // cannot find empty slot => return 0
    return 0;
}