animation is now decoupled from sprite
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#ifndef ANIMSPRITE_H
|
||||
#define ANIMSPRITE_H
|
||||
|
||||
typedef struct {
|
||||
int currentFrame;
|
||||
int maxFrame;
|
||||
int framesCounter;
|
||||
int framesSpeed;
|
||||
float animTimer;
|
||||
} Animation;
|
||||
|
||||
void UpdateAnimation(Animation *animation, float deltaTime) {
|
||||
animation->animTimer += deltaTime;
|
||||
float frameTime = 1.0f / animation->framesSpeed;
|
||||
|
||||
if (animation->animTimer < frameTime) {
|
||||
return;
|
||||
}
|
||||
|
||||
animation->animTimer -= frameTime;
|
||||
animation->currentFrame++;
|
||||
|
||||
if (animation->currentFrame >= animation->maxFrame) {
|
||||
animation->currentFrame = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // ANIMSPRITE_H
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
#include <raymath.h>
|
||||
#include "input.h"
|
||||
|
||||
void UpdateCamera2DTarget(Camera2D *camera, Vector2 *position, const float smoothness){
|
||||
camera->target = Vector2Lerp(camera->target, *position, smoothness * GetFrameTime());
|
||||
}
|
||||
|
||||
void UpdateCamera2DZoom(Camera2D *camera, const float *scaleFactor, const float *minZoom, const float *maxZoom){
|
||||
float targetZoom = Clamp(camera->zoom * *scaleFactor, *minZoom, *maxZoom);
|
||||
camera->zoom = Lerp(camera->zoom, targetZoom, 0.1f);
|
||||
}
|
||||
|
||||
#endif // CAMERA_H
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef INPUT_H
|
||||
#define INPUT_H
|
||||
#include <raymath.h>
|
||||
|
||||
typedef struct {
|
||||
Vector2 worldPosition;
|
||||
float zoomSpeed;
|
||||
float minZoom;
|
||||
float maxZoom;
|
||||
float scaleFactor;
|
||||
float wheelMove;
|
||||
bool leftButton;
|
||||
bool rightButton;
|
||||
} Mouse;
|
||||
|
||||
void UpdateMouse(Mouse *mouse, Camera2D *camera) {
|
||||
mouse->worldPosition = GetScreenToWorld2D(GetMousePosition(), *camera);
|
||||
mouse->leftButton = IsMouseButtonDown(MOUSE_LEFT_BUTTON);
|
||||
mouse->rightButton = IsMouseButtonDown(MOUSE_RIGHT_BUTTON);
|
||||
|
||||
// Calculate zoom scale factor
|
||||
mouse->wheelMove = GetMouseWheelMove();
|
||||
if (mouse->wheelMove != 0) {
|
||||
float zoomFactor = 1.0f + (mouse->zoomSpeed * fabsf(mouse->wheelMove));
|
||||
mouse->scaleFactor = (mouse->wheelMove < 0) ? (1.0f / zoomFactor) : zoomFactor;
|
||||
} else {
|
||||
mouse->scaleFactor = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateVectorOnInput(Vector2 *vector){
|
||||
*vector = (Vector2){0.0f, 0.0f};
|
||||
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)){ vector->x += 1.0f; }
|
||||
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)){ vector->x -= 1.0f; }
|
||||
if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)){ vector->y += 1.0f; }
|
||||
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)){ vector->y -= 1.0f; }
|
||||
if (Vector2Length(*vector) > 0.0f) { *vector = Vector2Normalize(*vector); }
|
||||
}
|
||||
|
||||
#endif //INPUT_H
|
||||
+6
-4
@@ -1,11 +1,13 @@
|
||||
#ifndef OBJECT_H
|
||||
#define OBJECT_H
|
||||
#include <raylib.h> // Vector2, Rectangle, Color, GetRandomValue(), CheckCollisionRecs(), CheckCollisionPointRec()
|
||||
#include <stdio.h> // printf()
|
||||
#include <stdlib.h> // malloc(), realloc()
|
||||
#include "sprite.h" // Sprite
|
||||
#include <raylib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <raymath.h>
|
||||
#include "sprite.h"
|
||||
#include "vector.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
Sprite sprite;
|
||||
Vector2 position;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef PHYSICS_H
|
||||
#define PHYSICS_H
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
Vector2 position;
|
||||
Vector2 direction;
|
||||
float speed;
|
||||
} Physics;
|
||||
|
||||
Vector2 GetVelocity(Vector2 *direction, float *speed){
|
||||
return Vector2Scale(*direction, *speed * GetFrameTime());
|
||||
}
|
||||
|
||||
void Move(Vector2 *position, Vector2 *direction, float *speed) {
|
||||
*position = Vector2Add(*position, GetVelocity(direction, speed));
|
||||
}
|
||||
|
||||
#endif // PHYSICS_H
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef SPRITE_H
|
||||
#define SPRITE_H
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
|
||||
typedef struct {
|
||||
Texture2D texture;
|
||||
@@ -22,6 +23,14 @@ void UpdateSpriteDestRec(Sprite *sprite, const Vector2 *vector){
|
||||
sprite->destRec.y = vector->y;
|
||||
}
|
||||
|
||||
void UpdateSpriteRotation(Sprite *sprite, Vector2 *vector){
|
||||
if(Vector2Length(*vector) <= 0.0f){
|
||||
return;
|
||||
}
|
||||
float targetRotation = (float)atan2(vector->y, vector->x) * (180.0f / PI) + 90.0f;
|
||||
sprite->rotation = Lerp(sprite->rotation, targetRotation, 0.1f);
|
||||
}
|
||||
|
||||
void RenderSprite(const Sprite *sprite){
|
||||
DrawTexturePro(
|
||||
sprite->texture,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
#include <raylib.h>
|
||||
#include <stdlib.h>
|
||||
#include <raymath.h>
|
||||
|
||||
typedef struct {
|
||||
Vector2 *data;
|
||||
|
||||
Reference in New Issue
Block a user