Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
wdnmd实例化后的所有对象共享一个内部static变量
Browse files Browse the repository at this point in the history
wdnmd这二分怎么还能比O(n)的遍历还慢
f**k性能问题没法解决
f**k我做的是伪oop
  • Loading branch information
Wider committed Apr 12, 2020
1 parent 81288db commit 39e92f3
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 49 deletions.
37 changes: 20 additions & 17 deletions BaseObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,20 @@ void Figure::update(int x, int y) {
for(auto& i : figures)
{
if(i.tigger()) {
using namespace std::chrono;
static auto time_tick = steady_clock::now();
static size_t cnt = 0;
if(_status != i.name) {
time_tick = steady_clock::now();
cnt = 0;
figure_cnt = 0;
_status = i.name;
}
putimage(x, y, &i.masks[i.images.size() - cnt - 1], NOTSRCERASE);
putimage(x, y, &i.images[i.images.size() - cnt - 1], SRCINVERT);
//cout << figure_cnt << endl;
//没人发现我写的bug
putimage(x, y, &i.masks[figure_cnt % i.images.size()], NOTSRCERASE);
putimage(x, y, &i.images[figure_cnt % i.images.size()], SRCINVERT);
if((steady_clock::now() - time_tick) >= milliseconds(100)) {
cnt = (cnt + 1) % i.images.size();
figure_cnt = (figure_cnt + 1) % i.images.size();
time_tick = steady_clock::now();
}

return;
}
}
}
Expand Down Expand Up @@ -122,7 +121,7 @@ void BaseObject::update(double time) {
void BaseObject::show(Vector& offset) {
figure.update(static_cast<int>(round(position.x()) + offset.x()), static_cast<int>(round(position.y()) + offset.y()));
}
bool BaseObject::block_crash(BaseObject& t) {
bool BaseObject::block_crash(BaseObject& t,bool left_exist_object,bool right_exist_object) {
//if(
// position.x() > t.position.x() - figure.width() && position.x() < t.position.x() + t.figure.width()
// &&
Expand Down Expand Up @@ -156,20 +155,24 @@ bool BaseObject::block_crash(BaseObject& t) {
//左碰撞
if(px + width() >= tpx && px < tpx &&
py + height() >= tpy && py <= tpy + t.height()) {
if(velocity.x() > 0) {
velocity.x(0);
position.x((double)tpx - width());
if(!left_exist_object) {
if(velocity.x() > 0) {
velocity.x(0);
position.x((double)tpx - width());
}
return true;
}
return true;
}
//右碰撞
if(px + width() > tpx + t.width()&& px <= tpx + t.width() &&
py + height() >= tpy && py <= tpy + t.height()) {
if(velocity.x() < 0) {
velocity.x(0);
position.x((double)tpx + t.width());
if(!right_exist_object) {
if(velocity.x() < 0) {
velocity.x(0);
position.x((double)tpx + t.width());
}
return true;
}
return true;
}
return false;
}
Expand Down
8 changes: 5 additions & 3 deletions BaseObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ struct FigureData {
vector<IMAGE> masks;
std::function<bool(void)> tigger;
};

using namespace std::chrono;
class Figure {
private:
vector<FigureData> figures;
int figure_cnt = 0;
steady_clock::time_point time_tick = steady_clock::now();
int _width;
int _height;
string _status;
public:
Figure():figures(),_width(0),_height(0),_status("default") {}
Figure():figures(), figure_cnt(0),_width(0),_height(0),_status("default") {}
void addFigure(string name, vector<IMAGE> imgs, vector<IMAGE> masks, function<bool(void)> tigger);
void update(int x, int y);
void turn();
Expand Down Expand Up @@ -94,7 +96,7 @@ class BaseObject
void height(int h);
void update(double time);
void show(Vector& offset);
bool block_crash(BaseObject& t);
bool block_crash(BaseObject& t, bool left_exist_object, bool right_exist_object);
bool pxiel_crash();
};

Expand Down
4 changes: 2 additions & 2 deletions Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

#define WINDOWS_WIDTH 680
#define WINDOWS_HEIGHT 490
#define WINDOWS_WIDTH 700
#define WINDOWS_HEIGHT 504

#define DEBUG
#ifdef DEBUG
Expand Down
109 changes: 104 additions & 5 deletions Map.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
#include "Map.h"

#include <algorithm>

void Map::init(BaseObject* h)
{
hero = h;

loadimage(&background, _T("assets\\images\\map1.png"));
_width = background.getwidth();
_height = background.getheight();
int unit_length = 35;
int unit_length = 36;
//单元格为35*35的方格,全地图横向228个单元,纵向14个单元
//注意坐标是从0开始的
//测试用
rocket.push_back(Rocket(0 * unit_length, 10 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(0 * unit_length, 11 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(2 * unit_length, 10 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(2 * unit_length, 11 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));

//铺垫底层基石
rocket.push_back(Rocket(-1 * unit_length, 0 * unit_length, 1 * unit_length, 14 * unit_length));
rocket.push_back(Rocket(228 * unit_length, 0 * unit_length, 1 * unit_length, 14 * unit_length));
rocket.push_back(Rocket(0 * unit_length, 12 * unit_length, 69 * unit_length, 2 * unit_length));
rocket.push_back(Rocket(71 * unit_length, 12 * unit_length, 15 * unit_length, 2 * unit_length));
rocket.push_back(Rocket(89 * unit_length, 12 * unit_length, 64 * unit_length, 2 * unit_length));
rocket.push_back(Rocket(155 * unit_length, 12 * unit_length, 73 * unit_length, 2 * unit_length));
rocket.push_back(Rocket(0 * unit_length, 14 * unit_length, 227 * unit_length, 1 * unit_length));

//铺垫默认山丘
for(int i=0;i<4;i++)
rocket.push_back(Rocket((134+i) * unit_length, (11-i) * unit_length, 1 * unit_length, (i+1) * unit_length));
for(int i = 0; i < 4; i++)
Expand All @@ -27,12 +38,56 @@ void Map::init(BaseObject* h)
for(int i = 0; i < 8; i++)
rocket.push_back(Rocket((181 + i) * unit_length, (11 - i) * unit_length, (9 - i) * unit_length, 1 * unit_length));
rocket.push_back(Rocket(212 * unit_length, 11 * unit_length, 1 * unit_length, 1 * unit_length));
hero = h;

//铺垫漂浮岩石
rocket.push_back(Rocket(16 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
for(int i = 0; i < 5; i++)
rocket.push_back(Rocket((20 + i) * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(22 * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
for(int i = 0; i < 3; i++)
rocket.push_back(Rocket((77 + i) * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
for(int i = 0; i < 8; i++)
rocket.push_back(Rocket((80 + i) * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
for(int i = 0; i < 4; i++)
rocket.push_back(Rocket((91 + i) * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(94 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(100 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(106 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(109 * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(109 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(112 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
rocket.push_back(Rocket(117 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
for(int i = 0; i < 3; i++)
rocket.push_back(Rocket((120 + i) * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
for(int i = 0; i < 4; i++)
rocket.push_back(Rocket((127 + i) * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
for(int i = 0; i < 2; i++)
rocket.push_back(Rocket((128 + i) * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));
for(int i = 0; i < 4; i++)
rocket.push_back(Rocket((168 + i) * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1"));

//rocket_sorted_by_left = rocket;
//rocket_sorted_by_right = rocket;
//sort(rocket_sorted_by_left.begin(), rocket_sorted_by_left.end(), [](Rocket &a,Rocket &b) {
// if(a.position.x() < b.position.x()) return true;
// if(a.position.x() == b.position.x() && a.position.y() < b.position.y()) return true;
// return false;
//});
//sort(rocket_sorted_by_right.begin(), rocket_sorted_by_right.end(), [](Rocket& a, Rocket& b) {
// if(a.position.x() + a.width() -1 > b.position.x() + b.width() - 1) return true;
// if(a.position.x() + a.width() - 1 == b.position.x() + b.width() - 1 && a.position.y() + a.height() -1 < b.position.y() + b.height() - 1) return true;
// return false;
//});
for(auto &i : rocket) i.init();

}

void Map::show(Vector offset)
{
putimage(0 + static_cast<int>(offset.x()), 0 + static_cast<int>(offset.y()), &background);
for(auto &i: rocket)
if(i.type()!="rock0")
i.show(offset);
}
int Map::width() {
return _width;
Expand All @@ -45,9 +100,53 @@ void Map::update()
check_crash();
}

bool Map::left_exist_object(BaseObject &t)
{
for(auto& i : rocket)
if(t.position.x() == i.position.x() + i.width() && t.position.y() == i.position.y())
return true;
return false;
}

bool Map::right_exist_object(BaseObject& t)
{
for(auto& i : rocket)
if(t.position.x() + t.width() == i.position.x() && t.position.y() == i.position.y())
return true;
return false;
}

void Map::check_crash()
{
for(auto& i : rocket) {
hero->block_crash(i);
for(auto &i : rocket) {
if(i.position.x() > hero->position.x() + hero->width() || i.position.x() + i.width() < hero->position.x())
continue;
hero->block_crash(i, left_exist_object(i), right_exist_object(i));
}

//vector<Rocket>:: reverse_iterator l;
//vector<Rocket>::iterator r;
//r = lower_bound(rocket_sorted_by_left.begin(), rocket_sorted_by_left.end(), *hero, [](BaseObject mid, BaseObject val) {
// return mid.position.x() < val.position.x() + val.width() -1;
//});
//l = upper_bound(rocket_sorted_by_right.rbegin(), rocket_sorted_by_right.rend(), *hero, [](BaseObject val, BaseObject mid) {
// return val.position.x() < mid.position.x() + mid.width() -1;
//});
//
//static auto time_tick = steady_clock::now();
//if((steady_clock::now() - time_tick) >= milliseconds(500)) {
// time_tick = steady_clock::now();
// for(vector<Rocket>::iterator i = l.base()--; i!= rocket_sorted_by_right.end() ; i++) {
// if((*i).position.x() != (*l.base()--).position.x()) break;
// hero->block_crash(i);
// //cout << (*i).position.y() << endl;
// }
// for(auto i = r; i != rocket_sorted_by_left.end(); i++) {
// if((*i).position.x() != (*r).position.x()) break;
// hero->block_crash(*i);
// //cout << (*i).position.y() << endl;
// }
// //cout << (*l.base()--).position.x() + (*l.base()--).width() - 1 << ' ' << (*(r)).position.x() << endl;
//}

}
4 changes: 4 additions & 0 deletions Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Map
private:
IMAGE background;
vector<Rocket> rocket;
//vector<Rocket> rocket_sorted_by_left;
//vector<Rocket> rocket_sorted_by_right;
BaseObject* hero;
int _width;
int _height;
Expand All @@ -23,6 +25,8 @@ class Map
int width();
int height();
void update();
bool left_exist_object(BaseObject& t);
bool right_exist_object(BaseObject& t);
void check_crash();
};

Expand Down
30 changes: 15 additions & 15 deletions Mario.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,39 @@ void Mario::init() {
loadimage(&organ_mask, mask_src);
int width = origin.getwidth() / figure_num;
int height = origin.getheight();

this->width(width);
this->height(height);
vector<IMAGE> imgs, masks;
IMAGE temp;

for(int i = 0; i < 3; i++) {
SetWorkingImage(&origin);
getimage(&temp, width + width * i, 0, width, height);
imgs.push_back(temp);
SetWorkingImage(&organ_mask);
getimage(&temp, width + width * i, 0, width, height);
masks.push_back(temp);
}
figure.addFigure("running", imgs, masks, [this]()->bool {
if(velocity.x() != 0 && velocity.y() == 0) return true;
return false;
});

imgs.clear(); masks.clear();
SetWorkingImage(&origin);
getimage(&temp, 0, 0, width, height);
imgs.push_back(temp);
SetWorkingImage(&organ_mask);
getimage(&temp, 0, 0, width, height);
masks.push_back(temp);
figure.addFigure("still", imgs, masks, [this]()->bool {
if(velocity.x() == 0 && velocity.y() == 0) return true;
if(velocity.x() == 0 && velocity.y() == 0) return true;
return false;
});
figure.addFigure("falling", imgs, masks, [this]()->bool {
if(velocity.y() > 0) return true;
return false;
});
imgs.clear(); masks.clear();
for(int i = 0; i < 3; i++) {
SetWorkingImage(&origin);
getimage(&temp, width + width * i, 0, width, height);
imgs.push_back(temp);
SetWorkingImage(&organ_mask);
getimage(&temp, width + width * i, 0, width, height);
masks.push_back(temp);
}
figure.addFigure("running", imgs, masks, [this]()->bool {
if(velocity.x() != 0 && velocity.y() == 0) return true;
return false;
});

imgs.clear(); masks.clear();
SetWorkingImage(&origin);
Expand Down
2 changes: 1 addition & 1 deletion Mario.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define _MARIO_H_

#define NORMAL_RUN_SPEED 0.3
#define NORMAL_JUMP_SPEED -1.2
#define NORMAL_JUMP_SPEED -1.1

#include<graphics.h>
#include "BaseObject.h"
Expand Down
Loading

0 comments on commit 39e92f3

Please sign in to comment.