Metro-Siberia-4/src/core.c

147 lines
3.2 KiB
C

/*
* Metro Siberia 4 - A sequel to Dark Storm's Metro Siberia 3 for CASIO calculators.
* Copyright (C) 2015 Dark Storm
* Copyright (C) 2022 Mibi88
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#include "includes.h"
Ship ship;
extern bopti_image_t map_1;
extern bopti_image_t timer_3;
extern bopti_image_t timer_2;
extern bopti_image_t timer_1;
int dgetpixel(int x, int y) // Thanks to Lephenixnoir
{
int offset = (y << 2) + (x >> 5);
return (int)(gint_vram[offset] << (x & 31)) < 0 ? C_BLACK : C_WHITE;
}
int play_game(void)
{
int collision = 0, ticks = 0, timer = 3;
init_ship();
ship.scrolling.x = 0;
ship.scrolling.y = 0;
while(timer != 0){
dclear(C_WHITE);
int i;
dimage(-ship.scrolling.x, 0, &map_1);
calculate_tops();
for(i = 0; i != 3; i++)
dline(ship.tops[i].x, ship.tops[i].y, ship.tops[(i + 1) % 3].x, ship.tops[(i + 1) % 3].y, C_BLACK);
if(timer == 3){
dimage(59, 25, &timer_3);
}else if(timer == 2){
dimage(59, 25, &timer_2);
}else if(timer == 1){
dimage(59, 25, &timer_1);
}
dupdate();
sleep_ms(1000);
timer--;
}
while(1)
{
clearevents();
if(keydown(KEY_SHIFT) || keydown(KEY_EXE)){ // Go up
ship.thrust = false;
}
if(keydown(KEY_EXIT)){
return 2;
}
sleep_ms(20);
calculate_tops();
dclear(C_WHITE);
collision = move();
draw();
ship.thrust = true;
clearevents();
if(ship.scrolling.x >= 3328){
return 1;
}
if(collision == 1){
return 0;
}
}
}
void draw(void)
{
static Vector smoke_cubes[5];
int i;
for(i = 0; i != 3; i++)
dline(ship.tops[i].x, ship.tops[i].y, ship.tops[(i + 1) % 3].x, ship.tops[(i + 1) % 3].y, C_BLACK);
dupdate();
}
int move(void)
{
int i;
ship.acceleration.y = - GRAVITY * (float)(TIMER_PHYSIC) / 1000;
if(ship.thrust) ship.acceleration.y += THRUST * (float)(TIMER_PHYSIC) / 1000;
ship.speed.x += ship.acceleration.x;
ship.speed.y += ship.acceleration.y;
ship.position.x += ship.speed.x;
ship.position.y += ship.speed.y;
ship.scrolling.x = ship.position.x;
dimage(-ship.scrolling.x, 0, &map_1);
// collision
for(i=0;i!=3;i++){
if(dgetpixel(ship.tops[i].x, ship.tops[i].y) == C_BLACK){
return 1;
}
if(ship.tops[i].y<0 || ship.tops[i].y>64){
return 1;
}
}
return 0;
}
void calculate_tops(void)
{
Vector tmp;
float angle = 15.0;
int i;
angle = _atan(ship.speed.y / ship.speed.x);
init_tops();
for(i = 0; i != 3; i++)
{
tmp.x = ship.tops[i].x;
tmp.y = ship.tops[i].y;
ship.tops[i].x = tmp.x * cos(angle) + tmp.y * sin(angle);
ship.tops[i].y = tmp.y * cos(angle) - tmp.x * sin(angle);
ship.tops[i].x += MARGIN;
ship.tops[i].y += ship.position.y;
}
}