Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //gfx_BlitRectangle
- #include <stdio.h>
- #include <fileioc.h>
- #include <keypadc.h>
- #include <graphx.h>
- #include "gfx/gfx.h"
- #include <sys/timers.h>
- #include <math.h>
- typedef struct {
- double x;
- double y;
- } Point;
- struct Tilemap {
- gfx_sprite_t **sprites; // Array of array of pointers to sprite data for each frame
- int sprites_num;
- int **sprite_matrix;
- };
- struct SpriteInfo {
- gfx_sprite_t **frames; // Array of pointers to sprite data for each frame
- int num_frames;
- int current_frame;
- };
- int findNearestTile(struct Tilemap *tilemap, int tileX, int tileY) {
- // Return the pointer to the nearest tile sprite
- return tilemap->sprite_matrix[tileY / 16][tileX / 16]; // / by 16 because it needs screen pixel to matrix
- }
- double getDistance(Point point1, Point point2){
- double dx = point1.x - point2.x;
- double dy = point1.y - point2.y;
- double distance = sqrt(dx * dx + dy * dy);
- return distance;
- }
- Point closestJump(struct Tilemap *tilemap, int playerX, int playerY){
- double closest = 10000;
- Point closestPoint = {0, 0};
- double dist = 0;
- for (int i = 0; i < 20; i++) {
- for (int j = 0; j < 20; j++) {
- if(tilemap->sprite_matrix[i][j] == 1){
- dist = getDistance((Point) {i * 16, j * 16}, (Point) {playerX, playerY});
- if(dist < closest){
- closest = dist;
- closestPoint = (Point) {i * 16, j * 16};
- }
- }
- }
- }
- return closestPoint;
- }
- void sprite(struct SpriteInfo *spriteInfo, int posX, int posY) {//second used to be Tilemap IMPORTANT if you want to bring the tile drawing back
- // int tileX = (round(posX / 16)) * 16;
- // int tileY = (round(posY / 16)) * 16;
- // if(tileY == 240){
- // tileY = 224;
- // }
- // int Tile = findNearestTile(tilemap, tileX, tileY);
- //gfx_Sprite(tilemap->sprites[Tile], tileX, tileY);
- gfx_TransparentSprite(spriteInfo->frames[spriteInfo->current_frame], posX, posY);
- }
- void animatedSprite(struct SpriteInfo *spriteInfo, int posX, int posY) { //second used to be Tilemap IMPORTANT if you want to bring the tile drawing back
- // int tileX = (round(posX / 16)) * 16;
- // int tileY = (round(posY / 16)) * 16;
- // if(tileY == 240){
- // tileY = 224;
- // }
- // int Tile = findNearestTile(tilemap, tileX, tileY);
- //gfx_Sprite(tilemap->sprites[Tile], tileX, tileY);
- gfx_TransparentSprite(spriteInfo->frames[spriteInfo->current_frame], posX, posY);
- if (++spriteInfo->current_frame >= spriteInfo->num_frames) {
- spriteInfo->current_frame = 0;
- }
- }
- void drawTileset(struct Tilemap tileset){
- for (int i = 0; i < 20; i++) {
- for (int j = 0; j < 20; j++) {
- gfx_Sprite(tileset.sprites[tileset.sprite_matrix[i][j]], i * 16, j * 16);
- }
- }
- }
- Point calculateTrajectory(double x0, double y0, double x1, double y1, double time_ms, int originalX0, int originalY0) {
- double t_travel = (fabs(originalX0 - x1) / 70.0) + .1; //seconds
- double v0 = (x1 - x0) / t_travel;
- double a = 2 * (y1 - y0 - v0 * t_travel) / (t_travel * t_travel);
- double vx = v0;
- double t = time_ms / 1000.0; // Convert time_passed to seconds
- // Calculate the y-coordinate of the control point
- double control_point_y = y0 + 1 * (y1 - y0);
- Point point;
- point.x = x0 + vx * t;
- point.y = y0 + v0 * t + 0.5 * a * t * t;
- // Adjust the y-coordinate based on the height_factor
- point.y += (control_point_y - point.y) * (2 * t / t_travel) * (1 - t / t_travel);
- if(point.y >= y1 - 1 && point.y <= y1 + 1){
- point.y = y1;
- }
- if(point.x >= x1 - 1 && point.x <= x1 + 1){
- point.x = x1;
- }
- return point;
- }
- int main() {
- gfx_Begin();
- gfx_FillScreen(1);
- gfx_SetDraw(gfx_buffer);
- struct Tilemap tileMap;
- tileMap.sprites_num = 2;
- tileMap.sprites = malloc(sizeof(gfx_sprite_t *) * tileMap.sprites_num);
- tileMap.sprites[0] = untitled;
- tileMap.sprites[1] = JumpingTile;
- // Dynamically allocate memory for the sprite matrix
- int matrix_size = 20;
- tileMap.sprite_matrix = malloc(sizeof(int *) * matrix_size);
- for (int i = 0; i < matrix_size; i++) {
- tileMap.sprite_matrix[i] = malloc(sizeof(int) * matrix_size);
- for (int j = 0; j < matrix_size; j++) {
- tileMap.sprite_matrix[i][j] = 0;
- gfx_SetDraw(gfx_buffer);
- gfx_Sprite(untitled, i * 16, j * 16);
- gfx_SetDraw(gfx_screen);
- gfx_Sprite(untitled, i * 16, j * 16);
- }
- }
- tileMap.sprite_matrix[1][14] = 1;
- tileMap.sprite_matrix[7][14] = 1;
- gfx_SetDraw(gfx_buffer);
- gfx_Sprite(JumpingTile, 1 * 16, 19 * 16);
- gfx_SetDraw(gfx_screen);
- gfx_Sprite(JumpingTile, 1 * 16, 19 * 16);
- struct SpriteInfo cat;
- cat.num_frames = 4;
- cat.current_frame = 0;
- cat.frames = malloc(sizeof(gfx_sprite_t *) * cat.num_frames * 16);
- cat.frames[0] = cat1;
- cat.frames[1] = cat2;
- cat.frames[2] = cat3;
- cat.frames[3] = cat4;
- gfx_SetPalette(global_palette, sizeof_global_palette, 0);
- /* These were set in the image conversion file */
- gfx_SetTransparentColor(0);
- int animationMS = 33;//83
- int msSinceLast = 0;
- int msStart = 0; // animation reset
- int msLoopStart = 0; //loop reset
- float playerX = 100;
- int playerY = 240 - 15; // - sprite size
- float playerSpeed = .1;
- bool isJumping = false;
- int jumpmsPassed = 0;
- bool firstJump = true;
- int originalX = 0;
- int originalY = 0;
- int playerOffsetY = 15;
- Point nearestJump = {0, 0};
- gfx_SetTextXY(100, 100);
- gfx_SetDraw(gfx_buffer);
- timer_Set(1, 0);
- timer_Enable(1, TIMER_32K, TIMER_NOINT, TIMER_UP);
- while (!(kb_Data[6] & kb_Clear)) {
- msLoopStart = timer_Get(1)/32.768;
- kb_Scan();
- if(!isJumping){
- if(kb_Data[7] & kb_Left){
- playerX -= playerSpeed * msSinceLast;
- }
- if(kb_Data[7] & kb_Right){
- playerX += playerSpeed * msSinceLast;
- }
- if(kb_Data[1] & kb_2nd){
- isJumping = true;
- }
- }else{
- jumpmsPassed += msSinceLast;
- if(firstJump){
- originalX = playerX;
- originalY = playerY;
- firstJump = false;
- nearestJump.y -= playerOffsetY;
- }
- Point traj = calculateTrajectory(playerX, playerY, nearestJump.x, nearestJump.y, jumpmsPassed, originalX, originalY); // subtract 17 because of the position
- playerX = traj.x;
- playerY = traj.y;
- if(playerX == nearestJump.x && playerY == nearestJump.y){
- firstJump = true;
- isJumping = false;
- jumpmsPassed = 0;
- }
- }
- if(msLoopStart - msStart >= animationMS){
- //gfx_FillRectangle(playerX, playerY, 16, 16);
- animatedSprite(&cat, (int) playerX, playerY); //second was , &tileMap
- gfx_SwapDraw();
- drawTileset(tileMap);
- if(!isJumping){
- nearestJump = closestJump(&tileMap, playerX, playerY);
- gfx_SetColor(0);
- gfx_Rectangle(nearestJump.x, nearestJump.y, 2, 2);
- }
- msStart = msLoopStart;
- //timer_Set(1, 0);
- }else{
- sprite(&cat, (int) playerX, playerY); // , &tileMap
- gfx_SwapDraw();
- }
- msSinceLast = timer_Get(1)/32.768 - msLoopStart;
- }
- timer_Disable(1);
- // Free the dynamically allocated memory before exiting the program
- for (int i = 0; i < matrix_size; i++) {
- free(tileMap.sprite_matrix[i]);
- }
- free(tileMap.sprite_matrix);
- free(tileMap.sprites);
- free(cat.frames);
- gfx_End();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement