summaryrefslogtreecommitdiff
path: root/src/camera.c
blob: 5cca51bb55c2f27bc9224c462f965a0e87202fe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "camera.h"
#include "cfg.h"
#include "clamp.h"
#include "map.h"
#include <math.h>
#include <stdlib.h>

static double cam[2] = {0, 0};
static double off[2] = {0, 0};

int flip_offset_x;
int flip_offset_y;

void
camera_init(void)
{
	cam[0] = 0.0;
	cam[1] = 0.0;
}

void
camera_teleport(int x, int y)
{
	cam[0] = x;
	cam[1] = y;
}

void
camera_update(double dest[2])
{
	for (int i = 0; i < 2; i++)
		cam[i] += 0.1 * (dest[i] - cam[i]);
	cam[0] = clamp(DWIDTH/2.0, map_width() * TSIZE - DWIDTH/2.0, cam[0]);
	cam[1] = clamp(DHEIGHT/2.0, map_height() * TSIZE - DHEIGHT/2.0, cam[1]);
	flip_offset_x += rand() % 15 - 8;
	flip_offset_y += rand() % 15 - 8;
}

int
camera_x(double scale)
{
	return -cam[0] * scale + DWIDTH / 2.0 + off[0] - 2 + rand() % 5;
}

int
camera_y(double scale)
{
	return -cam[1] * scale + DHEIGHT / 2.0 + off[1] - 2 + rand() % 5;
}