Ignore:
Timestamp:
2011-12-27 04:21:28 (11 years ago)
Author:
hniemi
Message:

Jonkinlainen toimiva protytyyppi kenttävalinnasta.

File:
1 edited

Legend:

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

    r2634 r2635  
    1414    private Block[,] playfield; 
    1515    private Blockpusher game; 
    16     private Dictionary<char, BlockType> charColor = new Dictionary<char, BlockType>(); 
     16    private List<Tuple<char, BlockType>> charcolors = new List<Tuple<char, BlockType>>(); 
    1717    private List<Block> goals = new List<Block>(); 
    1818 
     
    3333 
    3434    /// <summary> 
     35    /// Name of the level 
     36    /// </summary> 
     37    public string Name { get; set; } 
     38 
     39    /// <summary> 
    3540    /// Constructor from game 
    3641    /// </summary> 
     
    4045        this.game = game; 
    4146        InitCharColors(); 
     47    } 
     48 
     49    /// <summary> 
     50    /// Adds blocks to game 
     51    /// </summary> 
     52    public void AddToGame() 
     53    { 
     54        game.Level.Size = new Vector(playfield.GetLength(0) * (Blockpusher.BlockSize + 1), playfield.GetLength(1) * (Blockpusher.BlockSize + 1)); 
     55        game.Level.CreateBorders(); 
     56        for (int i = 0; i < playfield.GetLength(0); i++) 
     57        { 
     58            for (int j = 0; j < playfield.GetLength(1); j++) 
     59            { 
     60 
     61                playfield[i, j].X = i * (Blockpusher.BlockSize + 1) + game.Level.Left + (Blockpusher.BlockSize / 2); 
     62                playfield[i, j].Y = -(j * (Blockpusher.BlockSize + 1) + game.Level.Bottom + (Blockpusher.BlockSize / 2)); 
     63                game.Add(playfield[i,j]); 
     64            } 
     65        } 
    4266    } 
    4367 
     
    142166    public void InitCharColors() 
    143167    { 
    144         charColor.Add('x', BlockType.UnMovable); 
    145         charColor.Add('b', BlockType.Movable); 
    146         charColor.Add(' ', BlockType.Empty); 
    147         charColor.Add('m', BlockType.GoalZone); 
    148         charColor.Add('p', BlockType.Player); 
    149         charColor.Add('s', BlockType.SatisfiedGoalZone); 
     168        charcolors.Add(new Tuple<char, BlockType>('x', BlockType.UnMovable)); 
     169        charcolors.Add(new Tuple<char, BlockType>('b', BlockType.Movable)); 
     170        charcolors.Add(new Tuple<char, BlockType>(' ', BlockType.Empty)); 
     171        charcolors.Add(new Tuple<char, BlockType>('m', BlockType.GoalZone)); 
     172        charcolors.Add(new Tuple<char, BlockType>('p', BlockType.Player)); 
     173        charcolors.Add(new Tuple<char, BlockType>('s', BlockType.SatisfiedGoalZone)); 
     174    } 
     175 
     176    /// <summary> 
     177    /// Finds char matching type. 
     178    /// If not found, return ' ' 
     179    /// </summary> 
     180    /// <param name="type">Type you want to find</param> 
     181    /// <returns>Matching char</returns> 
     182    public char FindChar(BlockType type)  
     183    { 
     184        foreach (var item in charcolors) 
     185        { 
     186            if (item.Item2 == type) return item.Item1; 
     187        } 
     188        return ' '; 
     189    } 
     190 
     191    /// <summary> 
     192    /// Finds Blocktype matching char. 
     193    /// If not found, BlockType.Empty is returned. 
     194    /// </summary> 
     195    /// <param name="c">Char you want to find</param> 
     196    /// <returns>Matching type</returns> 
     197    public BlockType FindBlockType(char c) 
     198    { 
     199        foreach (var item in charcolors) 
     200        { 
     201            if (item.Item1 == c) return item.Item2; 
     202        } 
     203        return BlockType.Empty; 
    150204    } 
    151205 
     
    160214 
    161215        playfield = new Block[playfieldWidth, playfieldHeight]; 
    162         game.Level.Size = new Vector(playfieldWidth * (Blockpusher.BlockSize + 1), playfieldHeight * (Blockpusher.BlockSize + 1)); 
    163         game.Level.CreateBorders(); 
    164216 
    165217        for (int i = 0; i < playfield.GetLength(0); i++) 
     
    171223                block.Y = -(j * (Blockpusher.BlockSize + 1) + game.Level.Bottom + (Blockpusher.BlockSize / 2)); 
    172224 
    173                 block.Type = charColor[level[j][i]]; 
     225                block.Type = FindBlockType(level[j][i]); 
    174226                if (block.Type == BlockType.Player) 
    175227                { 
     
    181233                    goals.Add(block); 
    182234                } 
    183                 game.Add(block); 
    184235                playfield[i, j] = block; 
    185236            } 
     
    207258 
    208259    /// <summary> 
     260    /// Returns the current state of level. 
     261    /// </summary> 
     262    /// <returns></returns> 
     263    public List<string> CurrentState()  
     264    { 
     265        List<string> list = new List<string>(); 
     266 
     267        for (int i = 0; i < playfield.GetLength(1); i++) 
     268        { 
     269            StringBuilder line = new StringBuilder(); 
     270            for (int j = 0; j < playfield.GetLength(0); j++) 
     271            { 
     272                 
     273                line.Append(FindChar(playfield[j,i].Type)); 
     274            } 
     275            list.Add(line.ToString()); 
     276        } 
     277 
     278        return list; 
     279    } 
     280 
     281    /// <summary> 
    209282    /// Changes color of players coordinates 
    210283    /// </summary> 
Note: See TracChangeset for help on using the changeset viewer.