//判断碰撞类型 CollisionType Hero::checkCollision(Point heroPosition) { //cocos2d-x坐标转换为Tilemap坐标 targetTileCoord = sGlobal->gameMap->tileCoordForPosition(heroPosition); //如果勇士坐标超过地图边界,返回kWall类型,阻止其移动 if (heroPosition.x < 0 || targetTileCoord.x > sGlobal->gameMap->getMapSize().width - 1 || targetTileCoord.y < 0 || targetTileCoord.y > sGlobal->gameMap->getMapSize().height - 1) return kWall; //获取墙壁层对应坐标的图块ID int targetTileGID = sGlobal->gameMap->getWallLayer()->tileGIDAt(targetTileCoord); //如果图块ID不为0,表示有墙 if (targetTileGID) { return kWall; } //获取怪物层对应坐标的图块ID targetTileGID = sGlobal->gameMap->getEnemyLayer()->tileGIDAt(targetTileCoord); //如果图块ID不为0,表示有怪物 if (targetTileGID) { //开始战斗 fight(); return kEnemy; } //获取物品层对应坐标的图块ID targetTileGID = sGlobal->gameMap->getItemLayer()->tileGIDAt(targetTileCoord); //如果图块ID不为0,表示有物品 if (targetTileGID) { //拾取物品 pickUpItem(); return kItem; } //获取门层对应坐标的图块ID targetTileGID = sGlobal->gameMap->getDoorLayer()->tileGIDAt(targetTileCoord); //如果图块ID不为0,表示有门 if (targetTileGID) { //打开门 openDoor(targetTileGID); return kDoor; } //从npc字典中查询 int index = targetTileCoord.x + targetTileCoord.y * sGlobal->gameMap->getMapSize().width; NPC *npc = sGlobal->gameMap->npcDict.at(index); if (npc != NULL) { actWithNPC(); return kNPC; } //从Teleport字典中查询 Teleport *teleport = sGlobal->gameMap->teleportDict.at(index); if (teleport != NULL) { doTeleport(teleport); return kTeleport; } //可以通行 return kNone; }
void doEntities() { int i; EntityList *el; /* Loop through the entities and perform their action */ for (el=entities->next;el!=NULL;el=el->next) { self = el->entity; if (self->inUse == TRUE) { self->flags &= ~(HELPLESS|INVULNERABLE|FLASH|ATTRACTED); for (i=0;i<MAX_CUSTOM_ACTIONS;i++) { if (self->customAction[i].thinkTime > 0) { doCustomAction(&self->customAction[i]); } } if (!(self->flags & TELEPORTING)) { if (!(self->flags & (FLY|GRABBED))) { switch (self->environment) { case WATER: case SLIME: self->dirY += GRAVITY_SPEED * 0.25 * self->weight; if (self->flags & FLOATS) { if (self->dirX != 0) { self->endY++; self->dirY = cos(DEG_TO_RAD(self->endY)) / 20; } } if (self->dirY >= MAX_WATER_SPEED) { self->dirY = MAX_WATER_SPEED; } break; default: self->dirY += GRAVITY_SPEED * self->weight; if (self->dirY >= MAX_AIR_SPEED) { self->dirY = MAX_AIR_SPEED; } else if (self->dirY > 0 && self->dirY < 1) { self->dirY = 1; } break; } } if (self->flags & GRABBED) { self->dirY = 0; } if (self->standingOn != NULL) { if (self->standingOn->dirX != 0) { self->dirX = self->standingOn->dirX; } if (self->standingOn->dirY > 0) { self->dirY = self->standingOn->dirY + 1; } } if (!(self->flags & HELPLESS)) { if (self->action == NULL) { showErrorAndExit("%s has no action function", self->name); } self->action(); } else { checkToMap(self); } if (self->standingOn != NULL) { self->flags |= WAS_STANDING_ON; } else { self->flags &= ~WAS_STANDING_ON; } self->standingOn = NULL; if (self->flags & SPAWNED_IN) { self->spawnTime--; } if (self->inUse == TRUE) { addToGrid(self); } } else { doTeleport(); } addToDrawLayer(self, self->layer); } } }