1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using CastleMaster.World.Tiles; |
---|
6 | using CastleMaster.Graphics; |
---|
7 | using Microsoft.Xna.Framework; |
---|
8 | |
---|
9 | namespace CastleMaster.World |
---|
10 | { |
---|
11 | public class Level |
---|
12 | { |
---|
13 | public const int TILE_VOID = 0; |
---|
14 | protected List<Tile> registeredTiles; |
---|
15 | protected int[] tiles; |
---|
16 | protected byte[] data; |
---|
17 | private int width, height; |
---|
18 | |
---|
19 | public Level(int width, int height) |
---|
20 | { |
---|
21 | this.width = width; |
---|
22 | this.height = height; |
---|
23 | |
---|
24 | tiles = new int[width * height]; |
---|
25 | data = new byte[width * height]; |
---|
26 | registeredTiles = new List<Tile>(); |
---|
27 | |
---|
28 | new Tile(this); |
---|
29 | InitTiles(); |
---|
30 | |
---|
31 | InitLevel(); |
---|
32 | } |
---|
33 | |
---|
34 | public int Width { get { return width; } } |
---|
35 | |
---|
36 | public int Height { get { return height; } } |
---|
37 | |
---|
38 | public List<Tile> RegisteredTiles { get { return registeredTiles; } } |
---|
39 | |
---|
40 | protected virtual void InitTiles() { } |
---|
41 | |
---|
42 | protected virtual void InitLevel() { } |
---|
43 | |
---|
44 | public void RenderBackground(Camera camera, RenderHelper renderer) |
---|
45 | { |
---|
46 | float w = (Game.WIDTH / Viewport.FLOOR_TILE_WIDTH) / Viewport.ZOOM; |
---|
47 | float h = (Game.HEIGHT / Viewport.Y_STEP) / Viewport.ZOOM; |
---|
48 | |
---|
49 | int x0 = (int)((camera.XLeft / Viewport.FLOOR_TILE_WIDTH) / Viewport.ZOOM) - 3; |
---|
50 | int y0 = (int)((camera.YTop / Viewport.Y_STEP) / Viewport.ZOOM) - 5; |
---|
51 | int x1 = (int)(x0 + w) + 5; |
---|
52 | int y1 = (int)(y0 + h) + 7; |
---|
53 | |
---|
54 | renderer.SetOffset(camera); |
---|
55 | int tp; |
---|
56 | for (int y = y0; y < y1; y++) |
---|
57 | { |
---|
58 | for (int x = x0; x < x1; x++) |
---|
59 | { |
---|
60 | int xTile = x + (y >> 1) + (y & 1); |
---|
61 | int zTile = (y >> 1) - x; |
---|
62 | |
---|
63 | if (xTile >= 0 && zTile >= 0 && xTile < width && zTile < height) |
---|
64 | { |
---|
65 | tp = xTile + zTile * width; |
---|
66 | Tile t = registeredTiles[tiles[tp]]; |
---|
67 | if (t.ID != TILE_VOID) |
---|
68 | t.Render(renderer, new Vector2((x * Viewport.FLOOR_TILE_WIDTH + (y & 1) * Viewport.X_STEP) * Viewport.ZOOM, (y * Viewport.Y_STEP + Viewport.Y_STEP) * Viewport.ZOOM), xTile, zTile, data[tp]); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | renderer.SetOffset(); |
---|
74 | } |
---|
75 | |
---|
76 | public void SetTile(int tileX, int tileZ, int tileID) |
---|
77 | { |
---|
78 | if (IsValidPos(tileX, tileZ)) |
---|
79 | tiles[tileX + tileZ * width] = tileID; |
---|
80 | } |
---|
81 | |
---|
82 | public void SetData(int tileX, int tileZ, byte dataVal) |
---|
83 | { |
---|
84 | if (IsValidPos(tileX, tileZ)) |
---|
85 | data[tileX + tileZ * width] = dataVal; |
---|
86 | } |
---|
87 | |
---|
88 | public Tile GetTile(int tileX, int tileZ) |
---|
89 | { |
---|
90 | return IsValidPos(tileX, tileZ) ? registeredTiles[tiles[tileX + tileZ * width]] : registeredTiles[TILE_VOID]; |
---|
91 | } |
---|
92 | |
---|
93 | public byte GetData(int tileX, int tileZ) |
---|
94 | { |
---|
95 | return IsValidPos(tileX, tileZ) ? data[tileX + tileZ * width] : (byte)0; |
---|
96 | } |
---|
97 | |
---|
98 | private bool IsValidPos(int tileX, int tileZ) |
---|
99 | { |
---|
100 | return (tileX >= 0 && tileZ >= 0 && tileX < width && tileZ < height); |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|