Skip to content

Commit

Permalink
fix: cloud speed impacted by game speed (#1035)
Browse files Browse the repository at this point in the history
* fix: cloud speed impacted by game speed

* fix clouds speed if paused or speed higher than 100

* fix variable name

* Improve cloud pausing and movement

---------

Co-authored-by: José Cadete <crudelios@gmail.com>
  • Loading branch information
dvincent56 and crudelios authored Apr 12, 2024
1 parent f3bb609 commit e85be4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
41 changes: 27 additions & 14 deletions src/graphics/clouds.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "core/image.h"
#include "core/random.h"
#include "core/speed.h"
#include "game/settings.h"
#include "graphics/renderer.h"

#include <math.h>
Expand All @@ -30,6 +31,8 @@

#define CLOUD_SPEED 0.3

#define PAUSE_MIN_FRAMES 2

#define PI 3.14159265358979323846

typedef enum {
Expand Down Expand Up @@ -66,8 +69,11 @@ typedef struct {
int angle;
} cloud_type;

static cloud_type clouds[NUM_CLOUDS];
static int movement_timeout;
static struct {
cloud_type clouds[NUM_CLOUDS];
int movement_timeout;
int pause_frames;
} data;

static int random_from_min_to_range(int min, int range)
{
Expand Down Expand Up @@ -161,7 +167,7 @@ static void init_cloud_images(void)
{
graphics_renderer()->create_custom_image(CUSTOM_IMAGE_CLOUDS, CLOUD_TEXTURE_WIDTH, CLOUD_TEXTURE_HEIGHT, 0);
for (int i = 0; i < NUM_CLOUDS; i++) {
cloud_type *cloud = &clouds[i];
cloud_type *cloud = &data.clouds[i];
image *img = &cloud->img;
img->width = img->original.width = CLOUD_WIDTH;
img->height = img->original.height = CLOUD_HEIGHT;
Expand Down Expand Up @@ -213,7 +219,7 @@ static void generate_cloud(cloud_type *cloud)
static int cloud_intersects(const cloud_type *cloud)
{
for (int i = 0; i < NUM_CLOUDS; i++) {
const cloud_type *other = &clouds[i];
const cloud_type *other = &data.clouds[i];
if (other->status != STATUS_MOVING) {
continue;
}
Expand All @@ -236,31 +242,37 @@ static void position_cloud(cloud_type *cloud, int x_limit, int y_limit)
cloud->status = STATUS_MOVING;
speed_clear(&cloud->speed.x);
speed_clear(&cloud->speed.y);
movement_timeout = random_between_from_stdlib(CLOUD_MIN_CREATION_TIMEOUT, CLOUD_MAX_CREATION_TIMEOUT);
data.movement_timeout = random_between_from_stdlib(CLOUD_MIN_CREATION_TIMEOUT, CLOUD_MAX_CREATION_TIMEOUT);
}
}

void clouds_pause(void)
{
for (int i = 0; i < NUM_CLOUDS; i++) {
speed_clear(&clouds[i].speed.x);
speed_clear(&clouds[i].speed.y);
}
data.pause_frames = PAUSE_MIN_FRAMES;
}

void clouds_draw(int x_offset, int y_offset, int x_limit, int y_limit, float base_scale)
{
if (!config_get(CONFIG_UI_DRAW_CLOUD_SHADOWS)) {
return;
}

double cloud_speed = 0;

if (data.pause_frames) {
data.pause_frames--;
} else {
cloud_speed = CLOUD_SPEED * setting_game_speed() / 100;
}

for (int i = 0; i < NUM_CLOUDS; i++) {
cloud_type *cloud = &clouds[i];
cloud_type *cloud = &data.clouds[i];
if (cloud->status == STATUS_INACTIVE) {
generate_cloud(cloud);
continue;
} else if (cloud->status == STATUS_CREATED) {
if (movement_timeout > 0) {
movement_timeout--;
if (data.movement_timeout > 0) {
data.movement_timeout--;
} else {
position_cloud(cloud, x_limit, y_limit);
}
Expand All @@ -269,8 +281,9 @@ void clouds_draw(int x_offset, int y_offset, int x_limit, int y_limit, float bas
cloud->status = STATUS_INACTIVE;
continue;
}
speed_set_target(&cloud->speed.x, -CLOUD_SPEED, SPEED_CHANGE_IMMEDIATE, 1);
speed_set_target(&cloud->speed.y, CLOUD_SPEED / 2, SPEED_CHANGE_IMMEDIATE, 1);

speed_set_target(&cloud->speed.x, -cloud_speed, SPEED_CHANGE_IMMEDIATE, 1);
speed_set_target(&cloud->speed.y, cloud_speed / 2, SPEED_CHANGE_IMMEDIATE, 1);

graphics_renderer()->draw_image_advanced(&cloud->img,
(cloud->x - x_offset) / base_scale, (cloud->y - y_offset) / base_scale, COLOR_MASK_NONE,
Expand Down
5 changes: 3 additions & 2 deletions src/widget/city_without_overlay.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "figure/formation_legion.h"
#include "figure/roamer_preview.h"
#include "game/resource.h"
#include "game/state.h"
#include "graphics/clouds.h"
#include "graphics/graphics.h"
#include "graphics/image.h"
Expand Down Expand Up @@ -901,10 +902,10 @@ static int get_highlighted_formation_id(const map_tile *tile)

static void update_clouds(void)
{
if (!window_is(WINDOW_CITY) && !window_is(WINDOW_CITY_MILITARY)) {
int camera_x, camera_y;
if (game_state_is_paused() || (!window_is(WINDOW_CITY) && !window_is(WINDOW_CITY_MILITARY))) {
clouds_pause();
}
int camera_x, camera_y;
city_view_get_camera_in_pixels(&camera_x, &camera_y);
clouds_draw(camera_x, camera_y, GRID_SIZE * 60, GRID_SIZE * 30, draw_context.scale);
}
Expand Down

0 comments on commit e85be4e

Please sign in to comment.