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

📄 defaultmap.java.svn-base

📁 一个JAVA程序员的游戏
💻 SVN-BASE
字号:
/*
 * LevelMapImpl.java
 *
 * Created on 16. Dezember 2006, 17:25
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kanjitori.map;

import java.awt.Dimension;
import java.awt.Point;
import java.util.Arrays;
import java.util.Random;
import kanjitori.map.Content;
import kanjitori.map.Layer;
import kanjitori.map.Map;

/**
 *
 * @author Pirx
 */
public class DefaultMap implements Map {
    
    private static final Random RANDOM = new Random();
    
    private Dimension dim;
    private Layer[] layers;
    private Content[][] contents;
    private String[] skyboxTextures;
    private int botCount;
    private String name;
    
    /** Creates a new instance of LevelMapImpl */
    public DefaultMap(String name, Dimension dim, int layerCount, String[] skyboxTextures, int botCount) {
        this.skyboxTextures = skyboxTextures;
        this.botCount = botCount;
        this.name = name;
        this.dim = dim;
        this.layers = new Layer[layerCount];
        contents = new Content[dim.width][dim.height];
        for (int x = 0; x < dim.width; x++) {
            Arrays.fill(contents[x], Content.EMPTY);
        }
    }
    
    public DefaultMap() {
    }

    public String[] getSkyboxTextures() {
        return skyboxTextures;
    }

    public int getBotCount() {
        return botCount;
    }

    public String getName() {
        return name;
    }

    private final static int[][] NEIGHBORS = new int[][] 
    { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    public Point findFreePos() {
        outer:
        while(true) {
            int x = RANDOM.nextInt(dim.width);
            int y = RANDOM.nextInt(dim.height);
            if (contents[x][y] == Content.EMPTY) {
                for (int i = 0; i < NEIGHBORS.length; i++) {
                    int xn = x + NEIGHBORS[i][0];
                    int yn = y + NEIGHBORS[i][1];
                    if (isValidPos(xn, yn) && contents[xn][yn] == Content.BOT) {
                        continue outer;
                    }
                }
                return new Point(x, y);
            }
        }
    }
    
    public boolean isValidPos(int x, int y) {
        return 0 <= x && x < dim.width && 0 <= y && y < dim.height;
    }

    public Dimension getSize() {
        return dim;
    }

    public Content getContent(int x, int y) {
        if (isValidPos(x, y)) {
           return contents[x][y];
        } else {
            return Content.SOLID;
        }
    }

    public void setContent(int x, int y, Content content) {
        if (isValidPos(x, y)) {
           contents[x][y] = content;
        }
    }

    public int getLayerCount() {
        return layers.length;
    }

    public Layer getLayer(int index) {
        return layers[index];
    }
    
     public void setLayer(int index, Layer layer) {
        if (index >= getLayerCount() || ! layer.getSize().equals(getSize())) {
            throw new IllegalArgumentException("wrong layer size");
        }
        layers[index] = layer;
    }

    public void setSize(Dimension dim) {
        this.dim = dim;
        contents = new Content[dim.width][dim.height];
        for (int x = 0; x < dim.width; x++) {
            Arrays.fill(contents[x], Content.EMPTY);
        }
    }

    public void setLayers(Layer[] layers) {
        this.layers = layers;
    }

    public void setSkyboxTextures(String... skyboxTextures) {
        this.skyboxTextures = skyboxTextures;
    }

    public void setBotCount(int botCount) {
        this.botCount = botCount;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

⌨️ 快捷键说明

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