Skip to content

Commit d2d909e

Browse files
committed
Add levels
1 parent c291c23 commit d2d909e

17 files changed

Lines changed: 321 additions & 33 deletions

File tree

TurnBase.KaNoBu.Godot/Presentation/GameField.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,9 @@ public override void _Ready()
271271
{
272272
this.FillMembers();
273273
}
274+
275+
public Vector2 WorldToMap(Vector2 position)
276+
{
277+
return this.water.WorldToMap(position);
278+
}
274279
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Godot;
2+
3+
[SceneReference("Level1.tscn")]
4+
public partial class Level1
5+
{
6+
public override void _Ready()
7+
{
8+
base._Ready();
9+
this.FillMembers();
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[gd_scene load_steps=4 format=2]
2+
3+
[ext_resource path="res://Presentation/Levels/LevelBase.tscn" type="PackedScene" id=1]
4+
[ext_resource path="res://Presentation/Levels/Level1.cs" type="Script" id=2]
5+
[ext_resource path="res://Presentation/Unit.tscn" type="PackedScene" id=3]
6+
7+
[node name="Level1" instance=ExtResource( 1 )]
8+
script = ExtResource( 2 )
9+
10+
[node name="Beach" parent="." index="2"]
11+
tile_data = PoolIntArray( 65533, 2, 0, 65534, 2, 1, 65535, 2, 1, 0, 2, 1, 1, 2, 1, 2, 2, 1, 3, 2, 1, 4, 2, 1, 5, 2, 1, 6, 2, 1, 7, 2, 1, 8, 2, 1, 9, 2, 1, 10, 2, 2, 131069, 2, 65536, 131070, 2, 262147, 131071, 2, 3, 65536, 2, 131073, 65537, 2, 131073, 65538, 2, 131073, 65539, 2, 131073, 65540, 2, 131073, 65541, 2, 131073, 65542, 2, 131073, 65543, 2, 131073, 65544, 2, 4, 65545, 2, 262147, 65546, 2, 65538, 196605, 2, 65536, 196606, 2, 262147, 196607, 2, 65538, 131080, 2, 65536, 131081, 2, 262147, 131082, 2, 65538, 262141, 2, 65536, 262142, 2, 262147, 262143, 2, 65538, 196616, 2, 65536, 196617, 2, 262147, 196618, 2, 65538, 327677, 2, 65536, 327678, 2, 262147, 327679, 2, 65539, 262144, 2, 1, 262145, 2, 1, 262146, 2, 1, 262147, 2, 1, 262148, 2, 1, 262149, 2, 1, 262150, 2, 1, 262151, 2, 1, 262152, 2, 65540, 262153, 2, 262147, 262154, 2, 65538, 393213, 2, 65536, 393214, 2, 262147, 393215, 2, 3, 327680, 2, 131073, 327681, 2, 131073, 327682, 2, 131073, 327683, 2, 131073, 327684, 2, 131073, 327685, 2, 131073, 327686, 2, 131073, 327687, 2, 131073, 327688, 2, 4, 327689, 2, 262147, 327690, 2, 65538, 458749, 2, 65536, 458750, 2, 262147, 458751, 2, 65538, 393224, 2, 65536, 393225, 2, 262147, 393226, 2, 65538, 524285, 2, 65536, 524286, 2, 262147, 524287, 2, 65538, 458760, 2, 65536, 458761, 2, 262147, 458762, 2, 65538, 589821, 2, 65536, 589822, 2, 262147, 589823, 2, 65538, 524296, 2, 65536, 524297, 2, 262147, 524298, 2, 65538, 655357, 2, 65536, 655358, 2, 262147, 655359, 2, 65539, 589824, 2, 1, 589825, 2, 1, 589826, 2, 1, 589827, 2, 1, 589828, 2, 1, 589829, 2, 1, 589830, 2, 1, 589831, 2, 1, 589832, 2, 65540, 589833, 2, 262147, 589834, 2, 65538, 720893, 2, 65536, 720894, 2, 262147, 720895, 2, 262147, 655360, 2, 262147, 655361, 2, 262147, 655362, 2, 262147, 655363, 2, 262147, 655364, 2, 262147, 655365, 2, 262147, 655366, 2, 262147, 655367, 2, 262147, 655368, 2, 262147, 655369, 2, 262147, 655370, 2, 65538, 786429, 2, 65536, 786430, 2, 262147, 786431, 2, 262147, 720896, 2, 262147, 720897, 2, 262147, 720898, 2, 262147, 720899, 2, 262147, 720900, 2, 262147, 720901, 2, 262147, 720902, 2, 262147, 720903, 2, 262147, 720904, 2, 262147, 720905, 2, 262147, 720906, 2, 65538, 851965, 2, 65536, 851966, 2, 262147, 851967, 2, 262147, 786432, 2, 262147, 786433, 2, 262147, 786434, 2, 262147, 786435, 2, 262147, 786436, 2, 262147, 786437, 2, 262147, 786438, 2, 262147, 786439, 2, 262147, 786440, 2, 262147, 786441, 2, 262147, 786442, 2, 65538, 917501, 2, 131072, 917502, 2, 131073, 917503, 2, 131073, 851968, 2, 131073, 851969, 2, 131073, 851970, 2, 131073, 851971, 2, 131073, 851972, 2, 131073, 851973, 2, 131073, 851974, 2, 131073, 851975, 2, 131073, 851976, 2, 131073, 851977, 2, 131073, 851978, 2, 131074 )
12+
13+
[node name="Unit1" parent="Field" index="0" instance=ExtResource( 3 )]
14+
position = Vector2( 159, 185 )
15+
UnitType = 2
16+
17+
[node name="Unit2" parent="Field" index="1" instance=ExtResource( 3 )]
18+
position = Vector2( 325, 185 )
19+
UnitType = 4
20+
PlayerNumber = 1
21+
22+
[node name="Unit3" parent="Field" index="2" instance=ExtResource( 3 )]
23+
position = Vector2( 289, 474 )
24+
PlayerNumber = 1
25+
26+
[node name="Unit4" parent="Field" index="3" instance=ExtResource( 3 )]
27+
position = Vector2( 159, 466 )
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Godot;
2+
3+
[SceneReference("Level2.tscn")]
4+
public partial class Level2
5+
{
6+
public override void _Ready()
7+
{
8+
base._Ready();
9+
this.FillMembers();
10+
}
11+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[gd_scene load_steps=4 format=2]
2+
3+
[ext_resource path="res://Presentation/Levels/LevelBase.tscn" type="PackedScene" id=1]
4+
[ext_resource path="res://Presentation/Levels/Level2.cs" type="Script" id=2]
5+
[ext_resource path="res://Presentation/Unit.tscn" type="PackedScene" id=3]
6+
7+
[node name="Level2" instance=ExtResource( 1 )]
8+
script = ExtResource( 2 )
9+
10+
[node name="Unit" parent="Field" index="0" instance=ExtResource( 3 )]
11+
position = Vector2( 87.9999, 367 )
12+
13+
[node name="Unit2" parent="Field" index="1" instance=ExtResource( 3 )]
14+
position = Vector2( 88, 195 )
15+
16+
[node name="Unit1" parent="Field" index="2" instance=ExtResource( 3 )]
17+
position = Vector2( 89, 278 )
18+
UnitType = 2
19+
20+
[node name="Unit3" parent="Field" index="3" instance=ExtResource( 3 )]
21+
position = Vector2( 232, 184 )
22+
UnitType = 4
23+
PlayerNumber = 1
24+
25+
[node name="Unit4" parent="Field" index="4" instance=ExtResource( 3 )]
26+
position = Vector2( 227, 278 )
27+
PlayerNumber = 1
28+
29+
[node name="Unit5" parent="Field" index="5" instance=ExtResource( 3 )]
30+
position = Vector2( 234, 369 )
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Godot;
2+
using TurnBase;
3+
using TurnBase.KaNoBu;
4+
5+
[SceneReference("LevelBase.tscn")]
6+
public partial class LevelBase
7+
{
8+
public override void _Ready()
9+
{
10+
base._Ready();
11+
this.FillMembers();
12+
this.AddToGroup(Groups.Level);
13+
}
14+
15+
public virtual IGame<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel> Start()
16+
{
17+
var rules = new KaNoBuLevelRules(8, true);
18+
rules.SetInitialField(this.generateField());
19+
var game = new Game<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>(rules, "level");
20+
game.AddPlayer(this);
21+
game.AddPlayer(new PlayerLoose<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>());
22+
return game;
23+
}
24+
25+
public Field2D generateField()
26+
{
27+
var field2D = Field2D.Create(8, 8);
28+
var allUnits = this.field.GetChildren();
29+
foreach (Unit unit in allUnits)
30+
{
31+
var fig = new KaNoBuFigure(unit.PlayerNumber, unit.UnitType, true, 0);
32+
var pos = this.WorldToMap(unit.Position);
33+
field2D.trySet(new Point((int)pos.x, (int)pos.y), fig);
34+
}
35+
36+
return field2D;
37+
}
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[gd_scene load_steps=3 format=2]
2+
3+
[ext_resource path="res://Presentation/GameField.tscn" type="PackedScene" id=1]
4+
[ext_resource path="res://Presentation/Levels/LevelBase.cs" type="Script" id=2]
5+
6+
[node name="LevelBase" instance=ExtResource( 1 )]
7+
script = ExtResource( 2 )
8+
UnitScene = null

TurnBase.KaNoBu.Godot/Presentation/Main.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public override void _Ready()
1313

1414
private async void OnStartGameAsync()
1515
{
16-
var game = this.uI.BuildGame(this.gameField);
16+
var game = this.uI.BuildGame();
1717
if (game == null)
1818
{
1919
return;
@@ -23,6 +23,8 @@ private async void OnStartGameAsync()
2323

2424
await game.Play();
2525

26+
this.GetTree().CallGroup(Groups.Level, "queue_free");
27+
2628
this.uI.Visible = true;
2729
}
2830
}

TurnBase.KaNoBu.Godot/Presentation/UI.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using System;
2-
using System.CodeDom;
32
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Threading.Tasks;
64
using Godot;
5+
using Godot.Collections;
76
using TurnBase;
87
using TurnBase.KaNoBu;
98

10-
119
[SceneReference("UI.tscn")]
1210
public partial class UI
1311
{
12+
[Export]
13+
public List<PackedScene> Levels;
14+
1415
public override void _Ready()
1516
{
1617
this.FillMembers();
@@ -19,13 +20,22 @@ public override void _Ready()
1920
this.startButton.Connect("pressed", this, nameof(StartButonClicked));
2021

2122
this.serverIpInfo.Text = string.Join("\n", IP.GetLocalAddresses().Cast<string>().Where(a => !a.Contains(":")));
23+
24+
if (Levels != null)
25+
{
26+
for (int i = 0; i < Levels.Count; i++)
27+
{
28+
this.levelType.AddItem($"{i}");
29+
}
30+
}
2231
}
2332

2433
private void GameTypeChanged(int selectedId)
2534
{
2635
this.serverGameType.Visible = false;
2736
this.clientGameType.Visible = false;
2837
this.replayGameType.Visible = false;
38+
this.levelsGameType.Visible = false;
2939
switch (this.gameType.GetSelectedId())
3040
{
3141
case 0:
@@ -40,6 +50,10 @@ private void GameTypeChanged(int selectedId)
4050
// Replay
4151
this.replayGameType.Visible = true;
4252
break;
53+
case 3:
54+
// Levels
55+
this.levelsGameType.Visible = true;
56+
break;
4357
default:
4458
throw new Exception("Unknown game type");
4559
}
@@ -55,7 +69,7 @@ private void StartButonClicked()
5569

5670
private List<ICommunicationModel> lastReplay;
5771

58-
public IGame BuildGame(GameField field)
72+
public IGame BuildGame()
5973
{
6074
var humanFound = false;
6175
IGame<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel> kanobu;
@@ -64,7 +78,8 @@ public IGame BuildGame(GameField field)
6478
case 0:
6579
{
6680
// server
67-
var rules = new KaNoBuRules(8, true);
81+
var rules = new KaNoBuRules(8);
82+
rules.AllFiguresVisible = true;
6883
kanobu = new Game<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>(rules, "test");
6984

7085
var playerTypes = new[]{
@@ -88,7 +103,7 @@ public IGame BuildGame(GameField field)
88103
humanFound = true;
89104
}
90105

91-
kanobu.AddPlayer(BuildPlayer(playertype.GetSelectedId(), field, kanobu.GameId));
106+
kanobu.AddPlayer(BuildPlayer(playertype.GetSelectedId(), kanobu.GameId));
92107
}
93108

94109
var memoryReplay = new MemoryStorageEventListener<KaNoBuMoveNotificationModel>();
@@ -101,7 +116,7 @@ public IGame BuildGame(GameField field)
101116
{
102117
// Client
103118
kanobu = new RemoteGame<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>(this.client, $"http://{this.serverIpInput.Text}:8080", "test");
104-
kanobu.AddPlayer(BuildPlayer(this.clientPlayer.GetSelectedId(), field, kanobu.GameId));
119+
kanobu.AddPlayer(BuildPlayer(this.clientPlayer.GetSelectedId(), kanobu.GameId));
105120

106121
if (clientPlayer.GetSelectedId() == 1)
107122
{
@@ -127,6 +142,13 @@ public IGame BuildGame(GameField field)
127142
}
128143
break;
129144
}
145+
case 3:
146+
humanFound = true;
147+
var levelName = this.levelType.GetItemText(this.levelType.GetSelectedId());
148+
var level = this.Levels[int.Parse(levelName)].Instance<LevelBase>();
149+
this.GetParent().AddChild(level);
150+
kanobu = level.Start();
151+
break;
130152
default:
131153
throw new InvalidOperationException("Unknown game type");
132154
}
@@ -135,13 +157,14 @@ public IGame BuildGame(GameField field)
135157

136158
if (!humanFound)
137159
{
160+
var field = this.GetParent().GetNode<GameField>("GameField");
138161
kanobu.AddGameLogListener(field);
139162
}
140163

141164
return kanobu;
142165
}
143166

144-
public IPlayer<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel> BuildPlayer(int playerType, GameField field, string gameId)
167+
public IPlayer<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel> BuildPlayer(int playerType, string gameId)
145168
{
146169
switch (playerType)
147170
{
@@ -150,16 +173,17 @@ public IPlayer<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBu
150173
return new PlayerLoose<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>();
151174
case 1:
152175
// Human
176+
var field = this.GetParent().GetNode<GameField>("GameField");
153177
return new TimeoutPlayer<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>(
154-
field,
178+
field,
155179
async (delay) => await this.ToSignal(this.GetTree().CreateTimer(delay / 1000f), "timeout"),
156180
1000,
157181
60000);
158182
case 2:
159183
// Computer Easy
160184
var playerEasy = new KaNoBuPlayerEasy();
161185
return new DelayedPlayer<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>(
162-
playerEasy,
186+
playerEasy,
163187
async (delay) => await this.ToSignal(this.GetTree().CreateTimer(delay / 1000f), "timeout"),
164188
1,
165189
300);
@@ -168,7 +192,7 @@ public IPlayer<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBu
168192
this.server.StartServer();
169193
var player = new ServerPlayer<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>(server, gameId);
170194
return new TimeoutPlayer<KaNoBuInitModel, KaNoBuInitResponseModel, KaNoBuMoveModel, KaNoBuMoveResponseModel, KaNoBuMoveNotificationModel>(
171-
player,
195+
player,
172196
async (delay) => await this.ToSignal(this.GetTree().CreateTimer(delay / 1000f), "timeout"),
173197
600000,
174198
60000);

TurnBase.KaNoBu.Godot/Presentation/UI.tscn

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
[gd_scene load_steps=4 format=2]
1+
[gd_scene load_steps=6 format=2]
22

33
[ext_resource path="res://Presentation/UI.cs" type="Script" id=1]
44
[ext_resource path="res://Presentation/Server.tscn" type="PackedScene" id=2]
55
[ext_resource path="res://Presentation/Client.tscn" type="PackedScene" id=3]
6+
[ext_resource path="res://Presentation/Levels/Level1.tscn" type="PackedScene" id=4]
7+
[ext_resource path="res://Presentation/Levels/Level2.tscn" type="PackedScene" id=5]
68

79
[node name="UI" type="CanvasLayer"]
810
script = ExtResource( 1 )
11+
Levels = [ ExtResource( 4 ), ExtResource( 5 ) ]
912

1013
[node name="UIContainer" type="VBoxContainer" parent="."]
1114
anchor_left = 0.5
@@ -19,20 +22,21 @@ margin_bottom = 5.0
1922
rect_scale = Vector2( 2, 2 )
2023

2124
[node name="ServerIpInfo" type="Label" parent="UIContainer"]
22-
margin_right = 150.0
25+
margin_right = 125.0
2326
margin_bottom = 14.0
2427
text = "127.0.0.1"
2528
autowrap = true
2629

2730
[node name="GameType" type="OptionButton" parent="UIContainer"]
2831
margin_top = 18.0
29-
margin_right = 150.0
32+
margin_right = 125.0
3033
margin_bottom = 38.0
31-
text = "Server"
32-
items = [ "Server", null, false, 0, null, "Client", null, false, 1, null, "Replay", null, true, 2, null ]
33-
selected = 0
34+
text = "Levels"
35+
items = [ "Server", null, false, 0, null, "Client", null, false, 1, null, "Replay", null, true, 2, null, "Levels", null, false, 3, null ]
36+
selected = 3
3437

3538
[node name="ServerGameType" type="VBoxContainer" parent="UIContainer"]
39+
visible = false
3640
margin_top = 42.0
3741
margin_right = 150.0
3842
margin_bottom = 134.0
@@ -94,10 +98,19 @@ margin_top = 162.0
9498
margin_right = 150.0
9599
margin_bottom = 162.0
96100

101+
[node name="LevelsGameType" type="VBoxContainer" parent="UIContainer"]
102+
margin_top = 42.0
103+
margin_right = 125.0
104+
margin_bottom = 62.0
105+
106+
[node name="LevelType" type="OptionButton" parent="UIContainer/LevelsGameType"]
107+
margin_right = 125.0
108+
margin_bottom = 20.0
109+
97110
[node name="StartButton" type="Button" parent="UIContainer"]
98-
margin_top = 138.0
99-
margin_right = 150.0
100-
margin_bottom = 158.0
111+
margin_top = 66.0
112+
margin_right = 125.0
113+
margin_bottom = 86.0
101114
text = "Start"
102115

103116
[node name="Server" parent="." instance=ExtResource( 2 )]

0 commit comments

Comments
 (0)