forked from Open-GD/OpenGD
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PlayLayer and collisions (warning bad code)
- Loading branch information
Showing
10 changed files
with
946 additions
and
790 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,106 @@ | ||
#include "GameObject.h" | ||
#include <fmt/format.h> | ||
|
||
USING_NS_AX; | ||
|
||
bool GameObject::init(std::string_view frame) { | ||
// so ObjectManager is supposed to do some shit here | ||
// but there's not even any purpose for it to even exist | ||
|
||
// so this part is empty | ||
|
||
// avoid player lol | ||
if(frame.find("player") != std::string::npos) return true; | ||
if(!Sprite::initWithSpriteFrameName(frame)) return false; | ||
|
||
return true; | ||
} | ||
|
||
GameObject* GameObject::objectFromString(std::string str) { | ||
auto values = stringSetupToDict(str); | ||
|
||
if(values.count("1")) return nullptr; // you obviously can't do anything if there's no object ID | ||
|
||
int ID = stoi(values["1"]); | ||
auto frame = keyToFrame(ID); | ||
|
||
auto obj = createObject(frame); | ||
|
||
return obj; | ||
} | ||
|
||
GameObject* GameObject::createObject(std::string_view frame) { | ||
// if RingObject | ||
if(frame.find("ring_01_001.png") != std::string::npos) { | ||
return GameObject::create(frame); | ||
} | ||
else { | ||
return nullptr; | ||
} | ||
} | ||
|
||
GameObject* GameObject::create(std::string_view frame) { | ||
auto pRet = new(std::nothrow) GameObject(); | ||
|
||
if(pRet && pRet->init(frame)) { | ||
pRet->autorelease(); | ||
return pRet; | ||
} | ||
AX_SAFE_DELETE(pRet); | ||
return nullptr; | ||
} | ||
|
||
std::string GameObject::keyToFrame(int key) { | ||
return key > 0 && key < 8 ? fmt::format("square_{}_001.png", key) : ""; | ||
} | ||
|
||
// couldn't understand jack shit from the original | ||
// so I recreated from 2.1's GameToolbox::stringSetupToMap | ||
std::map <std::string, std::string> GameObject::stringSetupToDict(std::string str) { | ||
size_t index = 0; | ||
size_t length = str.length(); | ||
|
||
std::map<std::string, std::string> output; | ||
|
||
size_t currentComma = str.find(','); | ||
while (true) | ||
{ | ||
auto key = str.substr(index, currentComma - index); | ||
|
||
if(currentComma == str.npos) break; | ||
|
||
// find new comma | ||
index = currentComma + 1; | ||
currentComma = str.find(',', index); | ||
|
||
// set values | ||
output.insert(std::make_pair(key, str.substr(index, currentComma - index))); | ||
|
||
if(currentComma == str.npos) break; | ||
|
||
// find new comma (again) | ||
index = currentComma + 1; | ||
currentComma = str.find(',', index); | ||
} | ||
|
||
return output; | ||
#include "GameObject.h" | ||
#include <fmt/format.h> | ||
|
||
USING_NS_AX; | ||
|
||
bool GameObject::init(std::string_view frame) { | ||
// so ObjectManager is supposed to do some shit here | ||
// but there's not even any purpose for it to even exist | ||
|
||
// so this part is empty | ||
|
||
// avoid player lol | ||
if (frame.find("player") != std::string::npos) | ||
{ | ||
_pObjectType = GameObjectType::kObjectTypePlayer; | ||
return true; | ||
} | ||
|
||
if(!Sprite::initWithSpriteFrameName(frame)) return false; | ||
|
||
|
||
if (frame.find("d_spikes_") != std::string::npos) | ||
{ | ||
_pObjectType = GameObjectType::kObjectTypeTile; | ||
} | ||
else if (frame.find("square_") != std::string::npos) | ||
{ | ||
_pObjectType = GameObjectType::kObjectTypeTile; | ||
} | ||
|
||
|
||
_pOuterBounds = Rect(); | ||
_pInnerBounds = Rect(); | ||
|
||
return true; | ||
} | ||
|
||
GameObject* GameObject::objectFromString(std::string str) { | ||
auto values = stringSetupToDict(str); | ||
|
||
if(values.count("1")) return nullptr; // you obviously can't do anything if there's no object ID | ||
|
||
int ID = stoi(values["1"]); | ||
auto frame = keyToFrame(ID); | ||
|
||
auto obj = createObject(frame); | ||
|
||
return obj; | ||
} | ||
|
||
GameObject* GameObject::createObject(std::string_view frame) { | ||
// if RingObject | ||
if(frame.find("ring_01_001.png") != std::string::npos) { | ||
return GameObject::create(frame); | ||
} | ||
else { | ||
return nullptr; | ||
} | ||
} | ||
|
||
GameObject* GameObject::create(std::string_view frame) { | ||
auto pRet = new(std::nothrow) GameObject(); | ||
|
||
if(pRet && pRet->init(frame)) { | ||
pRet->autorelease(); | ||
return pRet; | ||
} | ||
AX_SAFE_DELETE(pRet); | ||
return nullptr; | ||
} | ||
|
||
std::string GameObject::keyToFrame(int key) { | ||
return key > 0 && key < 8 ? fmt::format("square_{}_001.png", key) : ""; | ||
} | ||
|
||
// couldn't understand jack shit from the original | ||
// so I recreated from 2.1's GameToolbox::stringSetupToMap | ||
std::map <std::string, std::string> GameObject::stringSetupToDict(std::string str) { | ||
size_t index = 0; | ||
size_t length = str.length(); | ||
|
||
std::map<std::string, std::string> output; | ||
|
||
size_t currentComma = str.find(','); | ||
while (true) | ||
{ | ||
auto key = str.substr(index, currentComma - index); | ||
|
||
if(currentComma == str.npos) break; | ||
|
||
// find new comma | ||
index = currentComma + 1; | ||
currentComma = str.find(',', index); | ||
|
||
// set values | ||
output.insert(std::make_pair(key, str.substr(index, currentComma - index))); | ||
|
||
if(currentComma == str.npos) break; | ||
|
||
// find new comma (again) | ||
index = currentComma + 1; | ||
currentComma = str.find(',', index); | ||
} | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,36 @@ | ||
#pragma once | ||
#include <axmol.h> | ||
#include <map> | ||
#include <string> | ||
|
||
class GameObject : public ax::Sprite { | ||
public: | ||
static GameObject* create(std::string_view frame); | ||
static GameObject* createObject(std::string_view frame); | ||
static GameObject* objectFromString(std::string); | ||
bool init(std::string_view frame); | ||
|
||
static std::string keyToFrame(int key); | ||
static std::map<std::string, std::string> stringSetupToDict(std::string); | ||
#pragma once | ||
#include <axmol.h> | ||
#include <map> | ||
#include <string> | ||
|
||
enum GameObjectType { | ||
kObjectTypePlayer = 0, | ||
kObjectTypeTile = 1, | ||
kObjectTypeHazard = 2 | ||
}; | ||
|
||
class GameObject : public ax::Sprite { | ||
private: | ||
ax::Rect _pOuterBounds; | ||
ax::Rect _pInnerBounds; | ||
|
||
GameObjectType _pObjectType; | ||
|
||
public: | ||
static GameObject* create(std::string_view frame); | ||
static GameObject* createObject(std::string_view frame); | ||
static GameObject* objectFromString(std::string); | ||
bool init(std::string_view frame); | ||
|
||
static std::string keyToFrame(int key); | ||
static std::map<std::string, std::string> stringSetupToDict(std::string); | ||
|
||
void setOuterBounds(const ax::Rect& value) { _pOuterBounds = value; } | ||
ax::Rect getOuterBounds() { return _pOuterBounds; } | ||
|
||
void setInnerBounds(const ax::Rect& value) { _pInnerBounds = value; } | ||
ax::Rect getInnerBounds() { return _pInnerBounds; } | ||
|
||
GameObjectType getGameObjectType() { return _pObjectType; } | ||
void setGameObjectType(const GameObjectType& value) { _pObjectType = value; } | ||
}; |
Oops, something went wrong.