📄 game.java
字号:
/* Bomber for Nokia Series 60 Phones Copyright (C) 2003, 2004 While True, d.o.o. 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 For any info contact gorazd@whiletrue.com.*//*==========================================================================; * * While True, d.o.o. * * File: Game.java * Content: Game class * Created: November 2002 * Created by: gorazd breskvar * ****************************************************************************/package bomber;import java.util.Vector;import javax.microedition.lcdui.*;// =========================================================================;// Name: Game// Desc: Class that contains game state (and all game objects)// ==========================================================================;public class Game implements GameState{ // game state types protected static final byte PLAYING = 0; protected static final byte GAME_OVER = 1; protected static final byte BRIEFING = 2; protected static final byte LEVEL_COMPLETE = 3; protected static final byte CUSTOM = 4; // modes protected static final byte NORMAL = 0; protected static final byte INTRO = 1; protected Vector m_objects; // all object currently flying around protected Vector m_visual; // all visual objects (clouds, smoke, etc.) protected Terrain m_terrain; // terrain object protected int m_full_width; // full width of the image protected int m_full_height; // full height of the image protected int m_view_start_height; // starting height of the viewport protected int m_view_height; // height of the viewport protected int m_scroll_x; // current position of the viewport protected int m_scroll_y; protected int m_scroll_offset_x; // for addition scroll in the direction of movement protected int m_bound_left; protected int m_bound_right; protected int m_bound_top; protected byte m_game_state_type; // current game state type (game, briefing, game over, etc) protected boolean m_handling = true; // is handle called for all object (eg. for pause) protected boolean m_is_lookahead = true; protected int m_handle_speed_factor = Common.FIXED; protected String m_level_name; protected short[] m_mission_data = null; protected ResourceManager m_resource_manager; protected GameHandler m_game_handler; protected boolean m_quit; // quit the game? protected byte[] m_special_state; // special state flags protected int m_score; // current score protected GameObject[] m_targets; // targets for current game (if any) protected byte m_level; // current level of the game protected byte m_difficulty_setting; // difficulty settings for the current game protected final static int MAX_OBJECT_RADIUS = 50 * Common.FIXED; protected final static int[] m_plane_info = { // plane 1 (player's plane) 80, // turn rate 250, // fire rate 50, // engine 7, // mass 2048, // hitpoints // plane 2 (very easy enemy) 160, // turn rate 750, // fire rate 50, // engine 9, // mass 1024, // hitpoints // plane 3 100, // turn rate 500, // fire rate 40, // engine 7, // mass 2048, // hitpoints // plane 4 100, // turn rate 300, // fire rate 50, // engine 7, // mass 2048, // hitpoints }; protected final static int[] c_water_table = { 0xf0f0ff, 0xeeeeff, 0xd0d0ff, 0xddddff, 0xffffff }; protected final static int[] c_tree_table = { 0x6AC78E, 0x46D875, 0x137E3A, 0xBC581C, }; protected final static int[] c_ground_table = { 0x000000, 0x400000, 0x201010, }; private final static int[] c_explosion_color_table = { 0xff0000, 0xff4040, 0xff6060, 0xff8080 }; private final static int[] c_fireworks_red_color_table = { 0xff0000 }; private final static int[] c_fireworks_blue_color_table = { 0x0000ff }; private final static int[] c_fireworks_green_color_table = { 0x00ff00 }; private final static int[] c_spark_color_table = { 0xFDE059, 0xffff40, 0xffff80, 0xffffff }; private void init(ResourceManager rm, int width, int height) { m_objects = new Vector(); m_visual = new Vector(); m_resource_manager = rm; m_full_width = width; m_full_height = height; m_special_state = new byte[SPECIAL_STATE_SIZE]; resetScreenOptions(); m_score = 0; m_level = 0; } public Game(ResourceManager rm, int width, int height, byte mode) throws Exception { init(rm, width, height); if (mode == NORMAL) { reset(); } else if (mode == INTRO) { m_game_state_type = CUSTOM; m_terrain = m_resource_manager.loadTerrain(this, 0, true); m_resource_manager.loadLevel(this, -1); m_game_handler = new IntroGameHandler(this); } } private void makeTargets(int flags) { int size = 0; for (int i = 0; i < getObjectSize(); i++) { if (getGameObject(i).getFlags() == flags) { size++; } } m_targets = new GameObject[size]; size = 0; for (int i = 0; i < m_objects.size(); i++) { if (getGameObject(i).getFlags() == flags) { m_targets[size++] = getGameObject(i); } } } public String getDifficultyBonusMultiplyerAsString() { switch (m_difficulty_setting) { case 0 : return "X 1"; case 1 : return "X 1.5"; default: return "X 2"; } } protected void loadLevel(int level) throws Exception { int l = level % 5; switch (m_difficulty_setting) { case 0 : m_level_name = Str.level + Integer.toString(m_level + 1) + Str.level_name_easy; break; case 1 : m_level_name = Str.level + Integer.toString(m_level + 1) + Str.level_name_normal; break; default: m_level_name = Str.level + Integer.toString(m_level + 1) + Str.level_name_hard; break; } if (l == 2) { m_terrain = m_resource_manager.loadTerrain(this, 0, true); m_resource_manager.loadLevel(this, 0); makeTargets(level % 2 + 1); briefing(Str.mission_1_briefing); } else if (l == 0) { m_terrain = m_resource_manager.loadTerrain(this, 1, true); m_resource_manager.loadLevel(this, 1); makeTargets(1); briefing(Str.mission_2_briefing); } else if (l == 1) { m_terrain = m_resource_manager.loadTerrain(this, 2, false); m_resource_manager.loadLevel(this, 2); makeTargets(1); briefing(Str.mission_3_briefing); } else if (l == 3) { m_terrain = m_resource_manager.loadTerrain(this, 3, false); m_resource_manager.loadLevel(this, 3); makeTargets(1); briefing(Str.mission_zeppelin_briefing); } else if (l == 4) { m_terrain = m_resource_manager.loadTerrain(this, 4, false); m_resource_manager.loadLevel(this, 4); makeTargets(1); briefing(Str.mission_sub_briefing); } handle(0); } protected void reset() throws Exception { for (int i = 0; i < m_objects.size(); i++) { getGameObject(i).destruct(); } m_objects.removeAllElements(); m_visual.removeAllElements(); if (m_terrain != null) m_terrain.destruct(); for (int i = 0; i < SPECIAL_STATE_SIZE; i++) m_special_state[i] = 0; m_difficulty_setting = m_resource_manager.getStorage().getDifficultyLevel(); loadLevel(m_level); System.gc(); } protected int binaryFindObject(int val) { int min = 0; int max = m_objects.size(); int i = min; while (min < max) { i = (min + max) / 2; if (((GameObject)m_objects.elementAt(i)).getPos().x < val) min = i + 1; else if (((GameObject)m_objects.elementAt(i)).getPos().x > val) max = i - 1; else return i; } return i; } void insertionSort() { int n = m_objects.size(); int pos_place_to_insert; // index of possible location to insert the // first unsorted object GameObject to_insert; // value to be inserted for (int i = 1; i < n; i++) { // objects 0.. i are sorted if ( ((GameObject)m_objects.elementAt(i)).getPos().x < ((GameObject)m_objects.elementAt(i - 1)).getPos().x ) { to_insert = (GameObject)m_objects.elementAt(i); pos_place_to_insert = i; do { pos_place_to_insert--; // move the possible insertion place down by m_objects.setElementAt(m_objects.elementAt(pos_place_to_insert), pos_place_to_insert + 1); } while ((pos_place_to_insert > 0) && (to_insert.getPos().x < ((GameObject)m_objects.elementAt(pos_place_to_insert - 1)).getPos().x)); m_objects.setElementAt(to_insert,pos_place_to_insert); } } } public void addObject(GameObject go) { //m_objects.insertElementAt(go, binaryFindObject(go.getPos().x)); m_objects.addElement(go); } public void setBounds(int top, int left, int right) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -