📄 level.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: Level.java,v 1.1 2003/05/16 00:16:01 zull Exp $ */package com.sun.j2me.blueprints.slugs.server;import com.sun.j2me.blueprints.slugs.shared.*;import java.io.BufferedReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Vector;/** * This class knows how to parse an ASCII level description. * Whitespace (leading and trailing blanks, tabs and comments * marked by '#') will be discarded during parsing. The number * of non-whitespace characters per line needs to be constant * across lines. Recognized characters are '.' (grass), '!' * (extra life), '*' (poison), '|' (wall), '^' (jet pack), * '>' (booster), 'v' (shovel) and '@' (house). */public class Level { private int width; private int height; private Item[] objectData; /** * Construct a new instance from a level description * read from 'in'. During the initialization process * 'player_list' will be reset to values found in * the level description. */ public Level(BufferedReader in, Player[] player_list) { String line; int c, i, line_length, x, y; List objects; y = 0; line_length = -1; objects = new Vector(); for (i = 0; i < player_list.length; i++) { player_list[i].setX(-1); player_list[i].setY(-1); player_list[i].setDirection(Player.STILL); } for (; ; ) { line = readLine(in); if (line == null) { break; } if (line_length < 0) { line_length = line.length(); } else if (line_length != line.length()) { throw new RuntimeException("all lines in map must have same length"); } for (x = 0; x < line_length; x++) { c = line.charAt(x); switch (c) { case '.': objects.add(new Item(x, y, Item.GRASS)); break; case '!': objects.add(new Item(x, y, Item.EXTRA_LIFE)); break; case '*': objects.add(new Item(x, y, Item.POISON)); break; case '|': objects.add(new Item(x, y, Item.WALL)); break; case '^': objects.add(new Item(x, y, Item.JET_PACK)); break; case '>': objects.add(new Item(x, y, Item.BOOSTER)); break; case 'v': objects.add(new Item(x, y, Item.SHOVEL)); break; case '@': objects.add(new Item(x, y, Item.HOUSE)); break; case '1': // fall through case '2': i = c - '1'; if (player_list[i].getX() >= 0 || player_list[i].getY() >= 0) { throw new RuntimeException("multiple definitions for player " + c); } player_list[i].setX(x); player_list[i].setY(y); break; default: throw new RuntimeException("unknown map character: " + c); } } y++; } for (i = 0; i < player_list.length; i++) { if (player_list[i].getX() < 0 || player_list[i].getY() < 0) { throw new RuntimeException("missing definition for player " + (i + 1)); } } width = line_length; height = y; objectData = new Item[objects.size()]; ArrayList arrayList = new ArrayList(objects); objectData = (Item[]) arrayList.toArray(objectData); } /** * Return the width of the level in cells */ public int getWidth() { return width; } /** * Return the height of the level in cells */ public int getHeight() { return height; } /** * Return an item array with an item for each cell in the level */ public Item[] getObjectData() { return objectData; } private String readLine(BufferedReader in) { String line; int pound_index; for (; ; ) { try { line = in.readLine(); } catch (IOException e) { return null; } if (line == null) { return null; } pound_index = line.indexOf('#'); if (pound_index >= 0) { line = line.substring(0, pound_index); } line = line.trim(); if (line.length() == 0) { continue; } return line; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -