//------------------------------- ctor -----------------------------------
//------------------------------------------------------------------------
SoccerPitch::SoccerPitch(int cx, int cy):m_cxClient(cx),
    m_cyClient(cy),
    m_bPaused(false),
    m_bGoalKeeperHasBall(false),
    m_Regions(NumRegionsHorizontal*NumRegionsVertical),
    m_bGameOn(true)
{
    //define the playing area
    m_pPlayingArea = new Region(20, 20, cx-20, cy-20);

    //create the regions
    CreateRegions(PlayingArea()->Width() / (double)NumRegionsHorizontal,
                  PlayingArea()->Height() / (double)NumRegionsVertical);

    //create the goals
    m_pRedGoal  = new Goal(Vector2D( m_pPlayingArea->Left(), (cy-Prm.GoalWidth)/2),
                           Vector2D(m_pPlayingArea->Left(), cy - (cy-Prm.GoalWidth)/2),
                           Vector2D(1,0));



    m_pBlueGoal = new Goal( Vector2D( m_pPlayingArea->Right(), (cy-Prm.GoalWidth)/2),
                            Vector2D(m_pPlayingArea->Right(), cy - (cy-Prm.GoalWidth)/2),
                            Vector2D(-1,0));


    //create the soccer ball
    m_pBall = new SoccerBall(Vector2D((double)m_cxClient/2.0, (double)m_cyClient/2.0),
                             Prm.BallSize,
                             Prm.BallMass,
                             m_vecWalls);


    //create the teams
    m_pRedTeam  = new SoccerTeam(m_pRedGoal, m_pBlueGoal, this, SoccerTeam::red);
    m_pBlueTeam = new SoccerTeam(m_pBlueGoal, m_pRedGoal, this, SoccerTeam::blue);

    //make sure each team knows who their opponents are
    m_pRedTeam->SetOpponents(m_pBlueTeam);
    m_pBlueTeam->SetOpponents(m_pRedTeam);

    //create the walls
    Vector2D TopLeft(m_pPlayingArea->Left(), m_pPlayingArea->Top());
    Vector2D TopRight(m_pPlayingArea->Right(), m_pPlayingArea->Top());
    Vector2D BottomRight(m_pPlayingArea->Right(), m_pPlayingArea->Bottom());
    Vector2D BottomLeft(m_pPlayingArea->Left(), m_pPlayingArea->Bottom());

    m_vecWalls.push_back(Wall2D(BottomLeft, m_pRedGoal->RightPost()));
    m_vecWalls.push_back(Wall2D(m_pRedGoal->LeftPost(), TopLeft));
    m_vecWalls.push_back(Wall2D(TopLeft, TopRight));
    m_vecWalls.push_back(Wall2D(TopRight, m_pBlueGoal->LeftPost()));
    m_vecWalls.push_back(Wall2D(m_pBlueGoal->RightPost(), BottomRight));
    m_vecWalls.push_back(Wall2D(BottomRight, BottomLeft));

    ParamLoader* p = ParamLoader::Instance();
}
    void
    Creature::LoadFromData(CreatureLoadDataPacket& load_data)
    {
        JsonNode * json_root = load_data.base_node.toNode();
        
        // Load points and topology
        JsonNode * json_mesh = GetJSONLevelNodeFromKey(*json_root, "mesh");
        global_pts = ReadJSONPoints3D(*json_mesh, "points", total_num_pts);
        global_indices = ReadJSONUints(*json_mesh,"indices", total_num_indices);
        global_uvs = ReadJSONPoints2D(*json_mesh, "uvs", total_num_pts);
        
        render_colours = new glm::uint8[total_num_pts * 4];
        render_pts = new glm::float32[total_num_pts * 3];
        FillRenderColours(255, 255, 255, 255);
        
        // Load bones
        meshBone * root_bone = CreateBones(*json_root, "skeleton");
        
        // Load regions
        std::vector<meshRenderRegion *> regions = CreateRegions(*json_mesh,
                                                                "regions",
                                                                global_indices,
                                                                global_pts,
                                                                global_uvs);
        
        // Add into composition
        render_composition = new meshRenderBoneComposition();
        render_composition->setRootBone(root_bone);
        render_composition->getRootBone()->computeRestParentTransforms();
        
        for(auto& cur_region : regions) {
            cur_region->setMainBoneKey(root_bone->getKey());
            cur_region->determineMainBone(root_bone);
            render_composition->addRegion(cur_region);
        }
        
        render_composition->initBoneMap();
        render_composition->initRegionsMap();
        
        for(auto& cur_region : regions) {
            cur_region->initFastNormalWeightMap(render_composition->getBonesMap());
        }
        
        render_composition->resetToWorldRestPts();

        // Fill up available animation names
        JsonNode * json_anim_base = GetJSONLevelNodeFromKey(*json_root, "animation");
        animation_names = GetJSONKeysFromNode(*json_anim_base);
    }