source: 2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/BlockLevel.cs @ 2633

Revision 2633, 6.6 KB checked in by hniemi, 11 years ago (diff)

Toiminnallisuutta jaettu luokkiin

Line 
1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using Jypeli;
7
8public class BlockLevel
9{
10    private Block[,] playfield;
11    private Blockpusher game;
12    private Dictionary<char, BlockType> charColor = new Dictionary<char, BlockType>();
13    private List<Block> goals = new List<Block>();
14
15    private int playerX;
16    private int playerY;
17
18    public int PlayfieldHeight { get { return playfield.GetLength(1); } }
19    public int PlayfieldWidth { get { return playfield.GetLength(0); } }
20
21    /// <summary>
22    /// Constructor from game
23    /// </summary>
24    /// <param name="game">Game that level is used in</param>
25    public BlockLevel(Blockpusher game)
26    {
27        this.game = game;
28        InitCharColors();
29    }
30
31    /// <summary>
32    /// Moves player x and y steps
33    /// </summary>
34    /// <param name="x">X Movement</param>
35    /// <param name="y">Y Movement</param>
36    public void MovePlayer(int x, int y)
37    {
38        switch (Type(playerX + x, playerY + y))
39        {
40            case BlockType.SatisfiedGoalZone:
41            case BlockType.Movable:
42                int nextX = playerX + x;
43                if (x != 0) { nextX += (x / Math.Abs(x)); }
44                int nextY = playerY + y;
45                if (y != 0) { nextY += (y / Math.Abs(y)); }
46                BlockType t = Type(nextX, nextY);
47                if (t == BlockType.Empty || t == BlockType.GoalZone || t == BlockType.SatisfiedGoalZone)
48                {
49                    Move(playerX + x, playerY + y, nextX, nextY);
50                    Move(playerX, playerY, playerX + x, playerY + y);
51                    playerX += x;
52                    playerY += y;
53                }
54
55                break;
56            case BlockType.GoalZone:
57            case BlockType.Empty:
58                Move(playerX, playerY, playerX + x, playerY + y);
59                playerX += x;
60                playerY += y;
61                break;
62            default:
63                break;
64        }
65
66        if (VictoryCheck())
67        {
68            //TODO Insert Event
69            //MessageDisplay.Add("You Win!");
70        }
71    }
72
73    /// <summary>
74    /// Moves block from place a to place b
75    ///
76    /// Don't move walls!
77    /// </summary>
78    /// <param name="startx">Source X coordinate</param>
79    /// <param name="starty">Source Y coordinate</param>
80    /// <param name="endx">Destination X coordinate</param>
81    /// <param name="endy">Destination Y coordinate</param>
82    public void Move(int startx, int starty, int endx, int endy)
83    {
84        //TODO Works, but fugly.
85        BlockType destType = playfield[endx, endy].Type;
86        BlockType sourType = playfield[startx, starty].Type;
87        bool destGoal = playfield[endx, endy].IsGoalZone;
88        bool sourGoal = playfield[startx, starty].IsGoalZone;
89
90        if (sourGoal)
91        {
92            playfield[startx, starty].Type = BlockType.GoalZone;
93        }
94        else
95        {
96            playfield[startx, starty].Type = BlockType.Empty;
97        }
98
99        if (destGoal)
100        {
101            if (sourType == BlockType.Player) { playfield[endx, endy].Type = BlockType.Player; }
102            if (sourType == BlockType.SatisfiedGoalZone) { playfield[endx, endy].Type = BlockType.SatisfiedGoalZone; }
103            if (sourType == BlockType.Movable) { playfield[endx, endy].Type = BlockType.SatisfiedGoalZone; }
104        }
105        else
106        {
107            if (sourType == BlockType.Player) { playfield[endx, endy].Type = BlockType.Player; }
108            if (sourType == BlockType.SatisfiedGoalZone) { playfield[endx, endy].Type = BlockType.Movable; }
109        }
110    }
111
112    /// <summary>
113    /// Checks if level is completed.
114    /// </summary>
115    /// <returns></returns>
116    public bool VictoryCheck()
117    {
118        foreach (var item in goals)
119        {
120            if (!(item.Type == BlockType.SatisfiedGoalZone)) return false;
121        }
122        return true;
123    }
124
125    /// <summary>
126    /// Initialaizes char->Color-dict.
127    /// </summary>
128    public void InitCharColors()
129    {
130        charColor.Add('x', BlockType.UnMovable);
131        charColor.Add('b', BlockType.Movable);
132        charColor.Add(' ', BlockType.Empty);
133        charColor.Add('m', BlockType.GoalZone);
134        charColor.Add('p', BlockType.Player);
135        charColor.Add('s', BlockType.SatisfiedGoalZone);
136    }
137
138    public void CreateField(List<string> level)
139    {
140        int playfieldWidth = level[0].Length;
141        int playfieldHeight = level.Count;
142
143        playfield = new Block[playfieldWidth, playfieldHeight];
144        game.Level.Size = new Vector(playfieldWidth * (Blockpusher.BlockSize + 1), playfieldHeight * (Blockpusher.BlockSize + 1));
145        game.Level.CreateBorders();
146
147        for (int i = 0; i < playfield.GetLength(0); i++)
148        {
149            for (int j = 0; j < playfield.GetLength(1); j++)
150            {
151                Block block = new Block(Blockpusher.BlockSize, Blockpusher.BlockSize);
152                block.X = i * (Blockpusher.BlockSize + 1) + game.Level.Left + (Blockpusher.BlockSize / 2);
153                block.Y = -(j * (Blockpusher.BlockSize + 1) + game.Level.Bottom + (Blockpusher.BlockSize / 2));
154
155                block.Type = charColor[level[j][i]];
156                if (block.Type == BlockType.Player)
157                {
158                    playerX = i;
159                    playerY = j;
160                }
161                if (block.IsGoalZone)
162                {
163                    goals.Add(block);
164                }
165                game.Add(block);
166                playfield[i, j] = block;
167            }
168        }
169        UpdatePlayer(BlockType.Player);
170    }
171
172    /// <summary>
173    /// Checks if given coordinates are empty and can be moved to.
174    ///
175    /// If coordinates are not in playfield, UnMovable is returned.
176    /// </summary>
177    /// <param name="x">X-coordinate</param>
178    /// <param name="y">Y-coordinate</param>
179    /// <returns>State of block at given coordinates</returns>
180    public BlockType Type(int x, int y)
181    {
182        if (x < 0) return BlockType.UnMovable;
183        if (y < 0) return BlockType.UnMovable;
184        if (x >= PlayfieldWidth) return BlockType.UnMovable;
185        if (y >= PlayfieldHeight) return BlockType.UnMovable;
186
187        return playfield[x, y].Type;
188    }
189
190    /// <summary>
191    /// Changes color of players coordinates
192    /// </summary>
193    /// <param name="color">Color</param>
194    public void UpdatePlayer(BlockType type)
195    {
196        playfield[playerX, playerY].Type = type;
197    }
198}
199
Note: See TracBrowser for help on using the repository browser.