技术开发 频道

Java中2.5D游戏八方走法实现原理及相关代码

  RprMap.java(负责描述角色单元集合在地图上的具体位置)

 

  1 package org.loon.game.simple.alldirection.rpg;
  2 import java.awt.Color;
  3 import java.awt.Graphics;
  4 import java.io.IOException;
  5 import java.util.List;
  6 import org.loon.game.simple.alldirection.GraphicsUtils;
  7 import org.loon.game.simple.alldirection.LSystem;
  8 public class RpgMap implements Config {
  9     private ImageMapFactory imageMap;
10     private Roles roles;
11     private boolean showGrid;
12     private Field2D map2d;
13     private int firstTileX;
14     private int firstTileY;
15     private int lastTileX;
16     private int lastTileY;
17     public RpgMap(String imageFile, String mapFile) {
18         try {
19             imageMap = new ImageMapFactory(imageFile, mapFile);
20         } catch (IOException e) {
21             throw new RuntimeException(e);
22         }
23         this.map2d = new Field2D(imageMap.getMap());
24         this.roles = new Roles();
25     }
26     public void addRole(Role role) {
27         roles.addChara(role);
28     }
29     public Role getHero() {
30         return roles.getHero();
31     }
32     public void setupHero(Role hero) {
33         roles.mainHero(hero);
34     }
35     public synchronized void draw(Graphics g, int offsetX, int offsetY) {
36         firstTileX = pixelsToTiles(-offsetX);
37         lastTileX = firstTileX + pixelsToTiles(LSystem.WIDTH) + 1;
38         lastTileX = Math.min(lastTileX, getRow());
39         firstTileY = pixelsToTiles(-offsetY);
40         lastTileY = firstTileY + pixelsToTiles(LSystem.HEIGHT) + 1;
41         lastTileY = Math.min(lastTileY, getCol());
42         for (int i = firstTileX; i < lastTileX; i++) {
43             for (int j = firstTileY; j < lastTileY; j++) {
44                 g.drawImage(imageMap.getImages()[i][j], tilesToPixels(i)
45                         + offsetX, tilesToPixels(j) + offsetY, null);
46                 if (showGrid) {
47                     if (imageMap.getMap()[j][i] == 1) {
48                         g.setColor(Color.white);
49                         g.drawRect(tilesToPixels(i) + offsetX, tilesToPixels(j)
50                                 + offsetY, CS - 2, CS - 2);
51                         GraphicsUtils.setAlpha(g, 0.5d);
52                         g.fillRect(tilesToPixels(i) + offsetX, tilesToPixels(j)
53                                 + offsetY, CS - 2, CS - 2);
54                         GraphicsUtils.setAlpha(g, 1.0d);
55                     } else if (imageMap.getMap()[j][i] == -1) {
56                         g.setColor(Color.blue);
57                         g.drawRect(tilesToPixels(i) + offsetX, tilesToPixels(j)
58                                 + offsetY, CS - 2, CS - 2);
59                         GraphicsUtils.setAlpha(g, 0.3d);
60                         g.fillRect(tilesToPixels(i) + offsetX, tilesToPixels(j)
61                                 + offsetY, CS - 2, CS - 2);
62                         GraphicsUtils.setAlpha(g, 1.0d);
63                     }
64                 }
65             }
66         }
67         roles.draw(g, offsetX, offsetY);
68     }
69     public int getSelfFirstX() {
70         return firstTileX;
71     }
72     public int getSelfFirstY() {
73         return firstTileY;
74     }
75     public int getSelfLastX() {
76         return lastTileX;
77     }
78     public int getSelfLastY() {
79         return lastTileY;
80     }
81     public int getSelfFirstWidth() {
82         return tilesToPixels(firstTileX);
83     }
84     public int getSelfFirstHeight() {
85         return tilesToPixels(firstTileY);
86     }
87     public int getSelfLastWidth() {
88         return tilesToPixels(lastTileX);
89     }
90     public int getSelfLastHeight() {
91         return tilesToPixels(lastTileY);
92     }
93     public List findPath(Role hero, Cell2D goal) {
94         return AStarFinder.find(map2d, hero.getCell2D(), goal);
95     }
96     public void showGrid(boolean show) {
97         this.showGrid = show;
98     }
99     public ImageMapFactory getFactory() {
100         return imageMap;
101     }
102     public boolean isHit(int x, int y) {
103         try {
104             int[][] map = imageMap.getMap();
105             if (map[y][x] == 1) {
106                 return true;
107             }
108             if (roles.isHit(x, y)) {
109                 return true;
110             }
111             return false;
112         } catch (Exception e) {
113             return false;
114         }
115     }
116     public static int pixelsToTiles(double pixels) {
117         return (int) Math.floor(pixels / CS);
118     }
119     public static int tilesToPixels(int tiles) {
120         return tiles * CS;
121     }
122     public int getRow() {
123         return imageMap.getMapWidth();
124     }
125     public int getCol() {
126         return imageMap.getMapHeight();
127     }
128     public int getWidth() {
129         return imageMap.getImageWidth();
130     }
131     public int getHeight() {
132         return imageMap.getImageHeight();
133     }
134     public Roles getRoles() {
135         return roles;
136     }
137 }

 

0
相关文章