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

📄 board_commands.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// Board command handlers (keyboard and mouse)
//
// (c) 2003 Ken Reed
//
// This is free software. You can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
// ============================================================================

#include "stdafx.h"
#include "cell.h"
#include "board.h"
#include "universal.h"


// ============================================================================
// Deal with a key press
// ============================================================================

void Board::key(int player, int key, Point position)
{
   Cell * cell = (get_cell(position));
   if (cell == 0) return;

   switch (key) {
   case 'a':
      attack(player, cell);
      break;

   case 'f':
      fill_sea(player, cell);
      break;

   case 's':
      scuttle(player, cell);
      break;
   }
}


// ============================================================================
// Deal with a mouse event
// ============================================================================

void Board::mouse(int player, Mouse_Command command, Point position)
{
   Cell * cell = (get_cell(position));

   if (cell == 0)              return;
   if (cell->player != player) return;

   switch (command) {
   case Left_Mouse:
      cell->toggle_vector(position);
      break;

   case Shift_Left_Mouse:
      if (cell->troops > 0) {
         cell->set_march(position);
      }
      break;

   case Right_Mouse:
      cell->set_vector(position);
      break;
   }
}


// ============================================================================
// Attack a cell
//
// If we occupy any cells adjacent to the cell pointed at, set their vectors
// to point to the cell under attack.
// ============================================================================

void Board::attack(int player, Cell * target_cell)
{
   if (target_cell->player == player) return;
   static int vector_map[6] = {3, 4, 5, 0, 1, 2};

   int i (target_cell->i);
   int j (target_cell->j);

   for (int k = 0; k < 6; k++) {
      Cell * adjacent_cell = (get_cell(i, j, k));
      if (adjacent_cell == 0) continue;

      if (adjacent_cell->player == player) {
         adjacent_cell->vector[vector_map[k]] = true;
      }
   }
}


// ============================================================================
// Fill sea
//
// If a cell is full and a vector points to some sea, raise the elevation of
// the cell pointed to.
// ============================================================================

void Board::fill_sea(int player, Cell * target_cell)
{
   if (target_cell->player != player) return;
   if (target_cell->troops < (max_troops - 0.1)) return;

   int side (target_cell->get_vector());
   if (side == -1) return;

   Cell * sea_cell = (get_cell(target_cell->i, target_cell->j, side));

   if (sea_cell == 0 || !(sea_cell->elevation < 0)) return;

   sea_cell->elevation += 10;
   sea_cell->changed    = true;
   sea_cell->troops     = 0;
   sea_cell->changed    = true;
}


// ============================================================================
// Scuttle a base
// ============================================================================

void Board::scuttle(int player, Cell * target_cell)
{
   if (target_cell->player != player) return;
   if (target_cell->type != Base)     return;

   target_cell->strength -= 10;
   target_cell->changed   = true;

   if (target_cell->strength <= 0) {
      target_cell->strength = 0;
      target_cell->type     = Empty;
   }
}

⌨️ 快捷键说明

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