Skip to content

Commit

Permalink
Added sample for CocosSharp entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
vchelaru committed Apr 8, 2015
1 parent f33e6b4 commit bddb556
Show file tree
Hide file tree
Showing 19 changed files with 512 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CocosSharpEntities/EntityProject.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityProject", "EntityProject\EntityProject.csproj", "{1C5A66D2-4086-4860-BDCC-B9F794DEA08D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1C5A66D2-4086-4860-BDCC-B9F794DEA08D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C5A66D2-4086-4860-BDCC-B9F794DEA08D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C5A66D2-4086-4860-BDCC-B9F794DEA08D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C5A66D2-4086-4860-BDCC-B9F794DEA08D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions CocosSharpEntities/EntityProject/Assets/AboutAssets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Any raw assets you want to be deployed with your application can be placed in
this directory (and child directories) and given a Build Action of "AndroidAsset".

These files will be deployed with your package and will be accessible using Android's
AssetManager, like this:

public class ReadAsset : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

InputStream input = Assets.Open ("my_asset.txt");
}
}

Additionally, some Android functions will automatically load asset files:

Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions CocosSharpEntities/EntityProject/Entities/Bullet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using CocosSharp;

namespace EntityProject
{
public class Bullet : CCNode
{
CCSprite sprite;

public float VelocityX
{
get;
set;
}

public float VelocityY
{
get;
set;
}

public Bullet () : base()
{
sprite = new CCSprite ("bullet.png");
// Making the Sprite be centered makes
// positioning easier.
sprite.AnchorPoint = CCPoint.AnchorMiddle;
this.AddChild(sprite);

this.Schedule (ApplyVelocity);
}

void ApplyVelocity(float time)
{
PositionX += VelocityX * time;
PositionY += VelocityY * time;
}
}
}

47 changes: 47 additions & 0 deletions CocosSharpEntities/EntityProject/Entities/Ship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using CocosSharp;

namespace EntityProject
{
public class Ship : CCNode
{
CCSprite sprite;

CCEventListenerTouchAllAtOnce touchListener;

public Ship () : base()
{
sprite = new CCSprite ("ship.png");
// Center the Sprite in this entity to simplify
// centering the Ship on screen
sprite.AnchorPoint = CCPoint.AnchorMiddle;
this.AddChild(sprite);

touchListener = new CCEventListenerTouchAllAtOnce();
touchListener.OnTouchesMoved = HandleInput;
AddEventListener(touchListener, this);

Schedule (FireBullet, interval: 0.5f);

}

void FireBullet(float unusedValue)
{
Bullet newBullet = BulletFactory.Self.CreateNew ();
newBullet.Position = this.Position;
newBullet.VelocityY = 100;
}

private void HandleInput(System.Collections.Generic.List<CCTouch> touches, CCEvent touchEvent)
{
if(touches.Count > 0)
{
CCTouch firstTouch = touches[0];

this.PositionX = firstTouch.Location.X;
this.PositionY = firstTouch.Location.Y;
}
}
}
}

95 changes: 95 additions & 0 deletions CocosSharpEntities/EntityProject/EntityProject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>{1C5A66D2-4086-4860-BDCC-B9F794DEA08D}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>EntityProject</RootNamespace>
<AndroidApplication>True</AndroidApplication>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
<AssemblyName>EntityProject</AssemblyName>
<TargetFrameworkVersion>v4.4.87</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<AndroidLinkMode>None</AndroidLinkMode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="Mono.Android" />
<Reference Include="box2d">
<HintPath>..\packages\CocosSharp.Android.1.3.2.0\lib\MonoAndroid10\box2d.dll</HintPath>
</Reference>
<Reference Include="CocosSharp">
<HintPath>..\packages\CocosSharp.Android.1.3.2.0\lib\MonoAndroid10\CocosSharp.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.SharpZipLib">
<HintPath>..\packages\CocosSharp.Android.1.3.2.0\lib\MonoAndroid10\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="Lidgren.Network">
<HintPath>..\packages\CocosSharp.Android.1.3.2.0\lib\MonoAndroid10\Lidgren.Network.dll</HintPath>
</Reference>
<Reference Include="MonoGame.Framework">
<HintPath>..\packages\CocosSharp.Android.1.3.2.0\lib\MonoAndroid10\MonoGame.Framework.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GameAppDelegate.cs" />
<Compile Include="GameLayer.cs" />
<Compile Include="Entities\Ship.cs" />
<Compile Include="Entities\Bullet.cs" />
<Compile Include="Factories\BulletFactory.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" />
<None Include="Properties\AndroidManifest.xml" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\values\Strings.xml" />
<AndroidResource Include="Resources\drawable\Icon.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="Assets\Content\animations\" />
<Folder Include="Assets\Content\fonts\" />
<Folder Include="Assets\Content\images\hd\" />
<Folder Include="Assets\Content\images\ld\" />
<Folder Include="Assets\Content\sounds\" />
<Folder Include="Entities\" />
<Folder Include="Factories\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
<AndroidAsset Include="Assets\Content\ship.png" />
<AndroidAsset Include="Assets\Content\bullet.png" />
</ItemGroup>
</Project>
42 changes: 42 additions & 0 deletions CocosSharpEntities/EntityProject/Factories/BulletFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;

namespace EntityProject
{
public class BulletFactory
{
static BulletFactory self;

// simple singleton implementation
public static BulletFactory Self
{
get
{
if (self == null)
{
self = new BulletFactory ();
}
return self;
}
}

public event Action<Bullet> BulletCreated;

private BulletFactory()
{

}

public Bullet CreateNew()
{
Bullet newBullet = new Bullet ();

if (BulletCreated != null)
{
BulletCreated (newBullet);
}

return newBullet;
}
}
}

61 changes: 61 additions & 0 deletions CocosSharpEntities/EntityProject/GameAppDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using CocosSharp;


namespace EntityProject
{
public class GameAppDelegate : CCApplicationDelegate
{
public override void ApplicationDidFinishLaunching (CCApplication application, CCWindow mainWindow)
{

application.PreferMultiSampling = false;
application.ContentRootDirectory = "Content";
application.ContentSearchPaths.Add ("animations");
application.ContentSearchPaths.Add ("fonts");
application.ContentSearchPaths.Add ("sounds");

CCSize windowSize = mainWindow.WindowSizeInPixels;

float desiredWidth = 1024.0f;
float desiredHeight = 768.0f;

// This will set the world bounds to be (0,0, w, h)
// CCSceneResolutionPolicy.ShowAll will ensure that the aspect ratio is preserved
CCScene.SetDefaultDesignResolution (desiredWidth, desiredHeight, CCSceneResolutionPolicy.ShowAll);

// Determine whether to use the high or low def versions of our images
// Make sure the default texel to content size ratio is set correctly
// Of course you're free to have a finer set of image resolutions e.g (ld, hd, super-hd)
if (desiredWidth < windowSize.Width)
{
application.ContentSearchPaths.Add ("images/hd");
CCSprite.DefaultTexelToContentSizeRatio = 2.0f;
}
else
{
application.ContentSearchPaths.Add ("images/ld");
CCSprite.DefaultTexelToContentSizeRatio = 1.0f;
}

// New code for resolution setting:
CCScene.SetDefaultDesignResolution(480, 320, CCSceneResolutionPolicy.ShowAll);

CCScene scene = new CCScene (mainWindow);
GameLayer gameLayer = new GameLayer ();

scene.AddChild (gameLayer);
mainWindow.RunWithScene (scene);
}

public override void ApplicationDidEnterBackground (CCApplication application)
{
application.Paused = true;
}

public override void ApplicationWillEnterForeground (CCApplication application)
{
application.Paused = false;
}
}
}
50 changes: 50 additions & 0 deletions CocosSharpEntities/EntityProject/GameLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using CocosSharp;

namespace EntityProject
{
public class GameLayer : CCLayer
{
Ship ship;
List<Bullet> bullets;

public GameLayer ()
{
ship = new Ship ();
ship.PositionX = 240;
ship.PositionY = 50;
this.AddChild (ship);

bullets = new List<Bullet> ();
BulletFactory.Self.BulletCreated += HandleBulletCreated;
}

void HandleBulletCreated(Bullet newBullet)
{
AddChild (newBullet);
bullets.Add (newBullet);
}

protected override void AddedToScene ()
{
base.AddedToScene ();

// Use the bounds to layout the positioning of our drawable assets
CCRect bounds = VisibleBoundsWorldspace;

// Register for touch events
var touchListener = new CCEventListenerTouchAllAtOnce ();
touchListener.OnTouchesEnded = OnTouchesEnded;
AddEventListener (touchListener, this);
}

void OnTouchesEnded (List<CCTouch> touches, CCEvent touchEvent)
{
if (touches.Count > 0)
{
// Perform touch handling here
}
}
}
}
Loading

0 comments on commit bddb556

Please sign in to comment.