⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m_map.cpp

📁 C++课程大学作业的一次任务
💻 CPP
字号:
////////////////////////////////////
//     M_Map.CPP
// MAP CLASS:
//    READ MAP
//    CTRL MAPBLOCKS
//    REPORT MAPBLOCK'S PROPERTY
//
// PROJECT: Ultra Pac Man
// PROGRAMER: Mal
// LAST UPDATE: Dec. 2nd 2001
////////////////////////////////////

#include "m_map.h"
#include "m_err.h"
#include <stdio.h>
#include <stdlib.h>

//Global Pre-Defined Pointer
M_Map* thisMap;

M_Map::M_Map(char* mapFileName)
{
	if((mapHead = new MapHeader) == NULL
	  || (mbHandler = new MapBlock[59*59]) == NULL)
	{
		MErr("ERROR OCCURRED WHEN CREATING MAPBLOCKS");
	}
	LoadMap(mapFileName);
}

M_Map::~M_Map()
{
	delete mapHead;
	delete mbHandler;
	delete SRPoints;
	delete MRPoints;
}

bool M_Map::LoadMap(char* mapFileName)
{
	if((MapFile = fopen(mapFileName, "rb"))==NULL)
	{
		bMapState = M_FALSE;
		MErr(mapFileName,M_ERR_MAPFILE_OPEN);
	}
	else
	{
		bMapState = M_TRUE;
		//Make sure the map structure is correct in size
		if(fread(mapHead,1,sizeof(MapHeader),MapFile)
		   !=sizeof(MapHeader))
		{
		   bMapState = M_FALSE;
		   MErr(mapFileName,M_ERR_MAP_STRUCT);
		}

		//get multi-respawn points
		if((SRPoints = new RespawnPoint[mapHead->nTotalSRPoints]) == NULL
		  || (MRPoints = new RespawnPoint[mapHead->nTotalMRPoints]) == NULL)
		{
			MErr("ERROR OCCURRED WHEN CREATING RESPAWN POINTS");
		}

		//fill each mapblock with its property and texture index
		for(int SRCnt=0, MRCnt=0, y=1; y<=59; y++)
		{
		 for(int x=1; x<=59; x++)
		 {
			fread((char*)(&mbHandler[(x-1)+(y-1)*59]),1,sizeof(MapBlock),MapFile);

			if((SRCnt<=mapHead->nTotalSRPoints)
				&& (mbHandler[(x-1)+(y-1)*59].nBlockType == 5))
			{
				(SRPoints+SRCnt)->x = x;
				(SRPoints+SRCnt)->y = y;
				SRCnt++;
			}

			if((MRCnt<=mapHead->nTotalMRPoints)
				&& (mbHandler[(x-1)+(y-1)*59].nBlockType == 6))
			{
				(MRPoints+MRCnt)->x = x;
				(MRPoints+MRCnt)->y = y;
				MRCnt++;
			}
		 }
		}

		fclose(MapFile);
	}
 return M_NORMAL;
}

bool M_Map::ChgBlockProperty(int x, int y, int type, int texture)
{
 if(bMapState == M_TRUE)
 {
	mbHandler[(x-1)+(y-1)*59].nBlockType = type;
	mbHandler[(x-1)+(y-1)*59].nTexture = texture;
	return M_NORMAL;
 }
 else
 {
	return M_ERR_MAP_NOT_READY;
 }
}

const MapHeader* M_Map::GetMapInfo()
{
	if(bMapState == M_TRUE)
	{
		return mapHead;
	}
	else
	{
		return NULL;
	}
}

const MapBlock* M_Map::GetMapBlockInfo(int x, int y)
{
 if(bMapState == M_TRUE)
 {
	if(x>0 && x<60 && y>0 && y<60)
	{
		return &mbHandler[(x-1)+(y-1)*59];
	}
	else
	{
		return NULL;
	}
 }
 return NULL;
}

const RespawnPoint* M_Map::SRPS()
{
	if(bMapState == M_TRUE)
	{
		return SRPoints;
	}
	else
	{
		return NULL;
	}
}

const RespawnPoint* M_Map::MRPS()
{
	if(bMapState == M_TRUE)
	{
		return MRPoints;
	}
	else
	{
		return NULL;
	}
}

bool M_Map::GetMapState()
{
	return bMapState;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -