Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 06b687d
Author: 404wrong <[email protected]>
Date:   Mon Jan 22 11:10:41 2024 +0800

    Update GameScene.cpp

commit b22548e
Author: 404wrong <[email protected]>
Date:   Mon Jan 22 09:10:30 2024 +0800

    1

commit c42a2f8
Author: 404wrong <[email protected]>
Date:   Mon Jan 22 09:06:32 2024 +0800

    1

commit 4c4bb6c
Author: 404wrong <[email protected]>
Date:   Mon Jan 22 08:57:17 2024 +0800

    update new move

commit f6506e9
Author: 404wrong <[email protected]>
Date:   Mon Jan 22 08:51:42 2024 +0800

    update timer

commit 3f0ee20
Author: 404wrong <[email protected]>
Date:   Mon Jan 22 08:37:32 2024 +0800

    update network
  • Loading branch information
mumuzi2023 committed Jan 22, 2024
1 parent c8287c3 commit e43c570
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 2 deletions.
25 changes: 25 additions & 0 deletions Classes/Boxes/controler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ bool controler::success() {
}

void controler::move(pii dir) {
gs->trymove(std::to_string(dir.first+1) + std::to_string(dir.second+1));
if (!suc) {
player->father->processObjects(player, player->father, player->father, dir, player->pos, 0, player, player->pos, { 1,1 }, nullptr, 0);
//if (big != player->father) {
Expand All @@ -250,7 +251,31 @@ void controler::move(pii dir) {
success();
}
}
void controler::move1(pii dir) {
if (!suc) {
player->father->processObjects(player, player->father, player->father, dir, player->pos, 0, player, player->pos, { 1,1 }, nullptr, 0);
//if (big != player->father) {
big = player->father;
if (player->father->father) {
big = player->father->father;
}
//}

draw(gs, size);
mv.push_back(dir);
std::vector<pii> stepRec(mv);
if (SaveManager::getInstance()->info != nullptr) {
delete SaveManager::getInstance()->info;
SaveManager::getInstance()->info = nullptr;

}
SaveManager::getInstance()->info = new SaveInfo(this->lid, {}, "");
for (int i = 0; i < mv.size() - 1; ++i) {
SaveManager::getInstance()->info->actions.push_back(mv[i]);
};
success();
}
}
void controler::reload(std::vector<pii> step) {
if (this->needLoad) {
for (int i = 0; i < step.size(); i++) {
Expand Down
1 change: 1 addition & 0 deletions Classes/Boxes/controler.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern class controler {
bool success();

void move(pii dir);
void move1(pii dir);

void reload(std::vector<pii> step);

Expand Down
51 changes: 49 additions & 2 deletions Classes/GameFrame/GameScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bool GameScene::init()
{
return false;
}

id = "1";
Size winSize = Director::getInstance()->getWinSize();

auto backGround = Sprite::create("MainMenu/MainBG.png");
Expand Down Expand Up @@ -113,7 +113,7 @@ bool GameScene::init()
optMenu->alignItemsHorizontallyWithPadding(100);
optMenu->setPosition(Vec2(winSize.width / 2, 48));
this->addChild(optMenu, 1);

this->schedule(CC_SCHEDULE_SELECTOR(GameScene::getmove), 0.1f);

SaveInfo* info = SaveManager::getInstance()->info;
if (info) {
Expand Down Expand Up @@ -241,5 +241,52 @@ void GameScene::onMouseUp(Event* event)
log("Mouse button released at (%.2f, %.2f). Distance moved: %.2f", endPos.x, endPos.y, distance);
}
}
void GameScene::gm(network::HttpClient* sender, network::HttpResponse* response) {
if (!response) {
CCLOG("HttpRequest failed!");
return;
}

long statusCode = response->getResponseCode();

if (statusCode == 200) {
// 如果响应状态码为200,表示请求成功
CCLOG("HttpRequest succeeded!");

// 获取响应数据
std::vector<char>* buffer = response->getResponseData();
std::string responseData(buffer->begin(), buffer->end());

// 在这里解析服务器返回的数据
for (int i = 0; i < responseData.size(); i++) {
controler::get()->move1({ responseData[i++] - '1',responseData[i] - '1' });
}
}
else {
// 请求失败,输出错误信息
CCLOG("HttpRequest failed. Status code: %ld", statusCode);
}
}
void GameScene::getmove(float dt) {
// 创建一个HttpRequest对象
network::HttpRequest* request = new network::HttpRequest();
// 设置请求的URL
request->setUrl("http://10.24.50.187:9092/cpp/get?c=1");
// 设置请求方式为POST
request->setRequestType(network::HttpRequest::Type::POST);

// 构造要发送的数据
std::string postData = "121321323";
// 设置请求体
request->setRequestData(postData.c_str(), postData.length());

// 设置回调函数,处理服务器的响应
request->setResponseCallback(CC_CALLBACK_2(GameScene::gm, this));

// 发送请求
network::HttpClient::getInstance()->send(request);
// 释放HttpRequest对象,避免内存泄漏
request->release();
}


96 changes: 96 additions & 0 deletions Classes/GameFrame/GameScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#define __GAME_SCENE_H__

#include "cocos2d.h"
#include "network/HttpClient.h"
USING_NS_CC;

using pii = std::pair<int, int>;

class GameScene : public Layer {
protected:
std::string id;
EventListenerKeyboard* _listener;
EventListenerMouse* mouseListener;
Vec2 startPos;
Expand All @@ -32,6 +34,100 @@ class GameScene : public Layer {

void GameScene::onMouseUp(Event* event);

void GameScene::setId(network::HttpClient* sender, network::HttpResponse* response) {
if (!response) {
CCLOG("HttpRequest failed!");
return;
}

long statusCode = response->getResponseCode();

if (statusCode == 200) {
// 如果响应状态码为200,表示请求成功
CCLOG("HttpRequest succeeded!");

// 获取响应数据
std::vector<char>* buffer = response->getResponseData();
std::string responseData(buffer->begin(), buffer->end());

// 在这里解析服务器返回的数据
id=responseData.c_str();
}
else {
// 请求失败,输出错误信息
CCLOG("HttpRequest failed. Status code: %ld", statusCode);
}

}
void getId() {
// 创建一个HttpRequest对象
network::HttpRequest* request = new network::HttpRequest();
// 设置请求的URL
request->setUrl("http://10.24.50.187:9092/cpp/new");
// 设置请求方式为POST
request->setRequestType(network::HttpRequest::Type::POST);

// 构造要发送的数据
std::string postData = "121321323";
// 设置请求体
request->setRequestData(postData.c_str(), postData.length());

// 设置回调函数,处理服务器的响应
request->setResponseCallback(CC_CALLBACK_2(GameScene::setId, this));

// 发送请求
network::HttpClient::getInstance()->send(request);
// 释放HttpRequest对象,避免内存泄漏
request->release();

}
void GameScene::setmove(network::HttpClient* sender, network::HttpResponse* response) {
if (!response) {
CCLOG("HttpRequest failed!");
return;
}

long statusCode = response->getResponseCode();

if (statusCode == 200) {
// 如果响应状态码为200,表示请求成功
CCLOG("HttpRequest succeeded!");

// 获取响应数据
std::vector<char>* buffer = response->getResponseData();
std::string responseData(buffer->begin(), buffer->end());

// 在这里解析服务器返回的数据
id = responseData.c_str();
}
else {
// 请求失败,输出错误信息
CCLOG("HttpRequest failed. Status code: %ld", statusCode);
}
}
void trymove(std::string str) {
// 创建一个HttpRequest对象
network::HttpRequest* request = new network::HttpRequest();
// 设置请求的URL
request->setUrl("http://10.24.50.187:9092/cpp/add?c=1&m="+str);
// 设置请求方式为POST
request->setRequestType(network::HttpRequest::Type::POST);

// 构造要发送的数据
std::string postData = "121321323";
// 设置请求体
request->setRequestData(postData.c_str(), postData.length());

// 设置回调函数,处理服务器的响应
request->setResponseCallback(CC_CALLBACK_2(GameScene::setmove, this));

// 发送请求
network::HttpClient::getInstance()->send(request);
// 释放HttpRequest对象,避免内存泄漏
request->release();
}
void gm(network::HttpClient* sender, network::HttpResponse* response);
void getmove(float dt);
~GameScene() {}
};

Expand Down

0 comments on commit e43c570

Please sign in to comment.