//#include <windows.h>
#include "dxlib.h"
#define CX 1024
#define CY 768
#define BPP 16
#define WHITE GetColor( 255, 255, 255 )
#define GRAY(a) GetColor( a, a, a )
#define ACCELL 7.5
#define GRAVITY 15.0
#define CSLOPE 7.5
#define RESIST 2.5
#define JUMP 300.0
#define FERROR 2.5
#define PSIZE 8
double GetSlope( VECTOR* fdata, int numdata, int x );
double GetFloor( VECTOR* fdata, int numdata, int x );
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
SetGraphMode( CX, CY, BPP );
ChangeWindowMode( TRUE );
if( DxLib_Init() == -1 ) return -1;
SetDrawScreen( DX_SCREEN_BACK );
VECTOR floors[] = { { 0, 64, 0 }, { 100, 64, 0 }, { 200, 96, 0 }, { 250, 104, 0 },
{ 500, 32, 0 }, { 800, 32, 0 }, { 1000, 256, 0 }, { 1500, 128, 0 }, { 2000, 64, 0 },
{ 2500, 96, 0 }, { 3000, 128, 0 }, { 3200, 160, 0 }, { 3400, 10, 0 }, { 5000, 64, 0 } };
int elements = sizeof(floors) / sizeof(VECTOR);
VECTOR player = { 50, 100, 0 };
VECTOR vec = { 0, 0 };
int cam_x;
double floor_y;
int pre_time, cur_time;
double past_time;
int bOnTheFloor = FALSE;
int bDrawAll = TRUE;
int bPreDKey = FALSE;
int bPreSPKey = FALSE;
int i;
pre_time = GetCurrentTime();
while ( ProcessMessage() != -1 ) {
if ( CheckHitKey( KEY_INPUT_ESCAPE ) )
break;
if ( CheckHitKey( KEY_INPUT_D ) ) {
if ( !bPreDKey )
bDrawAll = !bDrawAll;
bPreDKey = TRUE;
}
else
bPreDKey = FALSE;
cur_time = GetCurrentTime();
past_time = (double)( cur_time - pre_time) / 1000.0;
pre_time = cur_time;
if ( CheckHitKey( KEY_INPUT_RIGHT ) ) vec.x += ( bOnTheFloor ? ACCELL : (ACCELL * 0.25) );
if ( CheckHitKey( KEY_INPUT_LEFT ) ) vec.x -= ( bOnTheFloor ? ACCELL : (ACCELL * 0.25) );
if ( bOnTheFloor ) {
vec.x -= GetSlope( floors, elements, player.x ) * CSLOPE;
if ( GetSlope( floors, elements, player.x ) * vec.x < 0.0 )
vec.y -= abs( GetSlope( floors, elements, player.x ) * 500 );
}
if ( vec.x > 0.0 ) {
if ( ( vec.x -= ( bOnTheFloor ? RESIST : (RESIST * 0.25) ) ) < 0.0 ) vec.x = 0.0;
}
if ( vec.x < 0.0 ) {
if ( ( vec.x += ( bOnTheFloor ? RESIST : (RESIST * 0.25) ) ) > 0.0 ) vec.x = 0.0;
}
if ( CheckHitKey( KEY_INPUT_SPACE ) ) {
if ( !bPreSPKey && bOnTheFloor )
vec.y = JUMP;
bPreSPKey = TRUE;
}
else
bPreSPKey = FALSE;
player.x += vec.x * past_time;
player.y += vec.y * past_time;
vec.y -= GRAVITY;
floor_y = GetFloor( floors, elements, player.x );
if ( floor_y + FERROR > player.y ) {
player.y = floor_y;
vec.y = 0.0;
bOnTheFloor = TRUE;
}
else {
bOnTheFloor = FALSE;
}
cam_x = player.x - (CX >> 1);
ClearDrawScreen();
for ( i = 0; i < elements - 1; i++ ) {
DrawLine( floors[i].x - cam_x, CY - floors[i].y, floors[i+1].x - cam_x, CY - floors[i+1].y, WHITE );
}
if ( bDrawAll ) {
for ( i = floors[0].x; i < floors[elements - 1].x; i++ ) {
DrawLine( i - cam_x, CY - GetFloor( floors, elements, i ), i - cam_x, CY, GRAY( 64 ) );
}
}
DrawBox( player.x - PSIZE - cam_x, CY - player.y - (PSIZE << 1), player.x + PSIZE - cam_x, CY - player.y, GetColor( 128, 128, 255 ), TRUE );
DrawFormatString( 0, 0, WHITE, "← →で移動" );
DrawFormatString( 0, 24, WHITE, "スペースキーでジャンプ" );
DrawFormatString( 0, 48, WHITE, "重い時は\"D\"キーで地面非表示" );
ScreenFlip();
}
DxLib_End();
return 0;
}
double GetSlope( VECTOR* fdata, int numdata, int x ) {
double slope;
int i;
for ( i = 0; i < numdata - 1; i++ ) {
if ( fdata[i].x <= x && x < fdata[i+1].x )
break;
}
if ( i >= numdata - 1 )
slope = 0;
else
slope = (double)(fdata[i+1].y - fdata[i].y) / (double)(fdata[i+1].x - fdata[i].x);
return slope;
}
double GetFloor( VECTOR* fdata, int numdata, int x ) {
double floor_y;
int i;
for ( i = 0; i < numdata - 1; i++ ) {
if ( fdata[i].x <= x && x < fdata[i+1].x )
break;
}
if ( i >= numdata - 1 )
floor_y = -1000;
else
floor_y = (double)(fdata[i+1].y - fdata[i].y) / (double)(fdata[i+1].x - fdata[i].x) * (double)(x - fdata[i].x) + fdata[i].y;
return floor_y;
}
Ly8jaW5jbHVkZSA8d2luZG93cy5oPgojaW5jbHVkZSAiZHhsaWIuaCIKIAojZGVmaW5lIENYICAgICAgICAgICAgICAgICAgICAgIDEwMjQKI2RlZmluZSBDWSAgICAgICAgICAgICAgICAgICAgICA3NjgKI2RlZmluZSBCUFAgICAgICAgICAgICAgICAgICAgICAxNgojZGVmaW5lIFdISVRFICAgICAgICAgICBHZXRDb2xvciggMjU1LCAyNTUsIDI1NSApCiNkZWZpbmUgR1JBWShhKSAgICAgICAgIEdldENvbG9yKCBhLCBhLCBhICkKIAojZGVmaW5lIEFDQ0VMTCAgICAgICAgICA3LjUKI2RlZmluZSBHUkFWSVRZICAgICAgICAgMTUuMAojZGVmaW5lIENTTE9QRSAgICAgICAgICA3LjUKI2RlZmluZSBSRVNJU1QgICAgICAgICAgMi41CiNkZWZpbmUgSlVNUCAgICAgICAgICAgIDMwMC4wCiNkZWZpbmUgRkVSUk9SICAgICAgICAgIDIuNQojZGVmaW5lIFBTSVpFICAgICAgICAgICA4CiAKZG91YmxlIEdldFNsb3BlKCBWRUNUT1IqIGZkYXRhLCBpbnQgbnVtZGF0YSwgaW50IHggKTsKZG91YmxlIEdldEZsb29yKCBWRUNUT1IqIGZkYXRhLCBpbnQgbnVtZGF0YSwgaW50IHggKTsKIAppbnQgV0lOQVBJIFdpbk1haW4oSElOU1RBTkNFIGhJbnN0YW5jZSwgS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ICAgICAgaW50IGJQcmVTUEtleSA9IEZBTFNFOwoJCWludCBpOwogCiAgICAgICAgcHJlX3RpbWUgPSBHZXRDdXJyZW50VGltZSgpOwogICAgICAgIHdoaWxlICggUHJvY2Vzc01lc3NhZ2UoKSAhPSAtMSApICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAoIENoZWNrSGl0S2V5KCBLRVlfSU5QVVRfRVNDQVBFICkgKQogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIGlmICggQ2hlY2tIaXRLZXkoIEtFWV9JTlBVVF9EICkgKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmICggIWJQcmVES2V5ICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBiRHJhd0FsbCA9ICFiRHJhd0FsbDsKICAgICAgICAgICAgICAgICAgICAgICAgYlByZURLZXkgPSBUUlVFOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgICAgICBiUHJlREtleSA9IEZBTFNFOwogICAgICAgICAgICAgICAgY3VyX3RpbWUgID0gR2V0Q3VycmVudFRpbWUoKTsKICAgICAgICAgICAgICAgIHBhc3RfdGltZSA9IChkb3VibGUpKCBjdXJfdGltZSAtIHByZV90aW1lKSAvIDEwMDAuMDsKICAgICAgICAgICAgICAgIHByZV90aW1lICA9IGN1cl90aW1lOwogCiAgICAgICAgICAgICAgICBpZiAoIENoZWNrSGl0S2V5KCBLRVlfSU5QVVRfUklHSFQgKSApICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB2ZWMueCArPSAoIGJPblRoZUZsb29yID8gQUNDRUxMIDogKEFDQ0VMTCAqIDAuMjUpICk7CiAgICAgICAgICAgICAgICBpZiAoIENoZWNrSGl0S2V5KCBLRVlfSU5QVVRfTEVGVCApICkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB2ZWMueCAtPSAoIGJPblRoZUZsb29yID8gQUNDRUxMIDogKEFDQ0VMTCAqIDAuMjUpICk7CiAgICAgICAgICAgICAgICBpZiAoIGJPblRoZUZsb29yICkgewogICAgICAgICAgICAgICAgICAgICAgICB2ZWMueCAtPSBHZXRTbG9wZSggZmxvb3JzLCBlbGVtZW50cywgcGxheWVyLnggKSAqIENTTE9QRTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKCBHZXRTbG9wZSggZmxvb3JzLCBlbGVtZW50cywgcGxheWVyLnggKSAqIHZlYy54IDwgMC4wICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB2ZWMueSAtPSBhYnMoIEdldFNsb3BlKCBmbG9vcnMsIGVsZW1lbnRzLCBwbGF5ZXIueCApICogNTAwICk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoIHZlYy54ID4gMC4wICkgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoICggdmVjLnggLT0gKCBiT25UaGVGbG9vciA/IFJFU0lTVCA6IChSRVNJU1QgKiAwLjI1KSApICkgPCAwLjAgKSAgICB2ZWMueCA9IDAuMDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmICggdmVjLnggPCAwLjAgKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmICggKCB2ZWMueCArPSAoIGJPblRoZUZsb29yID8gUkVTSVNUIDogKFJFU0lTVCAqIDAuMjUpICkgKSA+IDAuMCApICAgIHZlYy54ID0gMC4wOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBpZiAoIENoZWNrSGl0S2V5KCBLRVlfSU5QVVRfU1BBQ0UgKSApIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKCAhYlByZVNQS2V5ICYmIGJPblRoZUZsb29yICkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB2ZWMueSA9IEpVTVA7CiAgICAgICAgICAgICAgICAgICAgICAgIGJQcmVTUEtleSA9IFRSVUU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgIGJQcmVTUEtleSA9IEZBTFNFOwogICAgICAgICAgICAgICAgcGxheWVyLnggKz0gdmVjLnggKiBwYXN0X3RpbWU7CiAgICAgICAgICAgICAgICBwbGF5ZXIueSArPSB2ZWMueSAqIHBhc3RfdGltZTsKIAogICAgICAgICAgICAgICAgdmVjLnkgLT0gR1JBVklUWTsKIAogICAgICAgICAgICAgICAgZmxvb3JfeSA9IEdldEZsb29yKCBmbG9vcnMsIGVsZW1lbnRzLCBwbGF5ZXIueCApOwogICAgICAgICAgICAgICAgaWYgKCBmbG9vcl95ICsgRkVSUk9SID4gcGxheWVyLnkgKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBsYXllci55ID0gZmxvb3JfeTsKICAgICAgICAgICAgICAgICAgICAgICAgdmVjLnkgPSAwLjA7CiAgICAgICAgICAgICAgICAgICAgICAgIGJPblRoZUZsb29yID0gVFJVRTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBiT25UaGVGbG9vciA9IEZBTFNFOwogICAgICAgICAgICAgICAgfQogCiAgICAgICAgICAgICAgICBjYW1feCA9IHBsYXllci54IC0gKENYID4+IDEpOwogCiAgICAgICAgICAgICAgICBDbGVhckRyYXdTY3JlZW4oKTsKIAogICAgICAgICAgICAgICAgZm9yICggaSA9IDA7IGkgPCBlbGVtZW50cyAtIDE7IGkrKyApIHsKICAgICAgICAgICAgICAgICAgICAgICAgRHJhd0xpbmUoIGZsb29yc1tpXS54IC0gY2FtX3gsIENZIC0gZmxvb3JzW2ldLnksIGZsb29yc1tpKzFdLnggLSBjYW1feCwgQ1kgLSBmbG9vcnNbaSsxXS55LCBXSElURSApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKCBiRHJhd0FsbCApIHsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yICggaSA9IGZsb29yc1swXS54OyBpIDwgZmxvb3JzW2VsZW1lbnRzIC0gMV0ueDsgaSsrICkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERyYXdMaW5lKCBpIC0gY2FtX3gsIENZIC0gR2V0Rmxvb3IoIGZsb29ycywgZWxlbWVudHMsIGkgKSwgaSAtIGNhbV94LCBDWSwgR1JBWSggNjQgKSApOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBEcmF3Qm94KCBwbGF5ZXIueCAtIFBTSVpFIC0gY2FtX3gsIENZIC0gcGxheWVyLnkgLSAoUFNJWkUgPDwgMSksIHBsYXllci54ICsgUFNJWkUgLSBjYW1feCwgQ1kgLSBwbGF5ZXIueSwgR2V0Q29sb3IoIDEyOCwgMTI4LCAyNTUgKSwgVFJVRSApOwogCiAgICAgICAgICAgICAgICBEcmF3Rm9ybWF0U3RyaW5nKCAwLCAwLCBXSElURSwgIuKGkCDihpLjgafnp7vli5UiICk7CiAgICAgICAgICAgICAgICBEcmF3Rm9ybWF0U3RyaW5nKCAwLCAyNCwgV0hJVEUsICLjgrnjg5rjg7zjgrnjgq3jg7zjgafjgrjjg6Pjg7Pjg5ciICk7CiAgICAgICAgICAgICAgICBEcmF3Rm9ybWF0U3RyaW5nKCAwLCA0OCwgV0hJVEUsICLph43jgYTmmYLjga9cIkRcIuOCreODvOOBp+WcsOmdoumdnuihqOekuiIgKTsKIAogICAgICAgICAgICAgICAgU2NyZWVuRmxpcCgpOwogICAgICAgIH0KIAogICAgICAgIER4TGliX0VuZCgpOwogCiAgICAgICAgcmV0dXJuIDA7Cn0KIApkb3VibGUgR2V0U2xvcGUoIFZFQ1RPUiogZmRhdGEsIGludCBudW1kYXRhLCBpbnQgeCApIHsKICAgICAgICBkb3VibGUgc2xvcGU7CgkJaW50IGk7CiAKICAgICAgICBmb3IgKCBpID0gMDsgaSA8IG51bWRhdGEgLSAxOyBpKysgKSB7CiAgICAgICAgICAgICAgICBpZiAoIGZkYXRhW2ldLnggPD0geCAmJiB4IDwgZmRhdGFbaSsxXS54ICkKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgICAgIGlmICggaSA+PSBudW1kYXRhIC0gMSApCiAgICAgICAgICAgICAgICBzbG9wZSA9IDA7CiAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgc2xvcGUgPSAoZG91YmxlKShmZGF0YVtpKzFdLnkgLSBmZGF0YVtpXS55KSAvIChkb3VibGUpKGZkYXRhW2krMV0ueCAtIGZkYXRhW2ldLngpOwogCiAgICAgICAgcmV0dXJuIHNsb3BlOwp9CiAKZG91YmxlIEdldEZsb29yKCBWRUNUT1IqIGZkYXRhLCBpbnQgbnVtZGF0YSwgaW50IHggKSB7CiAgICAgICAgZG91YmxlIGZsb29yX3k7CgkJaW50IGk7CiAKICAgICAgICBmb3IgKCBpID0gMDsgaSA8IG51bWRhdGEgLSAxOyBpKysgKSB7CiAgICAgICAgICAgICAgICBpZiAoIGZkYXRhW2ldLnggPD0geCAmJiB4IDwgZmRhdGFbaSsxXS54ICkKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgICAgIGlmICggaSA+PSBudW1kYXRhIC0gMSApCiAgICAgICAgICAgICAgICBmbG9vcl95ID0gLTEwMDA7CiAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgZmxvb3JfeSA9IChkb3VibGUpKGZkYXRhW2krMV0ueSAtIGZkYXRhW2ldLnkpIC8gKGRvdWJsZSkoZmRhdGFbaSsxXS54IC0gZmRhdGFbaV0ueCkgKiAoZG91YmxlKSh4IC0gZmRhdGFbaV0ueCkgKyBmZGF0YVtpXS55OwogCiAgICAgICAgcmV0dXJuIGZsb29yX3k7Cn0=