Archive for the 'source code' Category

Pixel based destructible ground with Cocos2d

Micro Miners iOS game

OMG a giant worm! Micro Miners iOS game.

 

Worms, Lemmings, Micro Miners…

A pixel based destructible ground is the kind of engine used in many popular games such as early appstore hit iShoot by Ethan NicholasWorms, or Lemmings. And also, since the 15th of November 2012, in my new iOS game Micro Miners. I started Micro Miners two years ago, after testing an amazing class called CCMutableTexture that was crafted and shared by the not less amazing Lam Pham. “2 years ago” means the source code provided here is based on cocos2d v1, but not too difficult to adapt to v2, I guess.

For this article, I’ve created a Xcode v4.5 + Cocos2d v1.01 project with a basic destructible ground engine. This is intended to be very simple starting point if you want to make your own game base on destructible grounds. But in any case this is a ready to use destructible ground engine.

I also recorded a short video of the project in action on the iPhone simulator. A quick way to see if this article could be of any interest to you.

Source code : https://github.com/jpsarda/Pixel-based-destructible-ground-with-Cocos2d-iPhone

1) GitHub project and video demo

Watch carefully the video below, it shows exactly what you get when you download, compile and run the demo project provided on GitHub.

2) The performance bottleneck

The engine is based on the CCMutableTexture class. Basically, a CCMutableTexture maintains an array of pixels value representing the texture. You can then quickly access and modify the pixels in this array. But once you’re done with the modifications, you have to apply them. The internal pixel array is then drawn on the actual texture. This step is the performance bottleneck of the destructible ground engine. Knowing this, all must be done to minimize the surface of the CCMutableTexture we draw on, and the number of times we call apply on the CCMutableTexture.

3) Minimizing the surface of the CCMutableTextures

This is achieved in 2 steps

  1. 1 pixel on the texture will be rendered as a 2×2 square on the screen. In other words the displayed CCSprite associated to the texture is scaled by a factor of 2. The look is more pixelated but it’s still acceptable. For example, instead for applying our changes on a 320×480 texture, we will apply them on a 160×240 texture. This is 4 times less pixels to draw when we call apply on the CCMutableTexture.
  2. If we use full screen textures, each time we move our finger on the screen to dig the ground, the full screen CCMutableTexture is modified and we must call apply on it. That’s a pity because only a small part of the screen is affected by our finger. To prevent this, the ground is splited in zones. In the project provided, we’ve splited the ground in 4 stripes (from top to bottom). When we dig the ground, only the affected zones are redrawn (you can see the affected grounds colorized in red while digging).

Destructible ground, 1 zone affected

Finger digging in the middle of a ground zone, only 1 zone affected.

Destructible grouns, 2 zones affected

Finger digging at the border of 2 zones, 2 zones affected.

4) Minimizing the calls to apply

ccTouchesMoved, which is the method invoked when the user moves his finger on the screen, can be called at very high frequency. We can’t afford to call apply on the textures on every move. We solve this problem by introducing a minimum delay (0.5 seconds) to pass before taking into account a move of the finger. Illustrated with this peace of code in ccTouchesMoved :

if (now-lastDigTime>0.05f) {
    // determine affected zones ...
    // draw lines ...
    // apply changes ...
    lastDigTime=now;
}

Continue reading ‘Pixel based destructible ground with Cocos2d’

A SpaceManager Game

This article is going to build upon the basics described in the previous SpaceManager article written some time ago which can be found here: http://www.cocos2d-iphone.org/archives/677

About the Author:

My name is Rob, I’m one of the guys working under the mobile bros. LLC title. We’ve released a few games in the recent years, all using SpaceManager, including: Pachingo, Trundle, and Kill Timmy. I am personally interested in physics myself and always looking for new ways to incorporate them in our games.

Goals

Today’s goal is to design a basic game using Cocos2d, Chipmunk, and SpaceManager. I believe the best lessons are those learned from example, so for this article I’ll walk you through building a small example of a game and show off some of the features of SpaceManager. Some of the things we’ll demonstrate here are:

* Save and Load entire game state
* Explosions in Chipmunk
* Creating a debug-layer
* Using impulses
* Retina Support

So what type of game should we do… perhaps one where we slingshot grenades at structures hoping to kill some kind of ninja animal inside? Sounds like a plan, so lets dive right in. This tutorial will use Cocos2d 0.99.5 and SpaceManager 0.0.6. I HIGHLY recommend you download the accompanying source code for this article so you can follow along; this will have everything you need and you can skip the setup steps. Source is found here:

Download the Source-Code: GrenadeGame.zip

Edit: Updated source location

Continue reading ‘A SpaceManager Game’

cocos2d migrated to git/github

The official cocos2d source code repository was migrated from SVN to Git. Also, the source code is hosted in GitHub.

The master branch is the stable branch, while the develop branch is the unstable branch.

How to download the develop branch ?

git clone http://github.com/cocos2d/cocos2d-iphone.git develop

New to git ?

For further info, please see this thread: http://www.cocos2d-iphone.org/forum/topic/5295

Notifications in OpenGL (Like OpenFeint but without lag)

I (manucorporat) have developed a notification system for cocos2D. It’s very useful when your game uses OpenFeint or another social network.

OpenFeint has his own notification system, and probably you wonder, Why am I going to use this new system?

The answer is easy. The new system is designed in OpenGL and the performance is higher.

No more lag, when a friend connect!!!

In addition, It has a lot of advantages.

  • You can customize the notification design. (Friendly to the rest of the interface)
  • High Performance, because it uses OpenGl instead of UIKit.
  • Personalized animations (in and out).
  • cocos2d based. (Easy integration)
  • Personalized notification position (Top and bottom)
  • Easy way to play sounds when a notification is showed.
  • Easy implementation with OpenFeint.
  • Callback when it’s pressed. (Like Openfeint)

AbstractWar 2.0 uses this system.

Are you interested?

Integrating this feature in your game/app is very easy:
In CCDirector.m::mainloop
You must add [[CCNotifications sharedManager] visit]; after [runningScene_ visit];
And #import "CCNotifications.h"

/* draw the scene */
 
[runningScene_ visit];
[[CCNotifications sharedManager] visit]; //<<<<<<<<
if( displayFPS )
[self showFPS];

Ok, now you can push a notification if you write this code:

[[CCNotifications sharedManager] addNotificationTitle:@"Game Name:" message:@"Running in 1.4 version" image:nil tag:0 animate:YES];

Openfeint integration:

Open (ProyectName)AppDelegate.m
It must implement <OpenFeintDelegate, OFNotificationDelegate, CCNotificationsDelegate> protocols.

- (void) applicationDidFinishLaunching:(UIApplication *)application {
 
//Openfeint init
NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], OpenFeintSettingDashboardOrientation,
@"Name", OpenFeintSettingShortDisplayName,
window, OpenFeintSettingPresentationWindow,
nil
];
 
OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:self andChallengeDelegate:nil andNotificationDelegate:self];
 
[OpenFeint initializeWithProductKey:@"yourkey"
andSecret:@"yourpassword"
andDisplayName:@"your proyect name"
andSettings:settings
andDelegates:delegates];
 
//CCNotifications delegate
[[CCNotifications sharedManager] setDelegate:self];
}
 
#pragma mark OpenFeint Delegate methods
 
- (BOOL) isOpenFeintNotificationAllowed:(OFNotificationData*)notificationData{
 
return NO;
 
}
 
- (void)handleDisallowedNotification:(OFNotificationData*)notificationData{
 
[[CCNotifications sharedManager] addNotificationTitle:@"Openfeint:" message:[notificationData notificationText] image:@"openfeintLogo.png" tag:1 animate:YES];
 
}

Delegate methods:

- (void) notificationChangeState:(char)state tag:(int)tag {
if(state == kCCNotificationStateShowing){
//Play sound
}
}
- (BOOL) touched:(int)tag{
//Example
/*
if(tag==kTagOpenfeint) {
	[OpenFeint launchDashboard];
}
*/
}

Customize:
You can customize the notification design in CCNotificationDefaultDesign class.

  • The position:
[[CCNotifications sharedManager] setPosition:kCCNotificationPositionBottom]; //kCCNotificationPositionTop
  • Animations:

Animation (In):

[[CCNotifications sharedManager] setAnimationIn:kCCNotificationAnimationMovement time:0.4f];

Animation (Out):

[[CCNotifications sharedManager] setAnimationOut:kCCNotificationAnimationMovement time:0.4f];

Both:

[[CCNotifications sharedManager] setAnimation:kCCNotificationAnimationMovement time:0.4f];
  • Time:
[[CCNotifications sharedManager] setShowingTime:3.2f];

Other usages:

  • Show app version.
  • Music now playing
  • New DLC content. (In-app purchase)

Conclusion: If you have got a game with cocos2D and it uses OpenFeint, I recommend that you implement this feature. It’s also a plus to get Openfeint Gold.

Forum discussion
Check here to download the code.

For further updates, follow me on Twitter or keep an eye on my blog.




Social Widgets powered by AB-WebLog.com.