VioletaBabel

33. 타워디펜스에 사용한, 간단한 길 찍는 툴 본문

BCA/3. C# WPF
33. 타워디펜스에 사용한, 간단한 길 찍는 툴
Beabletoet 2018. 5. 3. 12:05

타워 디펜스 : http://violetababel.tistory.com/408


몬스터가 걸어갈 패스를 지정해준 툴이다. (길은 몬스터가 지나가는 순서대로만 찍을 것, 간단한 배경 넣기 가능)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!--TileSelector.xaml-->
    <Window x:Class="defenceTool.TileSelector"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:defenceTool"
        mc:Ignorable="d"
        Title="TileSelector" Height="450" Width="600">
    <Grid Height="419" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="592">
        <Grid.Resources>
            <local:TileModel x:Key="model"/>
        </Grid.Resources>
        <ListView ItemsSource="{Binding Data, Source={StaticResource model}}" SelectionChanged="ListView_SelectionChanged" Height="419" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="592">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
                        <TextBlock Text="{Binding Header}"/>
                        <Image Source="{Binding Content}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListView>
    </Grid>
</Window>
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--MainWindow.xaml-->
    <Window x:Class="defenceTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:defenceTool"
        mc:Ignorable="d"
        Title="MainWindow" Height="832" Width="1280">
    <Grid>
        <Canvas Name="canvas" HorizontalAlignment="Left" Height="801" VerticalAlignment="Top" Width="1272" MouseLeftButtonUp="canvas_MouseLeftButtonUp" MouseRightButtonUp="canvas_MouseRightButtonUp" >
            <Button x:Name="SaveButton" Content="Saaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaave" Canvas.Left="10" Canvas.Top="711" Width="630" Click="Save_Button_Click" Height="80"/>
            <Button x:Name="LoadButton" Content="Loaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad" Canvas.Left="645" Canvas.Top="711" Width="617" Height="80" Click="LoadButton_Click"/>
        </Canvas>
 
    </Grid>
</Window>
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Net.Json;
 
namespace defenceTool
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        //string[] tName = new string[2] { "grass", "route" };
        private List<Point> path = new List<Point>();
        private List<Point> nonPath = new List<Point>();
        private Image[] map = new Image[240];
        private string[] pathMap = new string[240];
        private string thisName;
        private bool isRoute = false;
        public MainWindow()
        {
            InitializeComponent();
            thisName = "../../Resource/grass.png";
            for (int i = 0; i < 240++i)
            {
                map[i] = null;
                pathMap[i] = null;
            }
            for (int i = 0; i < 20++i)
                for (int j = 0; j < 11++j)
                    //makeBaseTile(i, j, tName[0]);
                    makeBaseTile(i, j, thisName);
            TileSelector tile = new TileSelector(this);
            tile.Show();
        }
 
        public void setTile(string fileName, bool bRoad = false)
        {
            thisName = fileName;
            if (fileName.Contains("route"))
            {
                isRoute = true;
            }
            else
                isRoute = false;
 
        }
 
        private Image makeBaseTile(int x, int y, string name)
        {
            //Stream imageStreamSource = new FileStream("../../Resource/" + name + ".png", FileMode.Open, FileAccess.Read, FileShare.Read);
            Stream imageStreamSource = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];
 
            var mImage = new Image();
            mImage.Source = bitmapSource;
            mImage.Stretch = Stretch.None;
            mImage.Margin = new Thickness(20);
            canvas.Children.Add(mImage);
            
            var m = mImage.Margin;
            m.Left = x * 64 + mImage.ActualWidth * 0.5;
            m.Top = y * 64 + mImage.ActualHeight * 0.5;
            mImage.Margin = m;
            
            return mImage;
        }
 
        private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
 
            Point pos = e.GetPosition(canvas);
            pos = new Point((int)pos.X / 64, (int)pos.Y / 64);
            if (pos.X > -1 & pos.X < 20 && pos.Y > -1 && pos.Y < 12)
            {
                if (map[(int)pos.X + ((int)pos.Y * 20)] != null)
                    map[(int)pos.X + ((int)pos.Y * 20)].Source = null;
 
                map[(int)pos.X + ((int)pos.Y * 20)] = makeBaseTile((int)pos.X, (int)pos.Y, thisName);
                pathMap[(int)pos.X + ((int)pos.Y * 20)] = thisName;
                if (pathMap[(int)pos.X + ((int)pos.Y * 20)].Contains("route"))
                    path.Add(pos);
                else
                    nonPath.Add(pos);
 
            }
        }
 
        private void canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point pos = e.GetPosition(canvas);
            pos = new Point((int)pos.X / 64, (int)pos.Y / 64);
            if (pos.X > -1 & pos.X < 20 && pos.Y > -1 && pos.Y < 12)
                if (map[(int)pos.X + ((int)pos.Y * 20)] != null)
                {
                    map[(int)pos.X + ((int)pos.Y * 20)].Source = null;
                    map[(int)pos.X + ((int)pos.Y * 20)] = null;
                    pathMap[(int)pos.X + ((int)pos.Y * 20)] = null;
                    if (!path.Count.Equals(0))
                        for (int i = 0; i < path.Count; ++i)
                        {
                            if ((int)path[i].X == (int)pos.X && (int)path[i].Y == (int)pos.Y)
                            {
                                path.Remove(path[i]);
                            }
                        }
                }
        }
 
        private void Save_Button_Click(object sender, RoutedEventArgs e)
        {
            JsonObjectCollection root = new JsonObjectCollection();
            JsonArrayCollection arr = new JsonArrayCollection("Path");
            JsonArrayCollection arr2 = new JsonArrayCollection("NonPath");
            foreach (Point pos in path)
            {
                JsonObjectCollection jsonPos = new JsonObjectCollection();
                jsonPos.Add(new JsonStringValue("path", pathMap[(int)pos.X + ((int)pos.Y * 20)]));
                jsonPos.Add(new JsonNumericValue("X", pos.X));
                jsonPos.Add(new JsonNumericValue("Y", pos.Y));
                arr.Add(jsonPos);
            }
            for(int i = 0; i < 240++i)
            {
                if(pathMap[i] != null && !pathMap[i].Contains("route"))
                {
                    JsonObjectCollection jsonPos = new JsonObjectCollection();
                    jsonPos.Add(new JsonStringValue("path", pathMap[i]));
                    jsonPos.Add(new JsonNumericValue("X", i%20));
                    jsonPos.Add(new JsonNumericValue("Y", i/20));
                    arr2.Add(jsonPos);
                }
            }
            root.Add(arr);
            root.Add(arr2);
            String s = root.ToString();
            System.IO.File.WriteAllText("PathData.json", s);
        }
 
        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            String s = System.IO.File.ReadAllText("PathData.json");
            JsonTextParser textParser = new JsonTextParser();
            JsonObjectCollection root = textParser.Parse(s) as JsonObjectCollection;
            JsonArrayCollection arr = root["Path"as JsonArrayCollection;
            JsonArrayCollection arr2 = root["NonPath"as JsonArrayCollection;
            foreach (Point pos in path)
            {
                if (map[(int)pos.X + ((int)pos.Y * 20)] != null)
                {
                    map[(int)pos.X + ((int)pos.Y * 20)].Source = null;
                    map[(int)pos.X + ((int)pos.Y * 20)] = null;
                    pathMap[(int)pos.X + ((int)pos.Y * 20)] = null;
                }
            }
            path.Clear();
 
            foreach (JsonObjectCollection obj in arr)
            {
                double x = (obj["X"as JsonNumericValue).Value;
                double y = (obj["Y"as JsonNumericValue).Value;
                string n = (obj["path"as JsonStringValue).Value;
                Point pos = new Point(x, y);
                if (pos.X > -1 && pos.X < 20 && pos.Y > -1 && pos.Y < 12)
                    if (map[(int)pos.X + ((int)pos.Y * 20)] == null)
                    {
                        map[(int)pos.X + ((int)pos.Y * 20)] = makeBaseTile((int)pos.X, (int)pos.Y, n);
                        pathMap[(int)pos.X + ((int)pos.Y * 20)] = n;
                        if (n.Contains("route"))
                            path.Add(pos);
                        else
                            nonPath.Add(pos);
                    }
            }
            foreach (JsonObjectCollection obj in arr2)
            {
                double x = (obj["X"as JsonNumericValue).Value;
                double y = (obj["Y"as JsonNumericValue).Value;
                string n = (obj["path"as JsonStringValue).Value;
                Point pos = new Point(x, y);
                if (pos.X > -1 && pos.X < 20 && pos.Y > -1 && pos.Y < 12)
                    if (map[(int)pos.X + ((int)pos.Y * 20)] == null)
                    {
                        map[(int)pos.X + ((int)pos.Y * 20)] = makeBaseTile((int)pos.X, (int)pos.Y, n);
                        pathMap[(int)pos.X + ((int)pos.Y * 20)] = n;
                        if (n.Contains("route"))
                            path.Add(pos);
                        else
                            nonPath.Add(pos);
                    }
            }
        }
    }
}
 
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//TileSelector.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Drawing; //참조추가
using System.IO;
 
namespace defenceTool
{
    /// <summary>
    /// TileSelector.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class TileSelector : Window
    {
        private MainWindow mainWindow = null;
        public TileSelector(MainWindow mWindow)
        {
            InitializeComponent();
            mainWindow = mWindow;
            DirectoryInfo dirInfo = new DirectoryInfo("C:/Users/user/source/repos/defenceTool/defenceTool/Resource");
            FileInfo[] info = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
            foreach(FileInfo f in info)
            {
                Tile t = new Tile();
                t.Header = f.FullName;
                t.Content = LoadImage(f.FullName);
                Tiles.instance.mTileData.Add(t);
            }
 
            //Tile t = new Tile();
            //t.Header = "C:/Users/user/source/repos/defenceTool/defenceTool/Resource/grass.png";
            //t.Content = LoadImage(t.Header);
            //Tiles.instance.mTileData.Add(t);
            //Tile t2 = new Tile();
            //t2.Header = "C:/Users/user/source/repos/defenceTool/defenceTool/Resource/route.png";
            //t2.Content = LoadImage(t2.Header);
            //Tiles.instance.mTileData.Add(t2);
        }
        private BitmapImage LoadImage(string fileName)
        {
            return new BitmapImage(new Uri(fileName, UriKind.Absolute));
        }
 
        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView listView = sender as ListView;
            if(listView != null)
            {
                int index = listView.SelectedIndex;
                Tile t = Tiles.instance.mTileData[index];
                mainWindow.setTile(t.Header);
            }
        }
        //public BitmapImage LoadBitmap(string fileName)
        //{
        //    System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
        //    BitmapImage bImage = new BitmapImage();
        //    bImage.BeginInit();
        //    MemoryStream ms = new MemoryStream();
        //    img.Save(ms, ImageFormat.Png);
        //    ms.Seek(0, SeekOrigin.Begin);
        //    bImage.StreamSource = ms;
        //    bImage.EndInit();
        //    return bImage;
        //}
    }
}
 

cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//TileModel.cs
using System;
 
namespace defenceTool
{
    class TileModel
    {
        object _data;
        public object Data
        {
            get
            {
                if (_data == null)
                    _data = GetData();
                return _data;
            }
        }
        private object GetData()
        {
            return Tiles.instance.mTileData;
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Tiles.cs
using System;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
 
namespace defenceTool
{
    public class Tile
    {
        private string header;
        public string Header
        {
            get { return header; }
            set { header = value; }
        }
        private BitmapImage content;
        public BitmapImage Content
        {
            get { return content; }
            set { content = value; }
        }
 
    }
    public class Tiles
    {
        public ObservableCollection<Tile> mTileData = new ObservableCollection<Tile>();
        private static Tiles Instance = null;
        public static Tiles instance
        {
            get
            {
                if (Instance == null)
                    Instance = new Tiles();
                return Instance;
            }
        }
    }
}
cs


'BCA > 3. C# WPF' 카테고리의 다른 글

30. 연출용 툴  (0) 2018.04.27
20일 : system.net.json  (0) 2018.03.15
19일 : 캔버스에 도형 그리기  (0) 2018.03.13
18일 : mp3 플레이어  (0) 2018.03.12
17일 : C# 윈도우 폼 입문, 계산기  (0) 2018.03.09
Comments