13 Star 72 Fork 26

tuxiaobei/mario

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
star.cpp 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
tuxiaobei 提交于 2022-01-05 02:44 . 添加注释,修改物理引擎细节
#include "star.h"
#include "musicplayer.h"
#include "level.h"
Star::Star(char* s)
{
ct = Costume{ 15, 0, 0 };
sscanf(s, "%d%d%d", &ct.b, &ct.c, &direction);
fx = 0;
collider_layer = 6;
id = ++COLLIDER_ID;
freeze = false;
width = 1, height = 1;
maxwx = 75;
name = "star";
show_layer = 2;
vy = -20;
fx = 100 * direction;
}
bool Star::update()
{
if (!isrun) return false;
if (x < 0 || x > level.map_range || y > 16)
level.remove(this);
return false;
}
std::pair<double, double> Star::getctpos()
{
return std::make_pair(sx, sy);
}
Costume Star::getcostume()
{
if (!isshow) return Costume{ -1, -1, -1 };
return ct;
}
bool Star::report_collision(int direction, Collider* target, int target_collider_layer)
{
if (!isrun) return false;
switch (target_collider_layer) {
case 0: //如果碰到马里奥
level.remove(this);
musicplayer.play("sound-powerup");
break;
case 1: //碰到墙壁,反弹
if ((direction == LEFT && fx < 0) || (direction == RIGHT && fx > 0)) {
fx = -fx, vx = -vx, this->direction = -this->direction;
if (direction == LEFT) {
x = max(target->x + (target->width + width) / 2.0, x);
}
else {
x = min(target->x - (target->width + width) / 2.0, x);
}
}
if (direction == TOP) {
vy = 0;
y = max(target->y + (target->height + height) / 2.0, y);
}
if (direction == BOTTOM) {
vy = -20;
y = min(target->y - (target->height + height) / 2.0, y);
}
break;
}
return true;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/tuxiaobei/mario.git
[email protected]:tuxiaobei/mario.git
tuxiaobei
mario
mario
master

搜索帮助