Dependent Classes Example
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
a7540c87d1
commit
882ccac3fb
Binary file not shown.
6
Map.cpp
Normal file
6
Map.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "Map.h"
|
||||
|
||||
void Map::test(){
|
||||
printf("Hello Map\n");
|
||||
}
|
||||
|
10
Map.h
Normal file
10
Map.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
class Player;
|
||||
|
||||
class Map{
|
||||
public:
|
||||
Player*p;
|
||||
void test();
|
||||
};
|
6
Player.cpp
Normal file
6
Player.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "Player.h"
|
||||
|
||||
void Player::test(){
|
||||
printf("Hello Player\n");
|
||||
}
|
||||
|
10
Player.h
Normal file
10
Player.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
class Map;
|
||||
|
||||
class Player{
|
||||
public:
|
||||
Map*map;
|
||||
void test();
|
||||
};
|
BIN
dependentClasses.zip
Normal file
BIN
dependentClasses.zip
Normal file
Binary file not shown.
120
main.cpp
120
main.cpp
@ -1,112 +1,14 @@
|
||||
#define OLC_PGE_APPLICATION
|
||||
#include "pixelGameEngine.h"
|
||||
#include "Map.h"
|
||||
#include "Player.h"
|
||||
|
||||
class CustomControls : public olc::PixelGameEngine
|
||||
{
|
||||
public:
|
||||
CustomControls()
|
||||
{
|
||||
sAppName = "Configuring Custom Controls";
|
||||
}
|
||||
int main() {
|
||||
Map newMap;
|
||||
Player newPlayer;
|
||||
newMap.p = &newPlayer;
|
||||
newMap.test();
|
||||
newPlayer.map = &newMap;
|
||||
newPlayer.test();
|
||||
|
||||
private:
|
||||
|
||||
olc::Key MOVE_UP=olc::W;
|
||||
olc::Key MOVE_DOWN=olc::S;
|
||||
olc::Key MOVE_RIGHT=olc::D;
|
||||
olc::Key MOVE_LEFT=olc::A;
|
||||
|
||||
olc::vd2d pos = {0,0};
|
||||
|
||||
std::array<std::string,4> keysList = {"UP","RIGHT","DOWN","LEFT"};
|
||||
int configuringKeyIndex = 0;
|
||||
|
||||
public:
|
||||
|
||||
bool OnUserCreate() override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OnUserUpdate(float fElapsedTime) override
|
||||
{
|
||||
if (GetKey(olc::F1).bPressed) {
|
||||
TextEntryEnable(true);
|
||||
configuringKeyIndex=0;
|
||||
}
|
||||
|
||||
if (TextEntryGetString().size()>0) {
|
||||
char c = TextEntryGetString()[0];
|
||||
olc::Key customKey;
|
||||
if (c>='a'&&c<='z') {
|
||||
c-=32; //Capitalize the letter.
|
||||
customKey=(olc::Key)(c-'A'+1);
|
||||
} else
|
||||
if (c>='A'&&c<='Z') {
|
||||
customKey=(olc::Key)(c-'A'+1);
|
||||
} else
|
||||
if (c>='0'&&c<='9') {
|
||||
customKey=(olc::Key)(c-'0'+olc::Key::K0);
|
||||
} else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (configuringKeyIndex) {
|
||||
case 0:{
|
||||
MOVE_UP=customKey;
|
||||
}break;
|
||||
case 1:{
|
||||
MOVE_RIGHT=customKey;
|
||||
}break;
|
||||
case 2:{
|
||||
MOVE_DOWN=customKey;
|
||||
}break;
|
||||
case 3:{
|
||||
MOVE_LEFT=customKey;
|
||||
}break;
|
||||
}
|
||||
|
||||
configuringKeyIndex++;
|
||||
if (configuringKeyIndex<keysList.size()) {
|
||||
TextEntryEnable(true);
|
||||
} else {
|
||||
TextEntryEnable(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (GetKey(MOVE_UP).bHeld) {
|
||||
pos+={0,-5*fElapsedTime};
|
||||
}
|
||||
if (GetKey(MOVE_DOWN).bHeld) {
|
||||
pos+={0,5*fElapsedTime};
|
||||
}
|
||||
if (GetKey(MOVE_RIGHT).bHeld) {
|
||||
pos+={5*fElapsedTime,0};
|
||||
}
|
||||
if (GetKey(MOVE_LEFT).bHeld) {
|
||||
pos+={-5*fElapsedTime,0};
|
||||
}
|
||||
|
||||
|
||||
Clear(olc::VERY_DARK_BLUE);
|
||||
|
||||
DrawRect(pos,{32,32},olc::WHITE);
|
||||
|
||||
if (IsTextEntryEnabled()) {
|
||||
DrawString({0,0},"Please press a key for "+keysList[configuringKeyIndex]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
CustomControls game;
|
||||
if (game.Construct(256, 240, 4, 4))
|
||||
game.Start();
|
||||
|
||||
return 0;
|
||||
newPlayer.map->test();
|
||||
newMap.p->test();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user