Skip to content

Commit

Permalink
development
Browse files Browse the repository at this point in the history
  • Loading branch information
bunnies authored and bunnies committed Mar 21, 2010
1 parent 87d0c5d commit 38711a3
Show file tree
Hide file tree
Showing 46 changed files with 227 additions and 732 deletions.
5 changes: 3 additions & 2 deletions AlkuIkkuna.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Seikkailu Laakso" Height="788" Width="1024">
<Canvas>
<Image Canvas.Left="0" Canvas.Top="0" Width="1024" Height="768" Source="file:///E:/programming/SeikkailuLaakso/alkukuva.png" />
<Button Canvas.Left="20" Canvas.Top="190" FontSize="24" Click="Aloitetaan">Aloita</Button>
<Image Canvas.Left="0" Canvas.Top="0" Width="1024" Height="768" Source="file:///C:/work/SeikkailuLaakso/alkukuva.png" />
<Button Canvas.Left="20" Canvas.Top="190" FontSize="24" Click="Aloitetaan" IsDefault="True">Aloita</Button>
<Button Canvas.Left="113" Canvas.Top="190" FontSize="24" Click="UkkelinMuokkaus" Height="37.923" Width="199">Ukkelin muokkaus</Button>
</Canvas>
</Window>
4 changes: 4 additions & 0 deletions AlkuIkkuna.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public AlkuIkkuna()

private void Aloitetaan(object sender, RoutedEventArgs e)
{
new Peli().Show();
}

private void UkkelinMuokkaus(object sender, RoutedEventArgs e) {
new UkkelinMuokkaus().Show();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Peli.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:L="clr-namespace:Net.Brotherus.SeikkailuLaakso"
Title="Peli" Height="880" Width="1000" WindowState="Maximized">
Title="Peli" Height="1200" Width="1600" WindowState="Normal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
Expand All @@ -13,7 +13,7 @@
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="_Game">
<MenuItem Header="_Save" Click="MenuItem_Click"/>
<MenuItem Header="_Save" Click="Save_Click"/>
<MenuItem Header="_Play" Click="PlayGame_Click"/>
</MenuItem>
</Menu>
Expand Down
53 changes: 31 additions & 22 deletions Peli.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -31,15 +32,19 @@ public Peli() {
// Create land if no previous content
if (this.scene.Children.Count == 0)
{
for(int x = 0; x < 12; ++x) {
this.scene.AddRectPolygon(900, x * 200, 200, 100);
}
CreateNewLand();
}
scene.MouseMove += scene_MouseMove;
scene.MouseLeftButtonDown += scene_MouseLeftButtonDown;
scene.MouseLeftButtonUp += scene_MouseLeftButtonUp;
}

private void CreateNewLand() {
for (int x = 0; x < 24; ++x) {
this.scene.AddRectPolygon(1100, x * 200, 200, 100);
}
}

private void scene_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
scene.StartDrawing(e);
Expand All @@ -64,37 +69,41 @@ private string SaveDir
{
get
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SeikkailuMaa");
return Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "SeikkailuMaa");
}
}

private void PlayGame_Click(object sender, RoutedEventArgs e)
{
using (var game = new SeikkailuLaaksoGame())
{
public void StartGame() {
using (var game = new SeikkailuLaaksoGame()) {
int i = 1;
foreach (FrameworkElement sceneItem in scene.Children)
{
var encoder = RenderToBitmap(sceneItem);
string picFile = Path.Combine(SaveDir, "scene_item_" + i.ToString() + ".png");
using (var picStream = new FileStream(picFile, FileMode.Create)) {
encoder.Save(picStream);
var items = new System.Collections.ArrayList(scene.Children);
foreach (FrameworkElement sceneItem in items) {
try {
var encoder = RenderToBitmap(sceneItem);
string picFile = Path.Combine(SaveDir, "scene_item_" + i.ToString() + ".png");
using (var picStream = new FileStream(picFile, FileMode.Create)) {
encoder.Save(picStream);
}
game.AddPolygonObstacle(picFile, Canvas.GetLeft(sceneItem), Canvas.GetTop(sceneItem));
++i;
} catch (Exception ex) {
Debug.WriteLine(ex);
scene.Children.Remove(sceneItem);
}
game.AddPolygonObstacle(picFile, Canvas.GetLeft(sceneItem), Canvas.GetTop(sceneItem));
++i;
}
game.Run();
}
}

private void PlayGame_Click(object sender, RoutedEventArgs e)
{
StartGame();
}

[STAThread]
public static void Main()
{
//new Application().Run(new AlkuIkkuna());

new Application().Run(new Peli());

//new Peli().PlayGame_Click(null, null);
new Application().Run(new AlkuIkkuna());
}

private static PngBitmapEncoder RenderToBitmap(FrameworkElement sceneItem)
Expand All @@ -114,7 +123,7 @@ private static PngBitmapEncoder RenderToBitmap(FrameworkElement sceneItem)
return encoder;
}

private void MenuItem_Click(object sender, RoutedEventArgs e)
private void Save_Click(object sender, RoutedEventArgs e)
{
if (!Directory.Exists(SaveDir)) Directory.CreateDirectory(SaveDir);
using (var stream = new FileStream(this.SaveFileName, FileMode.Create))
Expand Down
40 changes: 0 additions & 40 deletions PeliXNA/AngleTargetLimitJoint.cs

This file was deleted.

Binary file removed PeliXNA/Content/Common/background.png
Binary file not shown.
Binary file removed PeliXNA/Content/Common/blank.png
Binary file not shown.
Binary file removed PeliXNA/Content/Common/gradient.png
Binary file not shown.
Binary file removed PeliXNA/Content/Common/logo.png
Binary file not shown.
22 changes: 0 additions & 22 deletions PeliXNA/Content/Content.contentproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,6 @@
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Common\background.png">
<Name>background</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
<Compile Include="Common\blank.png">
<Name>blank</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
<Compile Include="Common\gradient.png">
<Name>gradient</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
<Compile Include="Common\logo.png">
<Name>logo</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Fonts\detailsFont.spritefont">
<Name>detailsFont</Name>
Expand Down
6 changes: 5 additions & 1 deletion PeliXNA/Content/Content.contentproj.user
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions PeliXNA/DrawingSystem/LineBrush.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FarseerGames.FarseerPhysics.Mathematics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Vector2 = Microsoft.Xna.Framework.Vector2;

namespace FarseerGames.GettingStarted.DrawingSystem
{
Expand Down
104 changes: 48 additions & 56 deletions PeliXNA/GameScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,32 @@
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.GettingStarted.DrawingSystem;
using Net.Brotherus.SeikkailuLaakso;
using Vector2Fs = FarseerGames.FarseerPhysics.Mathematics.Vector2;
using Vector2Xna = Microsoft.Xna.Framework.Vector2;

namespace Net.Brotherus
{
public class GameScreen : DrawableGameComponent
{
private static readonly Vector2 GRAVITY = new Vector2(0, 1000);
private float _scrollPosition = 0.0f;
private float _scrollSpeed = 0.0f;


public static Point ScreenSize = new Point(1600, 1200);
private static readonly float SCROLL_MARGIN = 400.0f;

private static readonly Vector2Fs GRAVITY = new Vector2Fs(0, 1000);
private PhysicsSimulator _physicsSimulator;
private ContentManager _contentManager;
private SpriteBatch _spriteBatch;
private InputState _input = new InputState();

private Ukkeli _ukkeli;
private UkkeliSimple _ukkeli;
private List<PolygonObstacle> _obstacles;

private LineBrush _lineBrush = new LineBrush(1, Color.Black); //used to draw spring on mouse grab
private FixedLinearSpring _mousePickSpring;
private Geom _pickedGeom;

private Texture2D _background;

public GameScreen(Game game) : base(game)
{
Expand All @@ -35,7 +44,7 @@ public GameScreen(Game game) : base(game)
_obstacles = new List<PolygonObstacle>();
}

public void AddObstacle(string picFile, Vector2 position)
public void AddObstacle(string picFile, Vector2Fs position)
{
_obstacles.Add(new PolygonObstacle(position, picFile));
}
Expand All @@ -46,9 +55,10 @@ public void AddObstacle(string picFile, Vector2 position)
protected override void LoadContent()
{
// Load content belonging to the screen manager.
_background = Texture2D.FromFile(GraphicsDevice, "Content/taustakuvat/berkin_talo.png");
_spriteBatch = new SpriteBatch(GraphicsDevice);
_lineBrush.Load(GraphicsDevice);
_ukkeli = new Ukkeli(new Vector2(150, 800), GraphicsDevice, _physicsSimulator);
_ukkeli = new UkkeliSimple(new Vector2Fs(150, 800), GraphicsDevice, _physicsSimulator);
foreach (var obstacle in _obstacles)
{
obstacle.Load(GraphicsDevice, _physicsSimulator);
Expand All @@ -58,77 +68,59 @@ protected override void LoadContent()
/// <summary>
/// Unload your graphics content.
/// </summary>
protected override void UnloadContent()
{
protected override void UnloadContent() {
_contentManager.Unload();
_physicsSimulator.Clear();
}

public override void Update(GameTime gameTime)
{
public override void Update(GameTime gameTime) {
// Read the keyboard and gamepad.
_input.Update();
float secondsElapsed = gameTime.ElapsedGameTime.Milliseconds * .001f;
_physicsSimulator.Update(secondsElapsed);
_ukkeli.HandleKeyboardInput(_input, gameTime);
HandleMouseInput(_input);
Scroll(secondsElapsed);
}

private void HandleMouseInput(InputState input)
{
Vector2 point = new Vector2(input.CurrentMouseState.X, input.CurrentMouseState.Y);
if (input.LastMouseState.LeftButton == ButtonState.Released &&
input.CurrentMouseState.LeftButton == ButtonState.Pressed)
{
CreateMouseSpring(point);
}
else if (input.LastMouseState.LeftButton == ButtonState.Pressed &&
input.CurrentMouseState.LeftButton == ButtonState.Released)
{
DestroyMouseSpring();
}
//move anchor point
if (input.CurrentMouseState.LeftButton == ButtonState.Pressed && _mousePickSpring != null)
{
_mousePickSpring.WorldAttachPoint = point;
}
}

private void DestroyMouseSpring()
{
if (_mousePickSpring != null && _mousePickSpring.IsDisposed == false)
{
_mousePickSpring.Dispose();
_mousePickSpring = null;
}
}

private void CreateMouseSpring(Vector2 point)
{
_pickedGeom = _physicsSimulator.Collide(point);
if (_pickedGeom != null)
{
_mousePickSpring = SpringFactory.Instance.CreateFixedLinearSpring(_physicsSimulator,
_pickedGeom.Body, _pickedGeom.Body.GetLocalPosition(point), point, 20, 10);
private void Scroll(float secondsElapsed) {
if (ManOnScreenX > ScreenSize.X - SCROLL_MARGIN) {
_scrollSpeed += secondsElapsed;
} else if (ManOnScreenX < SCROLL_MARGIN) {
_scrollSpeed -= secondsElapsed;
} else if (Math.Abs(_scrollSpeed) < 0.1f ) {
_scrollSpeed = 0.0f;
} else {
_scrollSpeed -= Math.Sign(_scrollSpeed) * secondsElapsed;
}
_scrollSpeed = _scrollSpeed.InLimits(-100.0f, 100.0f);
_scrollPosition += secondsElapsed * _scrollSpeed * 500.0f;
}

/// <summary>
/// This is called when the screen should draw itself.
/// </summary>
public override void Draw(GameTime gameTime)
{
public override void Draw(GameTime gameTime) {
_spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
foreach (var obstacle in _obstacles) obstacle.Draw(_spriteBatch);
_ukkeli.Draw(_spriteBatch);
if (_mousePickSpring != null)
{
_lineBrush.Draw(_spriteBatch,
_mousePickSpring.Body.GetWorldPosition(_mousePickSpring.BodyAttachPoint),
_mousePickSpring.WorldAttachPoint);

Action<Texture2D, Vector2Fs /*pos*/, float /*rot*/, Vector2Fs /*origin*/> drawer = (tex, pos, rot, orig) => {
_spriteBatch.Draw(tex, pos.ToVector2Xna(-_scrollPosition), null, Color.White, rot, orig.ToVector2Xna(), 1, SpriteEffects.None, 0);
};

// Background is made always only of three tiles so we must alter which ones
int bgIndex = Convert.ToInt32( Math.Floor(_scrollPosition / ScreenSize.X) );
for (int i = bgIndex - 1; i <= bgIndex + 1; ++i) {
var flip = i % 2 == 0 ? SpriteEffects.None : SpriteEffects.FlipHorizontally;
_spriteBatch.Draw(_background, new Rectangle((int)-_scrollPosition + ScreenSize.X * i, 0, ScreenSize.X, ScreenSize.Y), null, Color.White, 0, Vector2Xna.Zero, flip, 0.0f);
}


foreach (var obstacle in _obstacles) obstacle.Draw(drawer);
_ukkeli.Draw(drawer);
_spriteBatch.End();
}

private float ManOnScreenX {
get { return _ukkeli.Position.X - _scrollPosition; }
}
}
}
Loading

0 comments on commit 38711a3

Please sign in to comment.