Ignore:
Timestamp:
2011-12-09 18:33:32 (11 years ago)
Author:
hniemi
Message:

Toimiva prototyyppi.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/Blockpusher.cs

    r2627 r2632  
    1616    private int playerX = 4; 
    1717    private int playerY = 4; 
    18      
    19  
    20     private Dictionary<char, Color> charColor = new Dictionary<char, Color>(); 
     18 
     19 
     20    private Dictionary<char, BlockType> charColor = new Dictionary<char, BlockType>(); 
    2121    private readonly Color playerColor = Color.Blue; 
    2222    private readonly Color satisfyColor = Color.Green; 
    2323    private readonly Color unsatisfyColor = Color.Red; 
    2424 
     25    private List<Block> goals = new List<Block>(); 
     26 
    2527    //[x,y] 
    26     private GameObject[,] playfield; 
     28    private Block[,] playfield; 
    2729 
    2830    private string[] testLevel = {"xxxxxxxxx", 
    2931                                  "x  x  xmx", 
    3032                                  "xb s  xbx", 
    31                                   "x  x    x", 
    32                                   "xm xxxxxx", 
     33                                  "xm x    x", 
     34                                  "x  xxxxxx", 
     35                                  "x      xx", 
    3336                                  "xxxxxxxxx"}; 
    3437 
     
    4447    public void InitCharColors()  
    4548    { 
    46         charColor.Add('x', Color.Black); 
    47         charColor.Add('b', Color.Beige); 
    48         charColor.Add(' ', Color.Transparent); 
    49         charColor.Add('m', unsatisfyColor); 
    50         charColor.Add('s', playerColor); 
     49        charColor.Add('x', BlockType.UnMovable); 
     50        charColor.Add('b', BlockType.Movable); 
     51        charColor.Add(' ', BlockType.Empty); 
     52        charColor.Add('m', BlockType.GoalZone); 
     53        charColor.Add('s', BlockType.Player); 
    5154    } 
    5255 
     
    5861    public void MovePlayer(int x, int y)  
    5962    { 
    60         if (IsPassable(playerX + x, playerY + y))  
    61         { 
    62             UpdatePlayer(Color.Transparent); 
    63             playerX += x; 
    64             playerY += y; 
    65             UpdatePlayer(playerColor); 
     63        switch (Type(playerX + x, playerY + y)) 
     64        { 
     65            case BlockType.SatisfiedGoalZone: 
     66            case BlockType.Movable: 
     67                int nextX = playerX + x; 
     68                if (x != 0) { nextX += (x / Math.Abs(x)); } 
     69                int nextY = playerY + y; 
     70                if (y != 0) { nextY += (y / Math.Abs(y)); } 
     71                BlockType t = Type(nextX, nextY); 
     72                if (t == BlockType.Empty || t == BlockType.GoalZone || t == BlockType.SatisfiedGoalZone) 
     73                { 
     74                    Move(playerX + x, playerY + y, nextX, nextY); 
     75                    Move(playerX, playerY, playerX + x, playerY + y); 
     76                    playerX += x; 
     77                    playerY += y; 
     78                } 
     79 
     80                break; 
     81            case BlockType.Empty: 
     82                Move(playerX, playerY, playerX+x, playerY+y); 
     83                playerX += x; 
     84                playerY += y; 
     85                break; 
     86            default: 
     87                break; 
     88        } 
     89 
     90        if (VictoryCheck())  
     91        { 
     92            MessageDisplay.Add("You Win!"); 
     93        } 
     94    } 
     95 
     96    /// <summary> 
     97    /// Moves block from place a to place b 
     98    ///  
     99    /// Don't move walls! 
     100    /// </summary> 
     101    /// <param name="startx">Source X coordinate</param> 
     102    /// <param name="starty">Source Y coordinate</param> 
     103    /// <param name="endx">Destination X coordinate</param> 
     104    /// <param name="endy">Destination Y coordinate</param> 
     105    public void Move(int startx, int starty, int endx, int endy)  
     106    { 
     107        //TODO Works, but fugly. 
     108        BlockType destType = playfield[endx, endy].Type; 
     109        BlockType sourType = playfield[startx, starty].Type; 
     110        bool destGoal = playfield[endx, endy].IsGoalZone; 
     111        bool sourGoal = playfield[startx, starty].IsGoalZone; 
     112 
     113        if (sourGoal) 
     114        { 
     115            playfield[startx, starty].Type = BlockType.GoalZone; 
     116        } 
     117        else  
     118        { 
     119            playfield[startx, starty].Type = BlockType.Empty; 
     120        } 
     121 
     122        if (destGoal) 
     123        { 
     124            if (sourType == BlockType.Player) { playfield[endx, endy].Type = BlockType.Player; } 
     125            if (sourType == BlockType.SatisfiedGoalZone) { playfield[endx, endy].Type = BlockType.SatisfiedGoalZone; } 
     126            if (sourType == BlockType.Movable) { playfield[endx, endy].Type = BlockType.SatisfiedGoalZone; } 
     127        } 
     128        else  
     129        { 
     130            if (sourType == BlockType.Player) { playfield[endx, endy].Type = BlockType.Player; } 
     131            if (sourType == BlockType.SatisfiedGoalZone) { playfield[endx, endy].Type = BlockType.Movable; } 
    66132        } 
    67133    } 
     
    69135    /// <summary> 
    70136    /// Checks if given coordinates are empty and can be moved to. 
     137    ///  
     138    /// If coordinates are not in playfield, UnMovable is returned. 
    71139    /// </summary> 
    72140    /// <param name="x">X-coordinate</param> 
    73141    /// <param name="y">Y-coordinate</param> 
    74     /// <returns>True, if slot in given coordinates is passable</returns> 
    75     public bool IsPassable(int x, int y) 
    76     { 
    77         if (x < 0) return false; 
    78         if (y < 0) return false; 
    79         if (x >= playfieldWidth) return false; 
    80         if (y >= playfieldHeight) return false; 
    81         return playfield[x, y].Color == Color.Transparent; 
     142    /// <returns>State of block at given coordinates</returns> 
     143    public BlockType Type(int x, int y) 
     144    { 
     145        if (x < 0) return BlockType.UnMovable; 
     146        if (y < 0) return BlockType.UnMovable; 
     147        if (x >= playfieldWidth) return BlockType.UnMovable; 
     148        if (y >= playfieldHeight) return BlockType.UnMovable; 
     149 
     150        return playfield[x, y].Type; 
    82151    } 
    83152 
     
    86155    /// </summary> 
    87156    /// <param name="color">Color</param> 
    88     public void UpdatePlayer(Color color)  
    89     { 
    90         playfield[playerX, playerY].Color = color; 
     157    public void UpdatePlayer(BlockType type)  
     158    { 
     159        playfield[playerX, playerY].Type = type; 
    91160    } 
    92161 
     
    100169        playfieldWidth = level[0].Length; 
    101170        playfieldHeight = level.Length; 
    102         playfield = new GameObject[playfieldWidth,playfieldHeight]; 
     171        playfield = new Block[playfieldWidth,playfieldHeight]; 
    103172        Level.Size = new Vector(playfieldWidth * (blocksize + 1), playfieldHeight * (blocksize + 1)); 
    104173        Level.CreateBorders(); 
    105174 
    106         for (int i = 0; i < playfield.Length; i++) 
    107         { 
    108             GameObject block = new GameObject(blocksize, blocksize); 
    109             block.X = (i % playfieldHeight) * (blocksize + 1) + Level.Left + (blocksize/2); 
    110             block.Y = (i / playfieldWidth) * (blocksize + 1) + Level.Bottom + (blocksize / 2); 
    111  
    112             block.Color = charColor[level[i / playfieldWidth][i % playfieldWidth]]; 
    113             Add(block); 
    114             playfield[i % playfieldWidth, i / playfieldWidth] = block; 
    115         } 
    116         UpdatePlayer(playerColor); 
     175        for (int i = 0; i < playfield.GetLength(0); i++) 
     176        { 
     177            for (int j = 0; j < playfield.GetLength(1); j++) 
     178            { 
     179                Block block = new Block(blocksize, blocksize); 
     180                block.X = i * (blocksize + 1) + Level.Left + (blocksize / 2); 
     181                block.Y = -(j * (blocksize + 1) + Level.Bottom + (blocksize / 2)); 
     182 
     183                block.Type = charColor[level[j][i]]; 
     184                if (block.Type == BlockType.Player)  
     185                { 
     186                    playerX = i; 
     187                    playerY = j; 
     188                } 
     189                if (block.IsGoalZone)  
     190                { 
     191                    goals.Add(block); 
     192                } 
     193                Add(block); 
     194                playfield[i,j] = block; 
     195            } 
     196        } 
     197        UpdatePlayer(BlockType.Player); 
     198    } 
     199 
     200    /// <summary> 
     201    /// Checks if level is completed. 
     202    /// </summary> 
     203    /// <returns></returns> 
     204    public bool VictoryCheck()  
     205    { 
     206        foreach (var item in goals) 
     207        { 
     208            if (!(item.Type == BlockType.SatisfiedGoalZone)) return false; 
     209        } 
     210        return true; 
    117211    } 
    118212 
     
    131225    public void SetControls()  
    132226    { 
    133         Keyboard.Listen(Key.Up, ButtonState.Pressed, MovePlayer, null, 0, 1); 
    134         Keyboard.Listen(Key.Down, ButtonState.Pressed, MovePlayer, null, 0, -1); 
     227        Keyboard.Listen(Key.Up, ButtonState.Pressed, MovePlayer, null, 0, -1); 
     228        Keyboard.Listen(Key.Down, ButtonState.Pressed, MovePlayer, null, 0, 1); 
    135229        Keyboard.Listen(Key.Left, ButtonState.Pressed, MovePlayer, null, -1, 0); 
    136230        Keyboard.Listen(Key.Right, ButtonState.Pressed, MovePlayer, null, 1, 0); 
Note: See TracChangeset for help on using the changeset viewer.