Christian Fischer avatar Christian Fischer committed bfebfa6

player + collectable + movement + collission

Comments (0)

Files changed (21)

Classes/Android.mk

 LOCAL_MODULE_FILENAME := libgame_logic
 
 LOCAL_SRC_FILES := AppDelegate.cpp \
-                   HelloWorldScene.cpp
+                   HelloWorldScene.cpp \
+					GameScene.cpp \
+					GameObject.cpp \
+					Elephant.cpp \
+					Collectable.cpp \
                    
 LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
 

Classes/AppDelegate.cpp

 
 #include "cocos2d.h"
 #include "HelloWorldScene.h"
+#include "GameScene.h"
 
 #include "CCEGLView.h"
 
 	pDirector->setAnimationInterval(1.0 / 60);
 
 	// create a scene. it's an autorelease object
-	CCScene *pScene = HelloWorld::scene();
+	CCScene *pScene = GameScene::scene();
 
 	// run
 	pDirector->runWithScene(pScene);

Classes/COLLECTABLE.CPP

+#include "Collectable.h"
+#include "Elephant.h"
+#include "cocos2d.h"
+
+using namespace cocos2d;
+using namespace std;
+
+
+Collectable::Collectable() {
+	this->speed = 25;
+	return;
+}
+
+Collectable::~Collectable() {
+	return;
+}
+
+
+bool Collectable::initGameObject(const std::string &name) {
+	if (!GameObject::initGameObject(name)) {
+		return false;
+	}
+
+	runAction(CCRepeatForever::actionWithAction(
+				CCRotateBy::actionWithDuration(
+							0.5f + (4.0f * rand() / RAND_MAX),
+							360 * ((rand() & 1) ? -1 : +1)
+				)
+	));
+
+	return true;
+}
+
+
+
+void Collectable::onCollission(GameObject *other) {
+	Elephant *elephant = dynamic_cast<Elephant*>(other);
+
+	if (elephant) {
+		runAction(CCSequence::actionOneTwo(
+			CCEaseElasticIn::actionWithAction(CCScaleTo::actionWithDuration(0.7f, 0.0f)),
+			CCCallFunc::actionWithTarget(this, callfunc_selector(GameObject::self_destruct))
+		));
+	}
+
+	return;
+}

Classes/COLLECTABLE.H

+#ifndef COLLECTABLE_H
+#define COLLECTABLE_H
+
+#include "GameObject.h"
+
+
+class Collectable
+	: public GameObject
+{
+public:
+	Collectable();
+	virtual ~Collectable();
+
+	virtual bool initGameObject(const std::string &name);
+
+
+	virtual void onCollission(GameObject *other);
+};
+
+
+#endif // COLLECTABLE_H

Classes/ELEPHANT.CPP

+#include "Elephant.h"
+#include "cocos2d.h"
+
+using namespace cocos2d;
+using namespace std;
+
+
+Elephant::Elephant() {
+	this->speed = 100;
+	return;
+}
+
+Elephant::~Elephant() {
+	return;
+}
+
+
+bool Elephant::initGameObject(const std::string &name) {
+	if (!GameObject::initGameObject(name)) {
+		return false;
+	}
+
+	return true;
+}
+
+
+
+void Elephant::onPositionChanged(CCPoint *position) {
+	CCSize winsize = CCDirector::sharedDirector()->getWinSize();
+	CCSize size = this->getContentSize();
+	size.width  *= getScale();
+	size.height *= getScale();
+
+	position->x = max(position->x,                  size.width/2);
+	position->x = min(position->x, winsize.width  - size.width/2);
+	position->y = max(position->y,                  size.height/2);
+	position->y = min(position->y, winsize.height - size.height/2);
+
+	return;
+}

Classes/ELEPHANT.H

+#ifndef ELEPHANT_H
+#define ELEPHANT_H
+
+#include "GameObject.h"
+
+
+class Elephant
+	: public GameObject
+{
+public:
+	Elephant();
+	virtual ~Elephant();
+
+	virtual bool initGameObject(const std::string &name);
+
+
+	virtual void onPositionChanged(cocos2d::CCPoint *position);
+};
+
+
+#endif // GAMEOBJECT_H

Classes/GAMEOBJECT.CPP

+#include "GameObject.h"
+
+#include <sstream>
+
+using namespace cocos2d;
+using namespace std;
+
+
+GameObject::GameObject() {
+	this->vec_target_x  = 0.0f;
+	this->vec_target_y  = 0.0f;
+	this->vec_current_x = 0.0f;
+	this->vec_current_y = 0.0f;
+	return;
+}
+
+GameObject::~GameObject() {
+	return;
+}
+
+
+bool GameObject::initGameObject(const string &name) {
+	this->name = name;
+
+	string framename = (name + "_up.png");
+	CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(framename.c_str());
+	assert(frame);
+
+	if (!initWithSpriteFrame(frame)) {
+		return false;
+	}
+
+	updateImage();
+
+	setAnchorPoint(ccp(.5f, .5f));
+	setScale(0.25f);
+
+	return true;
+}
+
+
+void GameObject::updateImage() {
+	string direction = "up";
+
+	stringstream framename;
+	framename << name;
+	framename << "_";
+	framename << direction;
+	framename << ".png";
+
+	CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(framename.str().c_str());
+	this->setDisplayFrame(frame);
+}
+
+
+void GameObject::setMovement(float x, float y) {
+	this->vec_target_x = x;
+	this->vec_target_y = y;
+}
+
+
+void GameObject::move(float dt, const CCRect &bounds) {
+	CCPoint position = getPosition();
+
+	float diff_x = vec_target_x - vec_current_x;
+	float diff_y = vec_target_y - vec_current_y;
+	vec_current_x += diff_x * 0.1f;
+	vec_current_y += diff_y * 0.1f;
+
+	if (vec_current_x != 0 || vec_current_y != 0) {
+		position.x += vec_current_x * dt * speed;
+		position.y += vec_current_y * dt * speed;
+
+		onPositionChanged(&position);
+
+		setPosition(position);
+	}
+
+	return;
+}
+
+
+void GameObject::onPositionChanged(CCPoint *position) {
+
+}
+
+
+CCRect GameObject::getCollisionRect() {
+	CCPoint position = getPosition();
+	CCSize size = getContentSize();
+	size.width  *= getScale();
+	size.height *= getScale();
+
+	return CCRectMake(
+			-size.width / 2  + position.x,
+			-size.height / 2 + position.y,
+			 size.width,
+			 size.height
+	);
+}
+
+
+bool GameObject::doCollission(GameObject *other) {
+	if (CCRect::CCRectIntersectsRect(this->getCollisionRect(), other->getCollisionRect())) {
+		onCollission(other);
+		return true;
+	}
+
+	return false;
+}
+
+
+void GameObject::onCollission(GameObject *other) {
+	return;
+}
+
+
+void GameObject::self_destruct() {
+	this->removeFromParentAndCleanup(true);
+}

Classes/GAMEOBJECT.H

+#ifndef GAMEOBJECT_H
+#define GAMEOBJECT_H
+
+#include "cocos2d.h"
+
+#include <string>
+
+
+class GameScene;
+
+class GameObject
+	: public cocos2d::CCSprite
+{
+public:
+	GameObject();
+	virtual ~GameObject();
+
+	virtual bool initGameObject(const std::string &name);
+
+	template <typename CLASS>
+	static CLASS *create(const std::string &name) {
+		CLASS *object = new CLASS();
+
+		if (object->initGameObject(name)) {
+			object->autorelease();
+			return object;
+		}
+
+		delete object;
+		return NULL;
+	}
+
+	virtual cocos2d::CCRect getCollisionRect();
+	virtual bool doCollission(GameObject *other);
+
+	virtual void updateImage();
+
+
+	virtual void setMovement(float x, float y);
+	virtual void move(float dt, const cocos2d::CCRect &bounds);
+
+	void self_destruct();
+
+
+protected:
+	virtual void onPositionChanged(cocos2d::CCPoint *position);
+	virtual void onCollission(GameObject *other);
+	
+protected:
+	GameScene *gamescene;
+	std::string name;
+
+	float vec_target_x;
+	float vec_target_y;
+	float vec_current_x;
+	float vec_current_y;
+	float speed;
+};
+
+
+#endif // GAMEOBJECT_H

Classes/GAMESCENE.CPP

+#include "GameScene.h"
+
+#include "GameObject.h"
+#include "Collectable.h"
+
+#include <time.h>
+#include <algorithm>
+
+using namespace cocos2d;
+
+
+GameScene::GameScene() {
+	srand((unsigned)time(NULL));
+
+	this->elephant = NULL;
+	this->timeout     = 0.0f;
+	this->timeout_max = 1.0f;
+
+	return;
+}
+
+GameScene::~GameScene() {
+	return;
+}
+
+
+CCScene *GameScene::scene() {
+	// 'scene' is an autorelease object
+	CCScene *scene = CCScene::node();
+	
+	// 'layer' is an autorelease object
+	GameScene *layer = new GameScene();
+	if (layer->init()) {
+		layer->autorelease();
+
+		// add layer as a child to scene
+		scene->addChild(layer);
+	}
+	else {
+		delete layer;
+	}
+
+	return scene;
+}
+
+
+void GameScene::onExit() {
+	for(ObjectList::iterator it=objects.begin(); it!=objects.end(); it++) {
+		(*it)->removeFromParentAndCleanup(true);
+		(*it)->release();
+	}
+
+	if (elephant) {
+		elephant->release();
+	}
+
+	return;
+}
+
+
+bool GameScene::init() {
+	if (!CCLayer::init()) {
+		return false;
+	}
+
+	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(
+				"sprites.plist",
+				CCTextureCache::sharedTextureCache()->addImage("sprites.png")
+	);
+
+	CCSize winsize = CCDirector::sharedDirector()->getWinSize();
+
+	// create the player
+	this->elephant = GameObject::create<Elephant>("elephant");
+	assert(elephant);
+
+	if (this->elephant) {
+		this->elephant->setPosition(ccp(winsize.width/2, winsize.height/2));
+		this->elephant->retain();
+		addObject(this->elephant);
+		this->reorderChild(elephant, 10);
+	}
+
+	CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false);
+	CCScheduler::sharedScheduler()->scheduleUpdateForTarget(this, 0, false);
+	
+	return true;
+}
+
+
+bool GameScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
+	CCSize winsize = CCDirector::sharedDirector()->getWinSize();
+	CCPoint point = pTouch->locationInView(0);
+
+	float x = 0.0f;
+	float y = 0.0f;
+
+	if (point.x < winsize.width*1/4) {
+		x = -1;
+	}
+
+	if (point.x > winsize.width*3/4) {
+		x = +1;
+	}
+
+	if (point.y > winsize.height*4/5) {
+		y = -1;
+	}
+	
+	if (point.y < winsize.height*3/5) {
+		y = +1;
+	}
+
+	elephant->setMovement(x, y);
+
+	return true;
+}
+
+void GameScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {
+	elephant->setMovement(0.0f, 0.0f);
+	return;
+}
+
+
+void GameScene::update(ccTime dt) {
+	CCSize winsize = CCDirector::sharedDirector()->getWinSize();
+	CCRect bounds = CCRectMake(0, 0, winsize.width, winsize.height);
+
+	for(ObjectList::iterator it=objects.begin(); it!=objects.end(); it++) {
+		(*it)->move(dt, bounds);
+	}
+
+	// check collissions
+	for(int i=objects.size(); --i>=0;) {
+		GameObject *obj = objects[i];
+		bool collission = false;
+
+		if (obj != elephant) {
+			collission = obj->doCollission(elephant);
+		}
+
+		if (collission) {
+			ObjectList::iterator it = std::find(objects.begin(), objects.end(), obj);
+			if (it != objects.end()) {
+				objects.erase(it);
+			}
+
+			obj->release();
+		}
+	}
+
+	// spawn stuff
+	if ((timeout -= dt) < 0.0f) {
+		timeout = timeout_max;
+		timeout_max *= 0.99f;
+
+		GameObject *spawn = (rand() % 20)
+			?	GameObject::create<Collectable>("peanut")
+			:	GameObject::create<Collectable>("peanut")
+		;
+
+		spawn->setPosition(ccp(
+				winsize.width  * (1.0f * rand() / RAND_MAX),
+				winsize.height
+		));
+		spawn->setMovement(0.0f, -1.0f);
+
+		addObject(spawn);
+	}
+
+	return;
+}
+
+
+
+void GameScene::addObject(GameObject *object) {
+	this->objects.push_back(object);
+	this->addChild(object);
+	object->retain();
+
+	return;
+}
+
+
+

Classes/GAMESCENE.H

+#ifndef GAMESCENE_H
+#define GAMESCENE_H
+
+#include "cocos2d.h"
+
+#include "GameObject.h"
+#include "Elephant.h"
+
+
+class GameScene
+	: public cocos2d::CCLayer
+{
+public:
+	GameScene();
+	virtual ~GameScene();
+	virtual void onExit();
+
+	static cocos2d::CCScene *scene();
+
+	void addObject(GameObject *object);
+
+
+	virtual void update(cocos2d::ccTime dt);
+	virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
+    virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
+	
+private:
+	virtual bool init();
+
+
+private:
+	Elephant	*elephant;
+
+	typedef std::vector<GameObject*> ObjectList;
+	ObjectList objects;
+
+	float timeout;
+	float timeout_max;
+};
+
+
+#endif // GAMESCENE_H

Classes/HelloWorldScene.cpp

 
 USING_NS_CC;
 
+#include "Elephant.h"
+
 CCScene* HelloWorld::scene()
 {
 	// 'scene' is an autorelease object
Add a comment to this file

GRAPHICS/elephant/elephant_up.png

Added
New image
Add a comment to this file

GRAPHICS/elephant/elephant_up.xcf

Binary file added.

Add a comment to this file

GRAPHICS/elephant/peanut.xcf

Binary file added.

Add a comment to this file

GRAPHICS/elephant/peanut_up.png

Added
New image

Binary file added.

Resources/SPRITES.PLIST

+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+    <dict>
+        <key>frames</key>
+        <dict>
+            <key>elephant_up.png</key>
+            <dict>
+                <key>frame</key>
+                <string>{{2,2},{344,376}}</string>
+                <key>offset</key>
+                <string>{0,0}</string>
+                <key>sourceSize</key>
+                <string>{344,376}</string>
+            </dict>
+            <key>peanut_up.png</key>
+            <dict>
+                <key>frame</key>
+                <string>{{2,380},{198,181}}</string>
+                <key>offset</key>
+                <string>{0,0}</string>
+                <key>sourceSize</key>
+                <string>{198,181}</string>
+            </dict>
+        </dict>
+        <key>metadata</key>
+        <dict>
+            <key>format</key>
+            <integer>1</integer>
+            <key>realTextureFileName</key>
+            <string>sprites.png</string>
+            <key>size</key>
+            <string>{512,1024}</string>
+            <key>smartupdate</key>
+            <string>$TexturePacker:SmartUpdate:da8a64f8da8c3b0c9433e5680bb74b1d$</string>
+            <key>textureFileName</key>
+            <string>sprites.png</string>
+        </dict>
+    </dict>
+</plist>
Added
New image
Add a comment to this file

win32/LuftKriegsElefant-win32.vc2010.sdf

Binary file modified.

win32/LuftKriegsElefant.win32.vcxproj

   </ItemDefinitionGroup>
   <ItemGroup>
     <ClCompile Include="..\Classes\AppDelegate.cpp" />
+    <ClCompile Include="..\Classes\Collectable.cpp" />
+    <ClCompile Include="..\Classes\Elephant.cpp" />
+    <ClCompile Include="..\Classes\GameObject.cpp" />
+    <ClCompile Include="..\Classes\GameScene.cpp" />
     <ClCompile Include="..\Classes\HelloWorldScene.cpp" />
     <ClCompile Include="main.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\Classes\AppDelegate.h" />
+    <ClInclude Include="..\Classes\Collectable.h" />
+    <ClInclude Include="..\Classes\Elephant.h" />
+    <ClInclude Include="..\Classes\GameObject.h" />
+    <ClInclude Include="..\Classes\GameScene.h" />
     <ClInclude Include="..\Classes\HelloWorldScene.h" />
     <ClInclude Include="main.h" />
   </ItemGroup>

win32/LuftKriegsElefant.win32.vcxproj.filters

     <ClCompile Include="..\Classes\HelloWorldScene.cpp">
       <Filter>Classes</Filter>
     </ClCompile>
+    <ClCompile Include="..\Classes\GameScene.cpp">
+      <Filter>Classes</Filter>
+    </ClCompile>
+    <ClCompile Include="..\Classes\GameObject.cpp">
+      <Filter>Classes</Filter>
+    </ClCompile>
+    <ClCompile Include="..\Classes\Elephant.cpp">
+      <Filter>Classes</Filter>
+    </ClCompile>
+    <ClCompile Include="..\Classes\Collectable.cpp">
+      <Filter>Classes</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="main.h">
     <ClInclude Include="..\Classes\HelloWorldScene.h">
       <Filter>Classes</Filter>
     </ClInclude>
+    <ClInclude Include="..\Classes\GameScene.h">
+      <Filter>Classes</Filter>
+    </ClInclude>
+    <ClInclude Include="..\Classes\GameObject.h">
+      <Filter>Classes</Filter>
+    </ClInclude>
+    <ClInclude Include="..\Classes\Elephant.h">
+      <Filter>Classes</Filter>
+    </ClInclude>
+    <ClInclude Include="..\Classes\Collectable.h">
+      <Filter>Classes</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.