Skip to content

Commit

Permalink
Added animation for placing a firewall or boost card.
Browse files Browse the repository at this point in the history
  • Loading branch information
michi84o committed Oct 27, 2018
1 parent aaf1cdc commit 1c693a0
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 2 deletions.
14 changes: 14 additions & 0 deletions AccessBattleWpf/AccessBattleWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,15 @@
<Compile Include="View\ErrorNotification.xaml.cs">
<DependentUpon>ErrorNotification.xaml</DependentUpon>
</Compile>
<Compile Include="View\FirewallField.xaml.cs">
<DependentUpon>FirewallField.xaml</DependentUpon>
</Compile>
<Compile Include="View\GameOverMenu.xaml.cs">
<DependentUpon>GameOverMenu.xaml</DependentUpon>
</Compile>
<Compile Include="View\BoostField.xaml.cs">
<DependentUpon>BoostField.xaml</DependentUpon>
</Compile>
<Compile Include="View\NetworkGameMenu.xaml.cs">
<DependentUpon>NetworkGameMenu.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -127,10 +133,18 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\FirewallField.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\GameOverMenu.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\BoostField.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
1 change: 1 addition & 0 deletions AccessBattleWpf/View/BoardFieldView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace AccessBattle.Wpf.View
{
Expand Down
38 changes: 38 additions & 0 deletions AccessBattleWpf/View/BoostField.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<UserControl x:Class="AccessBattle.Wpf.View.BoostField"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AccessBattle.Wpf.View"
Width="48" Height="48" FontFamily="../ExternalResources/#Silkscreen">
<Border CornerRadius="2" BorderBrush="Yellow" BorderThickness="2" Background="White">
<Grid Margin="1">
<Grid.RowDefinitions>
<RowDefinition Height=".8*"/>
<RowDefinition Height=".1*"/>
<RowDefinition Height=".3*"/>
</Grid.RowDefinitions>
<Viewbox Margin="2" Grid.Row="0" Grid.RowSpan="2" >
<Path
Margin="1"
Fill="Black"
Stroke="Black" StrokeThickness="1"
Data="{StaticResource LineBoostPath}"/>
</Viewbox>
<Viewbox Grid.Row="1" Grid.RowSpan="2" VerticalAlignment="Top">
<TextBlock Margin="0"
FontSize="10" Foreground="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text=" Line " TextAlignment="Center" />
</Viewbox>
<Viewbox Grid.Row="2" VerticalAlignment="Bottom">
<TextBlock Margin="0"
FontSize="10" Foreground="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text=" Boost " TextAlignment="Center" />
</Viewbox>
</Grid>
</Border>
</UserControl>
28 changes: 28 additions & 0 deletions AccessBattleWpf/View/BoostField.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AccessBattle.Wpf.View
{
/// <summary>
/// Interaction logic for LinkField.xaml
/// </summary>
public partial class BoostField : UserControl
{
public BoostField()
{
InitializeComponent();
}
}
}
31 changes: 31 additions & 0 deletions AccessBattleWpf/View/FirewallField.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<UserControl x:Class="AccessBattle.Wpf.View.FirewallField"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AccessBattle.Wpf.View"
Width="48" Height="48" FontFamily="../ExternalResources/#Silkscreen">
<Border CornerRadius="2" BorderBrush="Yellow" BorderThickness="2" Background="White">
<Grid Margin="1">
<Grid.RowDefinitions>
<RowDefinition Height=".8*"/>
<RowDefinition Height=".3*"/>
</Grid.RowDefinitions>
<Viewbox Margin="2" Grid.Row="0">
<Path
Margin="1"
Fill="Black"
Stroke="Black" StrokeThickness="1"
Data="{StaticResource FirewallPath}"/>
</Viewbox>
<Viewbox Grid.Row="1">
<TextBlock Margin="2,0,2,0"
FontSize="10" Foreground="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Firewall" />
</Viewbox>
</Grid>

</Border>
</UserControl>
28 changes: 28 additions & 0 deletions AccessBattleWpf/View/FirewallField.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AccessBattle.Wpf.View
{
/// <summary>
/// Interaction logic for FirewallField.xaml
/// </summary>
public partial class FirewallField : UserControl
{
public FirewallField()
{
InitializeComponent();
}
}
}
5 changes: 4 additions & 1 deletion AccessBattleWpf/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
Title="Access Battle"
MinWidth="310" MinHeight="480"
Width="424" Height="646"
Background="Black">
Background="Black"
SnapsToDevicePixels="True"
UseLayoutRounding="True">
<Window.DataContext>
<viewmodel:MainWindowViewModel x:Name="ViewModel"/>
</Window.DataContext>
Expand Down Expand Up @@ -185,6 +187,7 @@
</Viewbox>
</local:BoardFieldView>

<!--TODO: It is probably much easier to define bindings within BoardFieldView.xaml and just set the DataContext here-->
<local:BoardFieldView
FieldVisualState="{Binding BoardFieldList[56].VisualState}" FieldCardVisualState="{Binding BoardFieldList[56].CardVisualState}"
Command="{Binding BoardFieldClickedCommand}" CommandParameter="56" IsHighlighted="{Binding BoardFieldList[56].IsHighlighted}"
Expand Down
104 changes: 103 additions & 1 deletion AccessBattleWpf/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AccessBattle.Wpf.ViewModel;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -41,6 +42,7 @@ public MainWindow()
{
Task.Delay(500).ContinueWith(o =>
{
//AnimatePlacement(1, 1, true, BoardFieldVisualState.Link);
//AnimateMovement(1, 1, 1, 9);
// Application.Current.Dispatcher.BeginInvoke((Action)(async () =>
Expand Down Expand Up @@ -90,7 +92,9 @@ private void Game_PropertyChanged(object sender, System.ComponentModel.PropertyC
// Trigger animation here
var move = cmd.Substring(2);

if (move.StartsWith("mv"))
// Move command
// mv 1,1,2,1
if (move.StartsWith("mv", StringComparison.InvariantCultureIgnoreCase))
{
var command = move.Substring(3).Trim();
var split = command.Split(new[] { ',' });
Expand Down Expand Up @@ -126,7 +130,103 @@ private void Game_PropertyChanged(object sender, System.ComponentModel.PropertyC
AnimateMovement(x1, y1, x2, y2);

}
// Boost or firewall command
// bs 1,1,1 // fw 1,1,1
else if (move.StartsWith("bs", StringComparison.InvariantCultureIgnoreCase) ||
move.StartsWith("fw", StringComparison.InvariantCultureIgnoreCase))
{
var boost = move.StartsWith("bs", StringComparison.InvariantCultureIgnoreCase);

move = move.Substring(3).Trim();
var split = move.Split(new[] { ',' });
if (split.Length != 3) return;

uint x1, y1, enabled;
if (!uint.TryParse(split[0], out x1) ||
!uint.TryParse(split[1], out y1) ||
!uint.TryParse(split[2], out enabled))
return;

// Convert to zero based index:
--x1; --y1;

// Invert Y
if (ViewModel.IsPlayerHost)
{
y1 = 7 - y1;
}
else // Invert X
{
x1 = 7 - x1;
}

if (x1 > 7 || y1 > 7 || enabled > 1)
return;

AnimatePlacement(
(int)x1, (int)y1, enabled == 1,
boost ? BoardFieldVisualState.LineBoost : BoardFieldVisualState.Firewall);
}

}

void AnimatePlacement(int x, int y, bool direction, BoardFieldVisualState card)
{
if (card != BoardFieldVisualState.LineBoost && card != BoardFieldVisualState.Firewall) return;

Application.Current.Dispatcher.BeginInvoke((Action)(async () =>
{
UserControl view;
if (card == BoardFieldVisualState.Firewall)
view = new FirewallField();
else view = new BoostField();
Grid.SetColumnSpan(view, 12);
Grid.SetRowSpan(view, 16);
view.HorizontalAlignment = HorizontalAlignment.Left;
view.VerticalAlignment = VerticalAlignment.Top;
var dx = XOf(x);
var dy = YOf(y);
view.Margin = new Thickness(dx, dy, 0, 0);
double startScale = direction ? 2 : 1;
double endScale = direction ? 1 : 2;
double increment = direction ? -0.05 : 0.05;
// TODO: correct CenterX/Y at the edges of the board
ScaleTransform scaleT = new ScaleTransform
{
ScaleY = startScale,
ScaleX = startScale,
CenterX = 24,
CenterY = 24
};
view.RenderTransform = scaleT;
MainGrid.Children.Add(view);
for (double i = startScale; direction && i>endScale || !direction && i<endScale ; i += increment)
{
scaleT.ScaleX = i;
scaleT.ScaleY = i;
await Task.Delay(50);
}
await Task.Delay(250);
for (double i = 1; i > 0; i -= 0.1)
{
await Task.Delay(50);
view.Opacity = i;
}
MainGrid.Children.Remove(view);
}));
}

// TODO: Handle Stack
Expand Down Expand Up @@ -172,6 +272,8 @@ void AnimateMovement(int x1, int y1, int x2, int y2)
await Task.Delay(50);
}
await Task.Delay(2500);
MainGrid.Children.Remove(line);
}));
Expand Down

0 comments on commit 1c693a0

Please sign in to comment.