Beispiel #1
0
/* ------------------------------ prints the given room --------------------*/
void CRoom::sendRoom() const
{
    unsigned int i, pos;
    char line[MAX_STR_LEN];
    
    send_to_user(" Id: %i, Flags: %s, Region: %s, Coord: %i,%i,%i\r\n", getId(),
                     (const char *) conf->sectors[ (int) getTerrain() ].desc,
	    (const char *) region->getName(),
        getX(), getY(), getZ());
    send_to_user(" %s\r\n", (const char *) getName() );

    line[0] = 0;
    pos = 0;

    if (!(proxy->isMudEmulation() && conf->getBriefMode() ) ) {
        QByteArray desc = getDesc();

        for (i = 0; i <= strlen(desc); i++)
            if (desc[i] == '|') {
                line[pos] = 0;
                send_to_user("%s\r\n", line);
                line[0] = 0;
                pos = 0;
            } else {
                line[pos++] = desc[i];
            }
    }
    send_to_user(" note: %s\r\n", (const char *) getNote());

    
    sprintf(line, "Doors:");
    for (i = 0; i <= 5; i++) {
        ExitDirection iDir = static_cast<ExitDirection>(i);
      if (isDoorSecret(iDir)) {
        sprintf(line + strlen(line), " %c: %s", dirbynum(iDir), (const char *) getDoor(iDir));
      }
    
    }
    send_to_user("%s\r\n", line);

    
    if (conf->getBriefMode() && proxy->isMudEmulation()) {
      sprintf(line, "Exits: ");
      for (i = 0; i <= 5; i++) {
          ExitDirection iDir = static_cast<ExitDirection>(i);
          if (isExitPresent(iDir) == true) {
              if ( isExitUndefined(iDir) ) {
                  sprintf(line + strlen(line), " #%s#", exitnames[i]);
                  continue;
              }
              if ( isExitDeath(iDir) ) {
                  sprintf(line + strlen(line), " !%s!", exitnames[i]);
                  continue;
              }

              if (isDoorSet(iDir)) {
                  if (isDoorSecret(iDir) == false)  {
                      sprintf(line + strlen(line), " (%s)", exitnames[i]);
                  } else {
                      sprintf(line + strlen(line), " +%s+", exitnames[i]);
                  }
              } else {
                  sprintf(line + strlen(line), " %s", exitnames[i]);
              }
          }
      }
      
      
    } else {
      
      line[0] = 0;
      sprintf(line, " exits:");
  
      for (i = 0; i <= 5; i++) {
          ExitDirection iDir = static_cast<ExitDirection>(i);
          if (isExitPresent(iDir) == true) {
              if (isExitUndefined(iDir) ) {
                  sprintf(line + strlen(line), " #%s#", exitnames[i]);
                  continue;
              }
              if (isExitDeath(iDir)) {
                  sprintf(line + strlen(line), " !%s!", exitnames[i]);
                  continue;
              }

              if (isDoorSet(iDir)) {
                  if (isDoorSecret(iDir) == false)  {
                      sprintf(line + strlen(line), " (%s)", exitnames[i]);
                  } else {
                      sprintf(line + strlen(line), " +%s+", exitnames[i]);
                  }
              } else {
                  sprintf(line + strlen(line), " %s", exitnames[i]);
              }

              sprintf(line + strlen(line), " -[to %i]-", getExitLeadsTo(iDir) );
          }
       }

    }

    send_to_user("%s\r\n", line);
}
void game_loop(void)
{
    //load font
    ALLEGRO_FONT *font = al_load_ttf_font("RobotoMono-Medium.ttf",10,0);
    //create floor array (will eventually make this a function)
    srand(time(NULL));
    for (int x = 0; x < 16; x++)
            {
                //for each y value between 2 and 13
                for (int y = 0; y < 16; y++)
                {
                    randX = 6 + (rand() % (int)(12 - 6 + 1));
                    randY = 0 + (rand() % (int)(3 - 0 + 1));
                    //drawDungeonTile(randX, randY, x, y);
                    floorArray [x] [y] [0] = randX;
                    floorArray [x] [y] [1] = randY;
                }
            }

    Player p1;
    //(speed, friction, sprint accel, max speed)
    //default: p1.setMovement(1.4, .4, 1.8, 4);
    p1.setMovement(1.4, .4, 1.8, 3);
    p1.setPosition(tileSize * 7, tileSize * 12);
    p1.setCurrentRoom(112);
    p1.setInitial(tileSize,tileSize,240,240);
    p1.setH(tileSize);
    p1.setW(tileSize);
    p1.setAnimation(tileSize * 0, tileSize * 0,0,0,9,4,0);
    p1.canMove = true;
    p1.vincible = true;
    p1.blinking = false;
    p1.blinkTimer = 5;
    p1.setMaxHP(100);
    p1.setCurrentHP(100);
    int tempBlinkTimer = p1.blinkTimer;
    p1.knockback = 0;
    p1.knockbackTimer = 0;
    p1.setBound(19,27,7,3, 14,8,9,24);
    p1.canRotate = true;
    //p1.setX(240);
    //p1.setY(240);
    p1.leavingTransport = 0;
    //init player prototypes
    void movePlayerUp(Player &p);
    void movePlayerDown(Player &p);
    void movePlayerLeft(Player &p);
    void movePlayerRight(Player &p);
    void sprintPlayer(Player &p);
    void maxSpeedPlayer(Player &p);
    void maxWalkSpeedPlayer(Player &p);
    void updatePlayer(Player &p);
    void collidePlayerWallX(Player &p, int obstacles [16][16]);
    void collidePlayerWallY(Player &p, int obstacles [16][16]);
    void collidePlayerMonster(Player &p, Monster m[]);
    void drawPlayer(Player &p);
    void resetPlayerAnimation(Player &p);

    //load current room map
    Room currentRoom;
    currentRoom.getWalkRoom(p1.getCurrentRoom());
    //currentRoom.getCollideRoom(p1.getCurrentRoom());

    //load all objects
    objectArray currentArray;
    currentArray.getObjects(p1.getCurrentRoom());

    //load current room transporters
    Transporter currentTrans;
    currentTrans.exitNumber = 0;

    //load current room buttons
    //void getButton (int array[][16]);
    void getButton (Button b[], int array[][16]);
    //void drawButtons (int buttonArray[][16]);
    void drawButtons (Button b[]);
    void collidePlayerButton(Player &p, Button b[]);

    Button currentButts[numButts];
    getButton (currentButts, currentArray.buttsArray);

    void getDoor (Door d[], int array[][16]);
    void placeDoor (Door d[], Room &r);

    Door currentDoor[numDoors];
    getDoor (currentDoor, currentArray.buttsArray);
    //currentDoor.getDoorState();

    void getMonsters (Monster m[], int array[][16]);
    void drawMonsters (Monster m[]);
    void collideMonsterWallX(Monster m[], int obstacles [16][16]);
    void collideMonsterWallY(Monster m[], int obstacles [16][16]);
    void updateMonstersX (Monster currentMonsters[]);
    void updateMonstersY (Monster currentMonsters[]);
    //void seekMonsters (Monster m[], Player p);
    void seekMonsters (Monster &m, float x, float y);
    void seekPathMonsters (Monster &m, Player p);
    void pathfindMonsters (Monster &m, Player p, int obstacles [16][16], int mt);
    void newPathfindMonster (Monster &m, Player p, int obstacles [16][16]);
    int monsterDebug = 1;


    //init monsters prototypes
    Monster currentMonsters[numMonsters];
    //load current room's monsters
    getMonsters(currentMonsters, currentArray.array);

    Weapon stick(15, 32, 4, 0, 20, bash, none);
    stick.setAnimation(tileSize * 0, tileSize * 0,0,0,2,3,0);
    void updateWeapon(Player &p, Weapon &w);
    void collideWeaponMonster(Weapon w, Monster m[], Player p);
    void resetAttack (Player p, Weapon &w);
    void attackWeapon(Player p, Weapon &w);
    void drawWeapon(Player p, Weapon &w);

    void drawCutscene(int num, int message);
    void closingSplash(void);
    Cutscene currentCutscene;
    currentCutscene.cutsceneFont = al_load_ttf_font("data/slkscr.ttf",32,0);

    int vincibleTimer = 0;
    int pathCounter = 0;
    bool showHitboxes = false;
    bool createCutscene = false;
    bool inCutscene = false;
    bool runSeekPath = false;
    al_start_timer(timer);
 
    while (!done) {
        ALLEGRO_EVENT event;
        al_wait_for_event(event_queue, &event);
 
        if (event.type == ALLEGRO_EVENT_TIMER) 
        {
            if(key[keyW])
            {
                movePlayerUp(p1);
            }
            else if(key[keyS])
            {
                movePlayerDown(p1);
            }

            if(key[keyA])
            {
                movePlayerLeft(p1);
            }
            else if(key[keyD])
            {
                movePlayerRight(p1);
            }

            if(!(key[keyW] || key[keyA] ||key[keyS] ||key[keyD])) 
            {
                resetPlayerAnimation(p1);
            }

            if(key[keyShift] && (key[keyW] || key[keyA] ||key[keyS] ||key[keyD])) 
            {
                sprintPlayer(p1);
            }
            else
            {
                maxWalkSpeedPlayer(p1);
            }
                maxSpeedPlayer(p1);
            if(key[keySpace])
            {
                //printf("ATTACK!\n");
            }
            
            //update player position
            p1.setX(p1.getX() + p1.getDx());
            collidePlayerMonster(p1,currentMonsters);
            collidePlayerButton(p1,currentButts);
            //printf("player DX: %f\n",p1.getDx());
            //printf("player DY: %f\n",p1.getDy());
            //X axis
            //detect and respond to collision with obstacles
            collidePlayerWallX(p1,currentRoom.collideArray);
            if(p1.canMove)
            {
                p1.setDx(p1.getDx() * p1.getFriction());
            }
            //knockback timer

            //Y axis
            p1.setY(p1.getY() + p1.getDy());
            collidePlayerWallY(p1,currentRoom.collideArray);
            p1.setDy(p1.getDy() * p1.getFriction());

            //Monster movement and wall collisions
                for (int i = 0; i < numMonsters; i++)
                {
                    if (currentMonsters[i].isLive)
                    {
                        newPathfindMonster( currentMonsters[i], p1, currentRoom.collideArray);
                    }
                }
            //seekMonsters(currentMonsters[1],p1.getX(), p1.getY());
            for (int i = 0; i < numMonsters; i++)
            {
                if (currentMonsters[i].isLive)
                {
                    //seekPathMonsters(currentMonsters[i],p1);
                    //printf("Monster location: %.1f, %.1f\n",currentMonsters[1].getX(),currentMonsters[1].getY());
                    seekMonsters(currentMonsters[i],currentMonsters[i].destX, currentMonsters[i].destY);
                    //seekMonsters(currentMonsters[i],64,64);
                }
            }
            //weapon updating
            updateWeapon(p1,stick);
            collideWeaponMonster(stick, currentMonsters, p1);
            updateMonstersX(currentMonsters);
            collideMonsterWallX(currentMonsters,currentRoom.collideArray);
            updateMonstersY(currentMonsters);
            collideMonsterWallY(currentMonsters,currentRoom.collideArray);

            //if able to transport
            if (p1.leavingTransport == 0)
            {
                //check for collisions with transporter
                currentTrans.exitNumber = currentTrans.playerTransCollision(p1.getX(), p1.getY(),currentArray.array);
                if (currentTrans.exitNumber)
                {
                    currentTrans.fromNumber = p1.getCurrentRoom();
                    p1.leavingTransport = 1;
                }
            }

            //if transporting
            if ( p1.leavingTransport == 1)
            {
                //set room to move to
                p1.setCurrentRoom(currentTrans.exitNumber);
                currentRoom.getWalkRoom(p1.getCurrentRoom());
                //currentRoom.getCollideRoom(p1.getCurrentRoom());
                currentArray.getObjects(p1.getCurrentRoom());
                getMonsters(currentMonsters, currentArray.array);
                getButton(currentButts, currentArray.buttsArray);
                getDoor(currentDoor, currentArray.buttsArray);
                placeDoor(currentDoor, currentRoom);
                //must set player's location to the location of the exit portal in the new room
                currentTrans.getDestination(currentArray.array);
                p1.setX(currentTrans.destination[0] + tileSize/2);
                p1.setY(currentTrans.destination[1] + tileSize/2);
                currentTrans.destination[0] = 0;
                currentTrans.destination[1] = 0;
                p1.leavingTransport = 0;
            }

            //functions for drawing the player (will need to go elsewhere eventually)
            //invincible timer
            if (p1.vincible == false)
            {
                vincibleTimer ++;
                tempBlinkTimer --;

                p1.blinking = true;
                //printf("p1 blinking? %d, %d\n",p1.blinking, tempBlinkTimer);
                if (tempBlinkTimer < 0)
                {
                    p1.blinking = false;
                    if ( tempBlinkTimer == p1.blinkTimer * (-3))
                    {
                        tempBlinkTimer = p1.blinkTimer;
                    }
                }
                if (vincibleTimer == invincibleTime)
                {
                    p1.vincible = true;
                    p1.blinking = false;
                    vincibleTimer = 0;
                    //printf ("vincible Again \n");
                }
            }
            pathCounter ++;
            if (pathCounter == 90) {pathCounter = 0;}

            redraw = true;
         }

        //key pressed down
        else if (event.type == ALLEGRO_EVENT_KEY_DOWN) 
        {
            switch (event.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                done = true;

                case ALLEGRO_KEY_W:
                key[keyW] = true;
                break;

                case ALLEGRO_KEY_A:
                key[keyA] = true;
                break;

                case ALLEGRO_KEY_S:
                key[keyS] = true;
                break;

                case ALLEGRO_KEY_D:
                key[keyD] = true;
                break;

                case ALLEGRO_KEY_C:
                key[keyC] = true;
                if (!inCutscene)
                {
                    currentCutscene.openDialog ("data/cutscenes/1");
                    createCutscene = 1;
                }
                if (inCutscene)
                {
                    currentCutscene.currentMessage ++;
                }
                while(1)
                break;

                case ALLEGRO_KEY_LSHIFT:
                key[keyShift] = true;
                break;

                case ALLEGRO_KEY_SPACE:
                key[keySpace] = true;
                runSeekPath = true;
                //pathfindMonsters( currentMonsters[1], p1, currentRoom.collideArray, pathCounter);
                newPathfindMonster( currentMonsters[1], p1, currentRoom.collideArray);
                attackWeapon(p1,stick);
                break;
            }
        }

        //key released
        else if (event.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch (event.keyboard.keycode)
            {
                case ALLEGRO_KEY_W:
                key[keyW] = false;
                break;

                case ALLEGRO_KEY_A:
                key[keyA] = false;
                break;

                case ALLEGRO_KEY_S:
                key[keyS] = false;
                break;

                case ALLEGRO_KEY_D:
                key[keyD] = false;
                break;

                case ALLEGRO_KEY_C:
                key[keyC] = false;
                break;

                case ALLEGRO_KEY_LSHIFT:
                key[keyShift] = false;
                break;

                case ALLEGRO_KEY_SPACE:
                key[keySpace] = false;
                resetAttack(p1, stick);
                break;
            }
        }
        
        else if(event.type == ALLEGRO_EVENT_MOUSE_AXES || ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY)
        {
           crosshair_x = event.mouse.x - crosshairSize/2;
           crosshair_y = event.mouse.y - crosshairSize/2;
        }
        
        if (redraw && al_is_event_queue_empty(event_queue)) {
            redraw = false;
            //Clear screen to black
            al_clear_to_color(al_map_rgb(0, 0, 0));

            if (createCutscene == true)
            {
                al_set_target_bitmap(cutsceneBackdrop);
                al_clear_to_color(al_map_rgb(0, 0, 0));
            }

            //update_graphics();
            //graphics that will always be there
            if (!inCutscene)
            {
                //drawDoor(currentDoor,currentRoom);
                drawDungeon(floorArray,currentRoom.walkArray);
                drawDungeon(floorArray,currentRoom.collideArray);
                for (int i = 0; i < 16; i++)
                {
                    for (int j = 0; j < 16; j++)
                    {
                        al_draw_textf(font, al_map_rgb(255,255,255),j*tileSize,i*tileSize,ALLEGRO_ALIGN_LEFT,"%d,%d",j+1,i+1);
                    }
                }

                if (transDebug) currentTrans.drawTransporters(currentArray.array);
                drawButtons(currentButts);
                drawMonsters(currentMonsters);
                
                if (p1.getDir() != UP && p1.getDir() != LEFT)
                {
                    if (p1.blinking == false)
                    {
                        drawPlayer(p1);
                    }
                }
                drawWeapon(p1,stick);

                if (p1.getDir() == UP || p1.getDir() == LEFT)
                {
                    if (p1.blinking == false)
                    {
                        drawPlayer(p1);
                    }
                }

                al_draw_textf(font, al_map_rgb(255,255,255),0,tileSize*0,ALLEGRO_ALIGN_LEFT,"playerHP: %d",p1.getCurrentHP());
                for (int i = 1; i < numMonsters; i++)
                {
                    if (currentMonsters[i].getCurrentHP() > -10000 && currentMonsters[i].getCurrentHP() <10000)
                    {
                        al_draw_textf(font, al_map_rgb(255,255,255),tileSize * 4 * i,tileSize*15,ALLEGRO_ALIGN_LEFT,"monster %d HP: %d",i,currentMonsters[i].getCurrentHP());
                    }
                }
            }
            if (inCutscene)
            {
                currentCutscene.drawCutscene(1, 1);
            }
            //al_draw_bitmap(crosshair, crosshair_x, crosshair_y, 0);

            if (createCutscene == true)
            {
                inCutscene = true;
                createCutscene = false;
                al_set_target_bitmap(al_get_backbuffer(display));
                //al_lock_bitmap(cutsceneBackdrop);
                inCutscene = true;
            }
            al_flip_display();
        }
    }
}