萌新跟练日志03【植物明星大乱斗】
跟随B站 Voidmatrix 的步伐第三天动画系统与简单二维向量终于开始做可以看得到的东西了目录动画类封装二维向量动画类教程里的OnUpdate函数是用三元运算符将不循环动画定格在最后一帧而我是用isFinished控制更新程序的运行与否以达到定格的效果似乎我的方法可读性更高而教程的方法更简洁#pragma once #include Tool.h #include Atlas.h #includeiostream #includefunctional class Animation { public: void SetAtlas(Atlas atlas) { this-atlas atlas; } void SetInterval(int itv) { interval itv; } void SetIsLoop(bool isLoop) { this-isLoop isLoop; } void SetOnEnd(std::functionvoid() onEnd) { this-onEnd onEnd; } void OnDraw(int x, int y) { putimage_alpha(x, y, atlas.GetImage(frameIndex)); } void OnUpdate(int delta) { if (CheckFinished()) return; timer delta; if(timer interval) { frameIndex; if (frameIndex atlas.GetSize()) { if (isLoop) frameIndex 0; else { isFinished true; frameIndex atlas.GetSize() - 1; if(onEnd) onEnd(); } } timer - interval; } } void Reset() { timer 0; frameIndex 0; isFinished false; } bool CheckFinished() { return isFinished; } private: Atlas atlas; int timer; int interval; int frameIndex; bool isLoop true; bool isFinished; std::functionvoid() onEnd; };可以看到OnDraw函数在每次绘制时都要传递坐标参数能不能使用指针链接游戏物体的坐标这样能使绘制坐标直接等同游戏物体的物理坐标省去了OnDraw函数的传参//假想代码 //并未真实放入项目中 class Animation { public: void SetPosition(int* object_x, int* object_y) { x object_x; y object_y; } //改为无参 void OnDraw() { putimage_alpha(*x, *y, atlas.GetImage(frameIndex)); } private: int* x; int* y; Atlas atlas; int frameIndex; };另外动画是一种需要在初始化阶段就加载好的资源还是跟随游戏物体生命周期的组件我感觉是一种组件在一局游戏中会同时存在许多拥有相同动画的游戏物体比如子弹、敌人如果动画是一种资源那么这些游戏物体共用同一个资源就会出现动画节奏一致的诡异情况封装二维向量简单的支持浮点型的自制二维向量类如向量点乘之类的运算懒得搞了这个项目中可能也用不到#pragma once #includemath.h class Vector2 { public: Vector2() { x 0; y 0; } Vector2(double x, double y) : x(x), y(y){} ~Vector2() default; double x; double y; Vector2 Normalized() { Vector2 result; result.x x / sqrt(x * x y * y); result.y y / sqrt(x * x y * y); return result; } double Norm() { return sqrt(x * x y * y); } bool Equal(const Vector2 target) const { return x target.x y target.y; } void operator(const Vector2 target) { x target.x; y target.y; } void operator(const Vector2 target) { x target.x; y target.y; } void operator-(const Vector2 target) { x - target.x; y - target.y; } Vector2 operator(const Vector2 target) const { return Vector2(x target.x, y target.y); } Vector2 operator-(const Vector2 target) const { return Vector2(x - target.x, y - target.y); } Vector2 operator*(double target) const { return Vector2(x * target, y * target); } Vector2 operator/(double target) const { return Vector2(x / target, y / target); } private: };在开发动画系统时有一点自己的小想法所用图形库EasyX文档EasyX 文档 - 基本说明