still fighting with item offsets
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 214 B |
@@ -107,6 +107,54 @@
|
||||
"file": "/home/magnus/code/c/projects/raylib_topdown/src/game.c",
|
||||
"output": "/home/magnus/code/c/projects/raylib_topdown/target"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"/usr/bin/gcc",
|
||||
"-c",
|
||||
"-o",
|
||||
"target",
|
||||
"src/item.c"
|
||||
],
|
||||
"directory": "/home/magnus/code/c/projects/raylib_topdown",
|
||||
"file": "/home/magnus/code/c/projects/raylib_topdown/src/item.c",
|
||||
"output": "/home/magnus/code/c/projects/raylib_topdown/target"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"/usr/bin/gcc",
|
||||
"-c",
|
||||
"-o",
|
||||
"target",
|
||||
"src/item_lance.c"
|
||||
],
|
||||
"directory": "/home/magnus/code/c/projects/raylib_topdown",
|
||||
"file": "/home/magnus/code/c/projects/raylib_topdown/src/item_lance.c",
|
||||
"output": "/home/magnus/code/c/projects/raylib_topdown/target"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"/usr/bin/gcc",
|
||||
"-c",
|
||||
"-o",
|
||||
"target",
|
||||
"src/item_meele.c"
|
||||
],
|
||||
"directory": "/home/magnus/code/c/projects/raylib_topdown",
|
||||
"file": "/home/magnus/code/c/projects/raylib_topdown/src/item_meele.c",
|
||||
"output": "/home/magnus/code/c/projects/raylib_topdown/target"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"/usr/bin/gcc",
|
||||
"-c",
|
||||
"-o",
|
||||
"target",
|
||||
"src/item_sword.c"
|
||||
],
|
||||
"directory": "/home/magnus/code/c/projects/raylib_topdown",
|
||||
"file": "/home/magnus/code/c/projects/raylib_topdown/src/item_sword.c",
|
||||
"output": "/home/magnus/code/c/projects/raylib_topdown/target"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"/usr/bin/gcc",
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
#include <raylib.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
typedef struct ItemTransl item_transl_t;
|
||||
typedef struct Item item_t;
|
||||
typedef struct Player player_t;
|
||||
typedef struct timeval timeval_t;
|
||||
typedef struct Entity entity_t;
|
||||
typedef struct World world_t;
|
||||
@@ -37,3 +40,5 @@ void play_animation(animator_t* anim, int state);
|
||||
unsigned long get_time_since_ms(timeval_t begin);
|
||||
|
||||
Rectangle get_animation_frame(entity_t* entity);
|
||||
|
||||
item_transl_t get_item_animation_frame(item_t* item, player_t* player);
|
||||
|
||||
+5
-1
@@ -16,17 +16,21 @@ typedef enum ItemType {
|
||||
MEELE_WEAPON,
|
||||
} itemtype_t;
|
||||
|
||||
typedef struct Item item_t;
|
||||
typedef struct Item {
|
||||
Texture2D sprite;
|
||||
Vector2 render_offset;
|
||||
Vector2 rot_origin_offs;
|
||||
|
||||
itemtype_t type;
|
||||
int cooldown_ms;
|
||||
|
||||
void* item_data;
|
||||
void (*item_action)(player_t*, world_t*);
|
||||
item_transl_t (*get_item_anim_transl)(item_t*, player_t*);
|
||||
} item_t;
|
||||
|
||||
void free_item(item_t* item);
|
||||
item_transl_t lerp_item_transl(item_transl_t a, item_transl_t b, float factor);
|
||||
|
||||
void free_item(item_t* item);
|
||||
void do_item_action(player_t* player, world_t* world);
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# pragma once
|
||||
|
||||
typedef struct Item item_t;
|
||||
|
||||
item_t* create_item_lance();
|
||||
@@ -6,8 +6,10 @@
|
||||
|
||||
typedef struct ItemDataMeele {
|
||||
int damage;
|
||||
|
||||
item_transl_t animation[10];
|
||||
int used_animation_frames;
|
||||
int used_frames;
|
||||
int animation_length_ms;
|
||||
|
||||
Rectangle hurtbox;
|
||||
float attack_speed;
|
||||
|
||||
+31
-1
@@ -1,11 +1,14 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
# include "../include/anim.h"
|
||||
# include "../include/entity.h"
|
||||
# include "../include/player.h"
|
||||
# include "../include/renderer.h"
|
||||
# include "../include/world.h"
|
||||
# include "../include/item.h"
|
||||
# include "../include/item_meele.h"
|
||||
|
||||
Rectangle get_animation_frame(entity_t* entity) {
|
||||
unsigned long time_delta = get_time_since_ms(entity->anim.anim_start);
|
||||
@@ -81,3 +84,30 @@ void render_entity(entity_t* entity, world_t* world) {
|
||||
.tint = entity->tint,
|
||||
});
|
||||
}
|
||||
|
||||
item_transl_t get_item_animation_frame(item_t* item, player_t* player) {
|
||||
itemdata_meele_t* item_data = get_meele_data(item->item_data);
|
||||
|
||||
item_transl_t translation = { Vector2Zero(), .0f };
|
||||
|
||||
long int anim_time = get_time_since_ms(player->last_item_action);
|
||||
|
||||
if (anim_time >= item_data->animation_length_ms) { return translation; };
|
||||
|
||||
double time_fraction = ((float)anim_time / item_data->animation_length_ms);
|
||||
double current_index = item_data->used_frames * time_fraction;
|
||||
int index_int = floor(current_index);
|
||||
double frac = current_index - index_int;
|
||||
// printf("INDEX: %d TIME: %ld FRAME_T: %lf\n", index_int, anim_time, frac);
|
||||
|
||||
if (index_int >= item_data->used_frames - 1) {
|
||||
translation = item_data->animation[index_int];
|
||||
}
|
||||
else {
|
||||
// lerp
|
||||
translation = lerp_item_transl(item_data->animation[index_int], item_data->animation[index_int+1], frac);
|
||||
}
|
||||
// no lerping for last element
|
||||
|
||||
return translation;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "../include/enemy.h"
|
||||
#include "../include/item.h"
|
||||
#include "../include/item_sword.h"
|
||||
#include "../include/item_lance.h"
|
||||
|
||||
int start_game() {
|
||||
player_t player;
|
||||
|
||||
@@ -21,3 +21,10 @@ void do_item_action(player_t* player, world_t* world) {
|
||||
}
|
||||
}
|
||||
|
||||
item_transl_t lerp_item_transl(item_transl_t a, item_transl_t b, float factor) {
|
||||
item_transl_t new_transl = a;
|
||||
new_transl.position.x += factor * (b.position.x-new_transl.position.x);
|
||||
new_transl.position.y += factor * (b.position.y-new_transl.position.y);
|
||||
new_transl.rotation += factor * (b.rotation-new_transl.rotation);
|
||||
return new_transl;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# include <assert.h>
|
||||
|
||||
# include "../include/item_lance.h"
|
||||
# include "../include/item.h"
|
||||
# include "../include/anim.h"
|
||||
# include "../include/item_meele.h"
|
||||
|
||||
item_t* create_item_lance() {
|
||||
item_t* item_p = create_meele_item_base();
|
||||
item_p->rot_origin_offs = (Vector2) { 0, 0 };
|
||||
|
||||
itemdata_meele_t* data = get_meele_data(item_p->item_data);
|
||||
data->animation[0] = (item_transl_t) {(Vector2) {0,0}, (float) 0.0f};
|
||||
data->animation[1] = (item_transl_t) {(Vector2) {0,-2}, (float) -40.0f};
|
||||
data->animation[2] = (item_transl_t) {(Vector2) {0,-1}, (float) 30.0f};
|
||||
data->animation[3] = (item_transl_t) {(Vector2) {0,0}, (float) 20.0f};
|
||||
data->animation[4] = (item_transl_t) {(Vector2) {0,0}, (float) 0.0f};
|
||||
|
||||
data->used_frames = 5; // not as index
|
||||
data->animation_length_ms = 1000;
|
||||
|
||||
item_p->cooldown_ms = 700;
|
||||
|
||||
item_p->sprite = LoadTexture("assets/sprites/items/lance.png");
|
||||
item_p->render_offset = (Vector2) { -8, -5 };
|
||||
|
||||
item_p->type = MEELE_WEAPON;
|
||||
item_p->item_action = &meele_item_attack;
|
||||
item_p->get_item_anim_transl = &get_item_animation_frame;
|
||||
return item_p;
|
||||
}
|
||||
@@ -21,6 +21,5 @@ item_t* create_meele_item_base() {
|
||||
|
||||
void meele_item_attack(player_t* player, world_t* world) {
|
||||
printf("TODO: make it attack stuff\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
+14
-3
@@ -1,20 +1,31 @@
|
||||
# include <stdlib.h>
|
||||
# include <assert.h>
|
||||
|
||||
# include "../include/item_sword.h"
|
||||
# include "../include/item.h"
|
||||
# include "../include/anim.h"
|
||||
# include "../include/item_meele.h"
|
||||
|
||||
item_t* create_item_sword() {
|
||||
|
||||
item_t* item_p = create_meele_item_base();
|
||||
item_p->rot_origin_offs = (Vector2) { 0, 16 };
|
||||
|
||||
item_p->cooldown_ms = 1000;
|
||||
itemdata_meele_t* data = get_meele_data(item_p->item_data);
|
||||
data->animation[0] = (item_transl_t) {(Vector2) {0,0}, (float) 0.0f};
|
||||
data->animation[1] = (item_transl_t) {(Vector2) {0,-2}, (float) -40.0f};
|
||||
data->animation[2] = (item_transl_t) {(Vector2) {0,-1}, (float) 30.0f};
|
||||
data->animation[3] = (item_transl_t) {(Vector2) {0,0}, (float) 20.0f};
|
||||
data->animation[4] = (item_transl_t) {(Vector2) {0,0}, (float) 0.0f};
|
||||
|
||||
data->used_frames = 5; // not as index
|
||||
data->animation_length_ms = 1000;
|
||||
|
||||
item_p->cooldown_ms = 700;
|
||||
|
||||
item_p->sprite = LoadTexture("assets/sprites/items/sword.png");
|
||||
item_p->render_offset = (Vector2) { 4, -5 };
|
||||
|
||||
item_p->type = MEELE_WEAPON;
|
||||
item_p->item_action = &meele_item_attack;
|
||||
item_p->get_item_anim_transl = &get_item_animation_frame;
|
||||
return item_p;
|
||||
}
|
||||
|
||||
+14
-5
@@ -116,6 +116,7 @@ void equip_item(player_t* player, item_t* item) {
|
||||
}
|
||||
|
||||
void unequip_item(player_t* player) {
|
||||
if (!player->current_item) {return;};
|
||||
UnloadTexture(player->current_item->sprite);
|
||||
free_item(player->current_item);
|
||||
player->current_item = NULL;
|
||||
@@ -123,17 +124,25 @@ void unequip_item(player_t* player) {
|
||||
|
||||
void render_player_item(player_t* player) {
|
||||
item_t* item = player->current_item;
|
||||
|
||||
item_transl_t anim_offset = { Vector2Zero(), .0f };
|
||||
if (item->get_item_anim_transl) {
|
||||
// item has animation
|
||||
anim_offset = item->get_item_anim_transl(item, player);
|
||||
}
|
||||
|
||||
int what_the_flip = player->entity.anim.flipped ? -1 : 1;
|
||||
int flip_dep_ply_width = player->entity.anim.flipped * player->entity.sprite_size.x;
|
||||
|
||||
Vector2 item_size = { item->sprite.width, item->sprite.height };
|
||||
|
||||
Vector2 flip_rend_offs = item->render_offset;
|
||||
Vector2 flip_rend_offs = Vector2Add(item->render_offset, anim_offset.position);
|
||||
flip_rend_offs.x *= what_the_flip;
|
||||
|
||||
Rectangle src = { 0, 0, item_size.x * what_the_flip, item_size.y };
|
||||
Rectangle dest = {
|
||||
player->entity.position.x + flip_rend_offs.x,
|
||||
player->entity.position.y + flip_rend_offs.y,
|
||||
player->entity.position.x + flip_rend_offs.x - flip_dep_ply_width + (item->rot_origin_offs.x * what_the_flip) + flip_dep_ply_width,
|
||||
player->entity.position.y + flip_rend_offs.y + item->rot_origin_offs.y,
|
||||
item_size.x, item_size.y
|
||||
};
|
||||
|
||||
@@ -145,8 +154,8 @@ void render_player_item(player_t* player) {
|
||||
.texture = item->sprite,
|
||||
.src = src,
|
||||
.dest = dest,
|
||||
.origin = (Vector2) { 0.0f, 0.0f },
|
||||
.rotation = 0.0f,
|
||||
.origin = (Vector2){item->rot_origin_offs.x - flip_dep_ply_width, item->rot_origin_offs.y},
|
||||
.rotation = anim_offset.rotation * what_the_flip,
|
||||
.tint = WHITE,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user