📄 slugsengine.java
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: SlugsEngine.java,v 1.2 2003/05/29 22:39:15 wildcard Exp $ */package com.sun.j2me.blueprints.slugs.server;import com.sun.j2me.blueprints.slugs.shared.*;import java.io.*;import java.util.List;import java.util.Vector;/** * This class implements the actual game engine * for the slugs game. It is concerned only with * game state, all visualization is handled by * the client. * <p> * The error handling approach taken here is very * simplistic, and not suitable for production * quality code. Upon encountering a problem, a * message is printed, and the application exits. */public class SlugsEngine implements Runnable, GameEngine, PlayerActions { private CommunicationDelegate del; private int frame_count; private int player_count; private int level_no; private int level_width; private int level_height; private boolean[] player_connected; private Player[] player_list; private Item[] item_list; private Item[] playing_field; private List events; private boolean level_complete; private boolean level_started; private boolean game_started; private boolean restart; private boolean players_die; /** * Create a new instance of this class */ public SlugsEngine() { frame_count = 0; player_count = 0; level_no = 0; players_die = true; String value = System.getProperty("players.die"); if (value != null) { players_die = (new Boolean(value)).booleanValue(); } events = new Vector(); game_started = false; player_list = new Player[getMaxPlayerCount()]; for (int i = 0; i < player_list.length; i++) { player_list[i] = new Player(-1, -1, i); Player player = player_list[i]; player.setLives(3); events.add(new Item(player.getLives(), i, Item.LIFE_COUNT)); } player_connected = new boolean[getMaxPlayerCount()]; for (int i = 0; i < player_connected.length; i++) { player_connected[i] = false; } initLevel(); } private void handleError(String msg) { System.out.println("***: " + msg); System.exit(-1); } private void placeItem(Item item, String string) { int n, x, y; x = item.getX(); y = item.getY(); n = y * level_width + x; if (x < 0 || x >= level_width || y < 0 || y >= level_height) { handleError("invalid position for " + string + ": x = " + x + ", y = " + y); } if (playing_field[n] != null) { handleError("position conflict between " + item + " and " + playing_field[n]); } playing_field[n] = item; } private void restart() { level_no = 0; game_started = true; for (int i = 0; i < player_list.length; i++) { player_list[i].setLives(3); player_list[i].setJetpackCount(0); player_list[i].setShovelCount(0); } initLevel(); } private void initLevel() { Level level; Item item; Item[] object_list; int i; String filename = "rsrc/level" + level_no + ".map"; level = null; InputStream is = getClass().getResourceAsStream(filename); if (is == null) { handleError("can't read level data for: " + filename); } BufferedReader in = new BufferedReader(new InputStreamReader(is)); level = new Level(in, player_list); level_width = level.getWidth(); level_height = level.getHeight(); object_list = level.getObjectData(); item_list = new Item[level_width * level_height + getMaxPlayerCount() + 10]; for (i = 0; i < item_list.length; i++) { item_list[i] = new Item(0, 0, 0); } playing_field = new Item[level_width * level_height]; for (i = 0; i < object_list.length; i++) { placeItem(object_list[i], "object " + (i + 1)); } for (i = 0; i < player_list.length; i++) { placeItem(player_list[i], "player " + (i + 1)); } level_started = true; level_complete = false; restart = false; dumpPlayingField(); } private void dumpPlayingField() { Item item; int x, y; char c; c = (char) 0; for (y = 0; y < level_height; y++) { for (x = 0; x < level_width; x++) { item = playing_field[y * level_width + x]; if (item instanceof Player) { if (item == player_list[0]) { c = '1'; } else if (item == player_list[1]) { c = '2'; } else { handleError("rogue player detected"); } } else { switch (item.getStatus()) { case Item.JET_PACK: c = '^'; break; case Item.SHOVEL: c = 'v'; break; case Item.BOOSTER: c = '>'; break; case Item.EXTRA_LIFE: c = '!'; break; case Item.POISON: c = '*'; break; case Item.WALL: c = '|'; break; case Item.HOUSE: c = '@'; break; case Item.PATH1: c = '-'; break; case Item.PATH2: c = '='; break; case Item.GRASS: c = '.'; break; default: handleError("should not reach here"); } } System.out.print(c); } System.out.println(); } System.out.println(); } /** * set the delegate which will handle client communication */ public void setDelegate(CommunicationDelegate del) { this.del = del; } /** * Add a player to the game. Returns the player id upon * success, or a negative number if the maximum number * of players has been reached. */ public synchronized int addPlayer() { if (player_count >= getMaxPlayerCount()) { return -1; } for (int i = 0; i < player_connected.length; i++) { if (!player_connected[i]) { player_connected[i] = true; if (++player_count == getMaxPlayerCount()) { restart(); } return i; } } handleError("should not reach here"); return -1; } /** * Remove player with id 'id' from the game */ public synchronized void removePlayer(int id) { player_connected[id] = false; player_count--; } /** * Update the game state according to action 'action' by the player * with player id 'player_id'. The action is one of LEFT, RIGHT, UP, * DOWN, JUMP or DIG. Frame number 'frame_no' from the incoming client * message is used to detect outdated or out-of-sync messages. */ public synchronized void updatePlayer(int frame_no, int player_id, int action) { if (player_count < getMaxPlayerCount()) { return; } if (frame_no != frame_count && frame_no != frame_count - 1 && frame_no != frame_count - 2) { // accept older frames for now } Player player = player_list[player_id]; switch (action) { case LEFT: player.setDirection(Player.WEST); break; case RIGHT:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -