This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
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.
- Loading branch information
Wider
committed
Apr 10, 2020
1 parent
2030f2d
commit 9702060
Showing
13 changed files
with
477 additions
and
266 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 |
---|---|---|
@@ -0,0 +1,180 @@ | ||
#include "BaseObject.h" | ||
|
||
Vector Vector::operator = (const Vector& rhs) { | ||
_x = rhs._x; | ||
_y = rhs._y; | ||
return *this; | ||
} | ||
Vector Vector::operator + (const Vector& rhs) const { | ||
return Vector(_x + rhs._x, _y + rhs._y); | ||
} | ||
Vector Vector::operator - (const Vector& rhs) const { | ||
return Vector(_x - rhs._x, _y - rhs._y); | ||
} | ||
Vector Vector::operator * (const double rhs) const { | ||
return Vector(_x * rhs, _y * rhs); | ||
} | ||
Vector Vector::operator / (const double rhs) const { | ||
return Vector(_x / rhs, _y / rhs); | ||
} | ||
Vector Vector::operator += (const Vector& rhs) { | ||
*this = *this + rhs; | ||
return *this; | ||
} | ||
Vector Vector::operator -= (const Vector& rhs) { | ||
*this = *this - rhs; | ||
return *this; | ||
} | ||
Vector Vector::operator *= (const double rhs) { | ||
*this = *this * rhs; | ||
return *this; | ||
} | ||
Vector Vector::operator /= (const double rhs) { | ||
*this = *this / rhs; | ||
return *this; | ||
} | ||
bool Vector::operator == (const Vector& rhs) const { | ||
if(_x == rhs._x && _y == rhs._y) return true; | ||
else return false; | ||
} | ||
bool Vector::operator == (const double rhs) const { | ||
if(_x == rhs && _y == rhs) return true; | ||
else return false; | ||
} | ||
bool Vector::operator != (const Vector& rhs) const { | ||
if(*this == rhs) return false; | ||
else return true; | ||
} | ||
double Vector::x() { return (_x); } | ||
double Vector::y() { return (_y); } | ||
void Vector::x(double x) { _x = x; } | ||
void Vector::y(double y) { _y = y; } | ||
void Vector::set(double x, double y) { _x = x, _y = y; } | ||
|
||
void Figure::addFigure(string name, vector<IMAGE> imgs, vector<IMAGE> masks, function<bool(void)> tigger) { | ||
if(figures.empty() && !imgs.empty()) { | ||
_width = imgs.front().getwidth(); | ||
_height = imgs.front().getheight(); | ||
} | ||
figures.push_back(FigureData{name, imgs, masks, tigger}); | ||
} | ||
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; | ||
_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); | ||
if((steady_clock::now() - time_tick) >= milliseconds(100)) { | ||
cnt = (cnt + 1) % i.images.size(); | ||
time_tick = steady_clock::now(); | ||
} | ||
|
||
} | ||
} | ||
} | ||
void Figure::turn() { | ||
for(auto& i : figures) | ||
{ | ||
for(auto& j : i.images) rotateFlip(&j); | ||
for(auto& j : i.masks) rotateFlip(&j); | ||
} | ||
} | ||
string Figure::status() | ||
{ | ||
return _status; | ||
} | ||
void Figure::status(string s) | ||
{ | ||
_status = s; | ||
} | ||
int Figure::width() { | ||
return _width; | ||
} | ||
int Figure::height() { | ||
return _height; | ||
} | ||
|
||
int BaseObject::width() { | ||
return _width; | ||
} | ||
int BaseObject::height() { | ||
return _height; | ||
} | ||
void BaseObject::width(int w) { | ||
_width = w; | ||
} | ||
void BaseObject::height(int h) { | ||
_height = h; | ||
} | ||
void BaseObject::update(double time) { | ||
position += (velocity * time + acceleration * time * time / 2); | ||
//position += velocity * time; | ||
velocity += acceleration * time; | ||
} | ||
void BaseObject::show() { | ||
figure.update(static_cast<int>(round(position.x())), static_cast<int>(round(position.y()))); | ||
} | ||
bool BaseObject::block_crash(BaseObject& t) { | ||
//if( | ||
// position.x() > t.position.x() - figure.width() && position.x() < t.position.x() + t.figure.width() | ||
// && | ||
// position.y() > t.position.y() - figure.height() && position.y() < t.position.y() + t.figure.height() | ||
// ) | ||
// return true; | ||
//cout << t.position.x() << " " << t.position.y() << endl; | ||
int px = static_cast<int>(round(position.x())), | ||
py = static_cast<int>(round(position.y())), | ||
tpx = static_cast<int>(round(t.position.x())), | ||
tpy = static_cast<int>(round(t.position.y())); | ||
|
||
//上碰撞(相对被撞物体) | ||
if(px + width() -1 >= tpx && px <= tpx + t.width() -1 && | ||
py + height() >= tpy && py < tpy) { | ||
if(velocity.y() > 0) { | ||
velocity.y(0); | ||
position.y((double)tpy - height()); | ||
} | ||
return true; | ||
} | ||
//下碰撞 | ||
if(px + width() -1 >= tpx && px <= tpx + t.width() -1 && | ||
py + height() -1 > tpy + height() -1 && py <= tpy + t.height()) { | ||
if(velocity.y() < 0) { | ||
velocity.y(0); | ||
position.y((double)tpy + t.height()); | ||
} | ||
return true; | ||
} | ||
//左碰撞 | ||
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()); | ||
} | ||
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()); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
//bool BaseObject::pxiel_crash() { | ||
// | ||
//} | ||
|
||
|
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,93 +1,103 @@ | ||
#pragma once | ||
#ifndef _BASIC_OBJECT_H_ | ||
#define _BASIC_OBJECT_H_ | ||
|
||
|
||
#include <graphics.h> | ||
#include <vector> | ||
#include <functional> | ||
#include <chrono> | ||
#include "Global.h" | ||
#include<vector> | ||
#include<functional> | ||
|
||
|
||
|
||
class Vector { | ||
private: | ||
double _x; | ||
double _y; | ||
public: | ||
Vector():_x(0), _y(0) {} | ||
Vector(const double x, const double y):_x(x), _y(y) {} | ||
Vector(const Vector& v):_x(v._x), _y(v._y) {} | ||
Vector operator = (const Vector& rhs); | ||
Vector operator + (const Vector& rhs) const; | ||
Vector operator - (const Vector& rhs) const; | ||
Vector operator * (const double rhs) const; | ||
Vector operator / (const double rhs) const; | ||
Vector operator += (const Vector& rhs); | ||
Vector operator -= (const Vector& rhs); | ||
Vector operator *= (const double rhs); | ||
Vector operator /= (const double rhs); | ||
bool operator == (const Vector& rhs) const; | ||
bool operator == (const double rhs) const; | ||
bool operator != (const Vector& rhs) const; | ||
double x(); | ||
double y(); | ||
void x(double x); | ||
void y(double y); | ||
void set(double x, double y); | ||
}; | ||
class Position: public Vector { | ||
public: | ||
using Vector::Vector; | ||
using Vector::operator=; | ||
}; | ||
class Velocity: public Vector { | ||
public: | ||
using Vector::Vector; | ||
using Vector::operator=; | ||
}; | ||
class Acceleration: public Vector { | ||
public: | ||
using Vector::Vector; | ||
using Vector::operator=; | ||
}; | ||
struct FigureData { | ||
string name; | ||
vector<IMAGE> images; | ||
vector<IMAGE> masks; | ||
std::function<bool(void)> tigger; | ||
}; | ||
|
||
class Figure { | ||
private: | ||
vector<FigureData> figures; | ||
int _width; | ||
int _height; | ||
string status; | ||
string _status; | ||
public: | ||
Figure():figures(),_width(0),_height(0),status("default") {} | ||
void addFigure(string name, vector<IMAGE> imgs, vector<IMAGE> masks, function<bool(void)> tigger) { | ||
if(figures.empty() && !imgs.empty()) { | ||
_width = imgs.front().getwidth(); | ||
_height = imgs.front().getheight(); | ||
} | ||
figures.push_back(FigureData{name, imgs, masks, tigger}); | ||
} | ||
void update(int x, int y) { | ||
for(auto& i : figures) | ||
{ | ||
if(i.tigger()) { | ||
using namespace std::chrono; | ||
static auto time_tick = steady_clock::now(); | ||
static int cnt = 0; | ||
if(status != i.name) { | ||
time_tick = steady_clock::now(); | ||
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); | ||
if((steady_clock::now() - time_tick) >= milliseconds(100)) { | ||
cnt = (cnt + 1) % i.images.size(); | ||
time_tick = steady_clock::now(); | ||
} | ||
|
||
} | ||
} | ||
} | ||
void turn() { | ||
for(auto& i : figures) | ||
{ | ||
for(auto& j : i.images) rotateFlip(&j); | ||
for(auto& j : i.masks) rotateFlip(&j); | ||
} | ||
} | ||
int width() { | ||
return _width; | ||
} | ||
int height() { | ||
return _height; | ||
} | ||
Figure():figures(),_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(); | ||
string status(); | ||
void status(string s); | ||
int width(); | ||
int height(); | ||
}; | ||
|
||
|
||
class BaseObject | ||
{ | ||
private: | ||
int _width; | ||
int _height; | ||
protected: | ||
Position position; | ||
Velocity velocity; | ||
Acceleration acceleration; | ||
Figure figure; | ||
public: | ||
BaseObject():position(), velocity(), acceleration(0, 0.004), figure(){} | ||
void update(double time) { | ||
position += (velocity * time + acceleration * time * time / 2); | ||
velocity += acceleration * time; | ||
} | ||
void show() { | ||
figure.update(position.x(), position.y()); | ||
} | ||
bool block_crash(BaseObject & t) { | ||
if( | ||
position.x() > t.position.x() - figure.width() && position.x() < t.position.x() + t.figure.width() | ||
&& | ||
position.y() > t.position.y() - figure.height() && position.y() < t.position.y() + t.figure.height() | ||
) | ||
return true; | ||
return false; | ||
} | ||
bool pxiel_crash() { | ||
|
||
} | ||
BaseObject():_width(0),_height(0),position(), velocity(), acceleration(0, 0.004), figure(){} | ||
int width(); | ||
int height(); | ||
void width(int w); | ||
void height(int h); | ||
void update(double time); | ||
void show(); | ||
bool block_crash(BaseObject& t); | ||
bool pxiel_crash(); | ||
}; | ||
|
||
|
||
#endif |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "Global.h" | ||
#include <utility> | ||
void rotateFlip(IMAGE* img) | ||
{ | ||
DWORD* img_pMem = GetImageBuffer(img); | ||
|
||
for(int y = 0; y < img->getheight(); y++) | ||
{ | ||
for(int x = 0; x< int(img->getwidth() / 2 + 0.5); x++) | ||
{ | ||
int num1 = y * img->getwidth() + x; | ||
int num2 = (y + 1) * img->getwidth() - x - 1; | ||
std::swap(img_pMem[num1], img_pMem[num2]); | ||
} | ||
} | ||
} |
Oops, something went wrong.