Christian Fischer avatar Christian Fischer committed f90c6a7

enemy + bullets

Comments (0)

Files changed (25)

Classes/Android.mk

 					GameObject.cpp \
 					Elephant.cpp \
 					Collectable.cpp \
+					Enemy.cpp \
+					Bullet.cpp \
                    
 LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
 

Classes/BULLET.CPP

+#include "Bullet.h"
+#include "Enemy.h"
+#include "cocos2d.h"
+
+#include <sstream>
+
+using namespace cocos2d;
+using namespace std;
+
+
+Bullet::Bullet() {
+	this->speed = 75;
+	return;
+}
+
+Bullet::~Bullet() {
+	return;
+}
+
+
+bool Bullet::initGameObject(const std::string &name) {
+	if (!GameObject::initGameObject(name)) {
+		return false;
+	}
+
+	this->setScale(.15f);
+
+	for(int i=1; i<=4; i++) {
+		stringstream ss;
+		ss << "bullet";
+		ss << i;
+		ss << ".png";
+
+		CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(ss.str().c_str());
+		if (frame) {
+			CCSprite *sprite = CCSprite::spriteWithSpriteFrame(frame);
+			sprite->setAnchorPoint(ccp(.5f, .5f));
+			sprite->setPosition(ccp(getContentSize().width/2, getContentSize().height/2));
+			this->addChild(sprite);
+
+			sprite->runAction(CCRepeatForever::actionWithAction(
+					CCRotateBy::actionWithDuration(1.0f / i, 360)
+			));
+		}
+	}
+
+	return true;
+}
+
+
+
+bool Bullet::onCollission(GameObject *other) {
+
+	return false;
+}
+#ifndef BULLET_H
+#define BULLET_H
+
+#include "GameObject.h"
+
+
+class Bullet
+	: public GameObject
+{
+public:
+	Bullet();
+	virtual ~Bullet();
+
+	virtual bool initGameObject(const std::string &name);
+
+
+	virtual bool onCollission(GameObject *other);
+};
+
+
+#endif // BULLET_H

Classes/COLLECTABLE.CPP

 
 
 bool Collectable::initGameObject(const std::string &name) {
-	if (!GameObject::initGameObject(name)) {
+	if(!GameObject::initGameObject(name)) {
 		return false;
 	}
 
 
 
 
-void Collectable::onCollission(GameObject *other) {
+bool Collectable::onCollission(GameObject *other) {
 	Elephant *elephant = dynamic_cast<Elephant*>(other);
 
 	if (elephant) {
 			CCEaseElasticIn::actionWithAction(CCScaleTo::actionWithDuration(0.7f, 0.0f)),
 			CCCallFunc::actionWithTarget(this, callfunc_selector(GameObject::self_destruct))
 		));
+
+		return true;
 	}
 
-	return;
+	return false;
 }

Classes/COLLECTABLE.H

 	virtual bool initGameObject(const std::string &name);
 
 
-	virtual void onCollission(GameObject *other);
+	virtual bool onCollission(GameObject *other);
 };
 
 

Classes/ELEPHANT.CPP

 #include "Elephant.h"
+#include "Bullet.h"
+#include "GameScene.h"
 #include "cocos2d.h"
 
 using namespace cocos2d;
 
 Elephant::Elephant() {
 	this->speed = 100;
+	this->cooldown = 1.0f;
 	return;
 }
 
 
 
 void Elephant::onPositionChanged(CCPoint *position) {
-	CCSize winsize = CCDirector::sharedDirector()->getWinSize();
+	CCSize winsize = CCDirector::sharedDirector()->getWinSize();
 	CCSize size = this->getContentSize();
 	size.width  *= getScale();
 	size.height *= getScale();
 
 	return;
 }
+
+
+void Elephant::onUpdate(float dt) {
+	if((cooldown -= dt) <= 0.0f) {
+		cooldown = 0.5f;
+		
+		CCPoint position = this->getPosition();
+		position.y += 30;
+
+		Bullet *bullet = GameObject::create<Bullet>("bullet", gamescene);
+		bullet->setPosition(position);
+		bullet->setMovement(0, 1);
+
+		gamescene->addObject(bullet);
+	}
+
+	return;
+}

Classes/ELEPHANT.H

 
 
 	virtual void onPositionChanged(cocos2d::CCPoint *position);
+
+	virtual void onUpdate(float dt);
+
+private:
+	float cooldown;
 };
 
 

Classes/ENEMY.CPP

+#include "Enemy.h"
+#include "Bullet.h"
+#include "Elephant.h"
+#include "cocos2d.h"
+
+using namespace cocos2d;
+using namespace std;
+
+
+Enemy::Enemy() {
+	this->speed = 10 + (30 * rand() / RAND_MAX);
+	return;
+}
+
+Enemy::~Enemy() {
+	return;
+}
+
+
+bool Enemy::initGameObject(const std::string &name) {
+	if (!GameObject::initGameObject(name)) {
+		return false;
+	}
+
+	return true;
+}
+
+
+
+bool Enemy::onCollission(GameObject *other) {
+	Bullet *bullet = dynamic_cast<Bullet*>(other);
+	if (bullet) {
+		CCPoint target = this->getPosition();
+		target.y += 200;
+
+		runAction(CCSequence::actionOneTwo(
+					CCSpawn::actionOneTwo(
+							CCEaseBackIn::actionWithAction(CCMoveTo::actionWithDuration(1.0f, target)),
+							CCScaleTo::actionWithDuration(1.0f, 0.0f)
+					),
+					CCCallFunc::actionWithTarget(this, callfunc_selector(GameObject::self_destruct))
+		));
+
+		return true;
+	}
+
+	return false;
+}
+#ifndef ENEMY_H
+#define ENEMY_H
+
+#include "GameObject.h"
+
+
+class Enemy
+	: public GameObject
+{
+public:
+	Enemy();
+	virtual ~Enemy();
+
+	virtual bool initGameObject(const std::string &name);
+
+
+	virtual bool onCollission(GameObject *other);
+};
+
+
+#endif // ENEMY_H

Classes/GAMEOBJECT.CPP

 }
 
 
+void GameObject::onUpdate(float dt) {
+	return;
+}
+
+
 void GameObject::setMovement(float x, float y) {
 	this->vec_target_x = x;
 	this->vec_target_y = y;
 
 bool GameObject::doCollission(GameObject *other) {
 	if (CCRect::CCRectIntersectsRect(this->getCollisionRect(), other->getCollisionRect())) {
-		onCollission(other);
-		return true;
+		return onCollission(other);
 	}
 
 	return false;
 }
 
 
-void GameObject::onCollission(GameObject *other) {
-	return;
+bool GameObject::onCollission(GameObject *other) {
+	return false;
 }
 
 

Classes/GAMEOBJECT.H

 	virtual bool initGameObject(const std::string &name);
 
 	template <typename CLASS>
-	static CLASS *create(const std::string &name) {
+	static CLASS *create(const std::string &name, GameScene *gamescene) {
 		CLASS *object = new CLASS();
+		object->gamescene = gamescene;
 
 		if (object->initGameObject(name)) {
 			object->autorelease();
 	virtual void setMovement(float x, float y);
 	virtual void move(float dt, const cocos2d::CCRect &bounds);
 
+	virtual void onUpdate(float dt);
+
 	void self_destruct();
 
-
 protected:
 	virtual void onPositionChanged(cocos2d::CCPoint *position);
-	virtual void onCollission(GameObject *other);
+	virtual bool onCollission(GameObject *other);
 	
 protected:
 	GameScene *gamescene;

Classes/GAMESCENE.CPP

 
 #include "GameObject.h"
 #include "Collectable.h"
+#include "Enemy.h"
 
 #include <time.h>
 #include <algorithm>
 	CCSize winsize = CCDirector::sharedDirector()->getWinSize();
 
 	// create the player
-	this->elephant = GameObject::create<Elephant>("elephant");
+	this->elephant = GameObject::create<Elephant>("elephant", this);
 	assert(elephant);
 
 	if (this->elephant) {
 	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);
+	for(int i=objects.size(); --i>=0;) {
+		GameObject *obj = objects[i];
+		obj->onUpdate(dt);
+		obj->move(dt, bounds);
 	}
 
 	// check collissions
 	for(int i=objects.size(); --i>=0;) {
-		GameObject *obj = objects[i];
-		bool collission = false;
+		GameObject *other = objects[i];
 
-		if (obj != elephant) {
-			collission = obj->doCollission(elephant);
-		}
+		for(int j=objects.size(); --j>=0;) {
+			GameObject *obj = objects[j];
+			bool collission = obj->doCollission(other);
 
-		if (collission) {
-			ObjectList::iterator it = std::find(objects.begin(), objects.end(), obj);
-			if (it != objects.end()) {
-				objects.erase(it);
+			if (collission) {
+				ObjectList::iterator it = std::find(objects.begin(), objects.end(), obj);
+				if (it != objects.end()) {
+					objects.erase(it);
+				}
+
+				obj->release();
 			}
-
-			obj->release();
 		}
 	}
 
 		timeout_max *= 0.99f;
 
 		GameObject *spawn = (rand() % 20)
-			?	GameObject::create<Collectable>("peanut")
-			:	GameObject::create<Collectable>("peanut")
+			?	static_cast<GameObject*>(GameObject::create<Enemy>("enemy", this))
+			:	static_cast<GameObject*>(GameObject::create<Collectable>("peanut", this))
 		;
 
-		spawn->setPosition(ccp(
-				winsize.width  * (1.0f * rand() / RAND_MAX),
-				winsize.height
-		));
-		spawn->setMovement(0.0f, -1.0f);
+		if (spawn) {
+			spawn->setPosition(ccp(
+					winsize.width  * (1.0f * rand() / RAND_MAX),
+					winsize.height
+			));
+			spawn->setMovement(0.0f, -1.0f);
 
-		addObject(spawn);
+			addObject(spawn);
+		}
 	}
 
 	return;
Add a comment to this file

GRAPHICS/elephant/BULLET.XCF

Binary file added.

Add a comment to this file

GRAPHICS/elephant/BULLET1.PNG

Added
New image
Add a comment to this file

GRAPHICS/elephant/BULLET2.PNG

Added
New image
Add a comment to this file

GRAPHICS/elephant/BULLET3.PNG

Added
New image
Add a comment to this file

GRAPHICS/elephant/BULLET4.PNG

Added
New image
Add a comment to this file

GRAPHICS/elephant/BULLET_UP.PNG

Added
New image
Add a comment to this file

GRAPHICS/elephant/ENEMY_UP.PNG

Added
New image
Add a comment to this file

GRAPHICS/elephant/ENEMY_UP.XCF

Binary file added.

Resources/SPRITES.PLIST

     <dict>
         <key>frames</key>
         <dict>
+            <key>bullet1.png</key>
+            <dict>
+                <key>frame</key>
+                <string>{{2,2},{108,116}}</string>
+                <key>offset</key>
+                <string>{0,0}</string>
+                <key>sourceSize</key>
+                <string>{108,116}</string>
+            </dict>
+            <key>bullet2.png</key>
+            <dict>
+                <key>frame</key>
+                <string>{{112,2},{108,116}}</string>
+                <key>offset</key>
+                <string>{0,0}</string>
+                <key>sourceSize</key>
+                <string>{108,116}</string>
+            </dict>
+            <key>bullet3.png</key>
+            <dict>
+                <key>frame</key>
+                <string>{{222,2},{108,116}}</string>
+                <key>offset</key>
+                <string>{0,0}</string>
+                <key>sourceSize</key>
+                <string>{108,116}</string>
+            </dict>
+            <key>bullet4.png</key>
+            <dict>
+                <key>frame</key>
+                <string>{{332,2},{108,116}}</string>
+                <key>offset</key>
+                <string>{0,0}</string>
+                <key>sourceSize</key>
+                <string>{108,116}</string>
+            </dict>
+            <key>bullet_up.png</key>
+            <dict>
+                <key>frame</key>
+                <string>{{2,120},{108,116}}</string>
+                <key>offset</key>
+                <string>{0,0}</string>
+                <key>sourceSize</key>
+                <string>{108,116}</string>
+            </dict>
             <key>elephant_up.png</key>
             <dict>
                 <key>frame</key>
-                <string>{{2,2},{344,376}}</string>
+                <string>{{112,120},{344,376}}</string>
                 <key>offset</key>
                 <string>{0,0}</string>
                 <key>sourceSize</key>
                 <string>{344,376}</string>
             </dict>
+            <key>enemy_up.png</key>
+            <dict>
+                <key>frame</key>
+                <string>{{2,498},{263,256}}</string>
+                <key>offset</key>
+                <string>{0,0}</string>
+                <key>sourceSize</key>
+                <string>{263,256}</string>
+            </dict>
             <key>peanut_up.png</key>
             <dict>
                 <key>frame</key>
-                <string>{{2,380},{198,181}}</string>
+                <string>{{267,498},{198,181}}</string>
                 <key>offset</key>
                 <string>{0,0}</string>
                 <key>sourceSize</key>
             <key>size</key>
             <string>{512,1024}</string>
             <key>smartupdate</key>
-            <string>$TexturePacker:SmartUpdate:da8a64f8da8c3b0c9433e5680bb74b1d$</string>
+            <string>$TexturePacker:SmartUpdate:5cc9ab6823ea3b7651f87ca54d5adac2$</string>
             <key>textureFileName</key>
             <string>sprites.png</string>
         </dict>
Add a comment to this file

Resources/SPRITES.PNG

Old
Old image
New
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\Bullet.cpp" />
     <ClCompile Include="..\Classes\Collectable.cpp" />
     <ClCompile Include="..\Classes\Elephant.cpp" />
+    <ClCompile Include="..\Classes\Enemy.cpp" />
     <ClCompile Include="..\Classes\GameObject.cpp" />
     <ClCompile Include="..\Classes\GameScene.cpp" />
     <ClCompile Include="..\Classes\HelloWorldScene.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\Classes\AppDelegate.h" />
+    <ClInclude Include="..\Classes\Bullet.h" />
     <ClInclude Include="..\Classes\Collectable.h" />
     <ClInclude Include="..\Classes\Elephant.h" />
+    <ClInclude Include="..\Classes\Enemy.h" />
     <ClInclude Include="..\Classes\GameObject.h" />
     <ClInclude Include="..\Classes\GameScene.h" />
     <ClInclude Include="..\Classes\HelloWorldScene.h" />

win32/LuftKriegsElefant.win32.vcxproj.filters

     <ClCompile Include="..\Classes\Collectable.cpp">
       <Filter>Classes</Filter>
     </ClCompile>
+    <ClCompile Include="..\Classes\Enemy.cpp">
+      <Filter>Classes</Filter>
+    </ClCompile>
+    <ClCompile Include="..\Classes\Bullet.cpp">
+      <Filter>Classes</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="main.h">
     <ClInclude Include="..\Classes\Collectable.h">
       <Filter>Classes</Filter>
     </ClInclude>
+    <ClInclude Include="..\Classes\Enemy.h">
+      <Filter>Classes</Filter>
+    </ClInclude>
+    <ClInclude Include="..\Classes\Bullet.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.