// $Id: mapengine.cc,v 1.4 2006/08/05 09:08:59 matthew Exp $ // Fish Supper // Copyright (C) 2006 Matthew Clarke // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. #include "mapengine.h" #include // ******************* // *** CONSTRUCTOR *** // ******************* FS::MapEngine::MapEngine() { my_blocks_allocated = false; } // FS::MapEngine::MapEngine() // ****************** // *** DESTRUCTOR *** // ****************** FS::MapEngine::~MapEngine() { if (my_blocks_allocated) { for (int i = 0; i < NUM_ROWS; ++i) { delete [] my_blocks[i]; } // for } // if } // FS::MapEngine::~MapEngine() // ************************ // *** MEMBER FUNCTIONS *** // ************************ // ************************************************** void FS::MapEngine::load_map(std::ifstream & fin) { std::string my_line; // 1 row at a time: direction, velocity, row length, data for (int r = 0; r < NUM_ROWS; ++r) { int len; // use this to control inner for loop // direction getline(fin, my_line); directions[r] = original_directions[r] = (Direction) atoi( my_line.c_str() ); // velocity getline(fin, my_line); velocities[r] = original_velocities[r] = (float) atof( my_line.c_str() ); // row length getline(fin, my_line); len = atoi( my_line.c_str() ); row_lengths[r] = len; row_lengths_pixels[r] = len * BLOCK_WIDTH; // do we need to delete [] first? if (my_blocks_allocated) { delete [] my_blocks[r]; } // if my_blocks[r] = new Block[len]; for (int c = 0; c < len; ++c) // 'c' for column { getline(fin, my_line); // log my_blocks[r][c].lc = (LogColor) atoi( my_line.c_str() ); my_line.erase( 0, my_line.find(',') + 1 ); // crystal my_blocks[r][c].cc = (CrystalColor) atoi( my_line.c_str() ); my_line.erase( 0, my_line.find(',') + 1 ); // bonus my_blocks[r][c].bt = (BonusType) atoi( my_line.c_str() ); } // for c } // for r // set this to make sure memory cleared next time before loading // new data my_blocks_allocated = true; } // FS::MapEngine::load_map() // ************************************************** void FS::MapEngine::update(int time_passed) { int temp_pixel; for (int r = 0; r < NUM_ROWS; ++r) { old_pixels_to_move[r] = pixels_to_move[r]; pixels_to_move[r] = (int) ( velocities[r] * (time_passed - start_times[r]) ); switch(directions[r]) { case LEFT_TO_RIGHT: temp_pixel = pixels_to_move[r] % row_lengths_pixels[r]; if ( temp_pixel > 0 ) { temp_pixel = start_pixel_offsets[r] - temp_pixel; if ( temp_pixel < 0 ) { temp_pixel += row_lengths_pixels[r]; } // if current_start_pixels[r] = temp_pixel; } else { current_start_pixels[r] = start_pixel_offsets[r]; } // if ... else break; case RIGHT_TO_LEFT: temp_pixel = pixels_to_move[r] % row_lengths_pixels[r]; current_start_pixels[r] = (temp_pixel + start_pixel_offsets[r]) % row_lengths_pixels[r]; break; } // switch } // for } // FS::MapEngine::update() // ************************************************** /* void FS::MapEngine::reset() { for (int i = 0; i < NUM_ROWS; ++i) { old_pixels_to_move[i] = 0; } // for } // FS::MapEngine::reset() */ // ************************************************** void FS::MapEngine::reset() { for (int i = 0; i < NUM_ROWS; ++i) { old_pixels_to_move[i] = pixels_to_move[i] = 0; start_pixel_offsets[i] = 0; start_times[i] = 0; } // for } // FS::MapEngine::reset() // ************************************************** void FS::MapEngine::restart_level(int time_passed) { for (int i = 0; i < NUM_ROWS; ++i) { bool changed = false; if ( directions[i] != original_directions[i] ) { directions[i] = original_directions[i]; changed = true; } // if if ( velocities[i] != original_velocities[i] ) { velocities[i] = original_velocities[i]; changed = true; } // if if (changed) { start_pixel_offsets[i] = current_start_pixels[i]; start_times[i] = time_passed; old_pixels_to_move[i] = pixels_to_move[i] = 0; } // if } // for } // FS::MapEngine::restart_level() // ************************************************** void FS::MapEngine::change_direction(int row, int time_passed) { directions[row] = ((directions[row] == LEFT_TO_RIGHT) ? RIGHT_TO_LEFT : LEFT_TO_RIGHT); start_pixel_offsets[row] = current_start_pixels[row]; start_times[row] = time_passed; old_pixels_to_move[row] = pixels_to_move[row] = 0; } // FS::MapEngine::change_direction() // ************************************************** void FS::MapEngine::slow_down(int row, int time_passed) { // FIXME: use a symbolic constant for this value velocities[row] *= 0.5; start_pixel_offsets[row] = current_start_pixels[row]; start_times[row] = time_passed; old_pixels_to_move[row] = pixels_to_move[row] = 0; } // FS::MapEngine::slow_down() // ************************************************** void FS::MapEngine::speed_up(int row, int time_passed) { // FIXME: use a symbolic constant for this value velocities[row] *= 1.5; start_pixel_offsets[row] = current_start_pixels[row]; start_times[row] = time_passed; old_pixels_to_move[row] = pixels_to_move[row] = 0; } // FS::MapEngine::speed_up() // **************************************************