1 | using Microsoft.Xna.Framework; |
---|
2 | |
---|
3 | namespace CastleMaster.Graphics |
---|
4 | { |
---|
5 | public static class Viewport |
---|
6 | { |
---|
7 | public const float TILESIZE = 16.0F; |
---|
8 | public const int FLOOR_TILE_WIDTH = 32; |
---|
9 | public const int FLOOR_TILE_HEIGHT = 16; |
---|
10 | public const int X_STEP = FLOOR_TILE_WIDTH / 2; |
---|
11 | public const int Y_STEP = FLOOR_TILE_HEIGHT / 2; |
---|
12 | public const float X_SCALE = X_STEP / TILESIZE; |
---|
13 | public const float Y_SCALE = Y_STEP / TILESIZE; |
---|
14 | public const float MAX_ZOOM = 3.0F; |
---|
15 | public const float MIN_ZOOM = 1.0F; |
---|
16 | public const float ZOOM_STEP = 0.25F; |
---|
17 | |
---|
18 | private static float xScaleZoomed = X_SCALE, yScaleZOomed = Y_SCALE; |
---|
19 | private static float zoom = 1.0F; |
---|
20 | |
---|
21 | public static float X_SCALE_ZOOMED { get { return xScaleZoomed; } } |
---|
22 | |
---|
23 | public static float Y_SCALE_ZOOMED { get { return yScaleZOomed; } } |
---|
24 | |
---|
25 | public static float ZOOM |
---|
26 | { |
---|
27 | get { return zoom; } |
---|
28 | set |
---|
29 | { |
---|
30 | if (value < MIN_ZOOM || value > MAX_ZOOM) return; |
---|
31 | zoom = value; |
---|
32 | xScaleZoomed = X_SCALE * zoom; |
---|
33 | yScaleZOomed = Y_SCALE * zoom; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | public static Vector2 WorldToScreen(float x, float z) |
---|
38 | { |
---|
39 | return new Vector2((x- z) * X_SCALE_ZOOMED, (x + z) * Y_SCALE_ZOOMED); |
---|
40 | } |
---|
41 | |
---|
42 | public static Vector2 ScreenToWorld(int x, int y) |
---|
43 | { |
---|
44 | float xRegion = (x / ZOOM) / X_SCALE; |
---|
45 | float yRegion = (y / ZOOM) / Y_SCALE; |
---|
46 | |
---|
47 | return new Vector2((xRegion + yRegion) / 2 - Viewport.TILESIZE, (yRegion - xRegion) / 2); |
---|
48 | } |
---|
49 | } |
---|
50 | } |
---|