aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-10-28 23:45:34 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-10-28 23:45:34 +0700
commitbd8985065f2377a62f0ee9f4236d5cd9ad99dfe4 (patch)
treecee76279026bbba88fededfc9474c8eba67d5c2d /src
parent036ab1d1051b54e71f80fbb791bf69bd7c236f62 (diff)
downloadblackshades-bd8985065f2377a62f0ee9f4236d5cd9ad99dfe4.tar.gz
Reorganize key mappings
Diffstat (limited to 'src')
-rw-r--r--src/GameInitDispose.cpp16
-rw-r--r--src/GameLoop.cpp10
-rw-r--r--src/GameTick.cpp7
-rw-r--r--src/Globals.cpp9
-rw-r--r--src/Person.cpp207
-rw-r--r--src/Person.h1
-rw-r--r--src/Skeleton.h2
-rw-r--r--src/Support.cpp9
-rw-r--r--src/Support.h13
-rw-r--r--src/config.h10
-rw-r--r--src/config.zig3
-rw-r--r--src/misc.h1
-rw-r--r--src/misc.zig9
13 files changed, 110 insertions, 187 deletions
diff --git a/src/GameInitDispose.cpp b/src/GameInitDispose.cpp
index c4e1518..57dca01 100644
--- a/src/GameInitDispose.cpp
+++ b/src/GameInitDispose.cpp
@@ -54,13 +54,7 @@ extern float precipitationvert;
extern float precipitationdensity;
extern float soundscalefactor;
extern int slomo;
-extern int forwardskey;
-extern int backwardskey;
-extern int leftkey;
-extern int rightkey;
-extern int aimkey;
-extern int psychicaimkey;
-extern int psychickey;
+extern struct Key keymap;
void resizeWindow(Game* game, int width, int height)
{
@@ -80,13 +74,7 @@ Game* makeGame(Config config, Scores scores)
game->musictoggle = config.music;
game->mouse_sensitivity = config.mouse_sensitivity;
- forwardskey = config.key.forwards;
- backwardskey = config.key.backwards;
- leftkey = config.key.left;
- rightkey = config.key.right;
- aimkey = config.key.aim;
- psychicaimkey = config.key.psychic_aim;
- psychickey = config.key.psychic;
+ keymap = config.key;
game->levels = config.levels.ptr;
game->nummissions = config.levels.len;
diff --git a/src/GameLoop.cpp b/src/GameLoop.cpp
index 06d4e90..8a1af59 100644
--- a/src/GameLoop.cpp
+++ b/src/GameLoop.cpp
@@ -32,9 +32,7 @@ extern float rad2deg;
extern Fog fog;
extern int environment;
extern int slomo;
-extern int aimkey;
-extern int psychicaimkey;
-extern int psychickey;
+extern struct Key keymap;
void handleKey(Game* game, int key, int action, int mods)
{
@@ -81,9 +79,9 @@ void handleKey(Game* game, int key, int action, int mods)
&& player.reloads[player.whichgun] > 0
&& player.reloading <= 0)
player.ammo = -1;
- } else if (key == aimkey) {
+ } else if (key == keymap.aim) {
player.aiming ^= 1;
- } else if (key == psychicaimkey) {
+ } else if (key == keymap.psychic_aim) {
// Picking up gun from rag doll
if (player.currentanimation == crouchanim
&& (!player.aiming || player.whichgun == nogun
@@ -125,7 +123,7 @@ void handleKey(Game* game, int key, int action, int mods)
alSourcef(gSourceID[shootsong], AL_PITCH, 0.5f);
alSourcef(gSourceID[zombiesong], AL_PITCH, 0.5f);
}
- } else if (key == psychickey) {
+ } else if (key == keymap.psychic) {
if (visions ^= 1) {
game->flashamount = game->flashr = 1;
game->flashg = game->flashb = 0;
diff --git a/src/GameTick.cpp b/src/GameTick.cpp
index 42af2a6..f3ce015 100644
--- a/src/GameTick.cpp
+++ b/src/GameTick.cpp
@@ -43,13 +43,6 @@ extern float snowdelay;
extern float precipitationdensity;
extern float soundscalefactor;
extern int slomo;
-extern int forwardskey;
-extern int backwardskey;
-extern int leftkey;
-extern int rightkey;
-extern int aimkey;
-extern int psychicaimkey;
-extern int psychickey;
extern Decals decals;
#define maxfallvel 40
diff --git a/src/Globals.cpp b/src/Globals.cpp
index dba8ad5..a22955b 100644
--- a/src/Globals.cpp
+++ b/src/Globals.cpp
@@ -3,6 +3,7 @@
#include "Models.h"
#include "Person.h"
#include "Game.h"
+#include "config.h"
float sinefluct;
float sinefluctprog;
@@ -40,13 +41,7 @@ float camerashake;
Fog fog;
-int forwardskey;
-int backwardskey;
-int leftkey;
-int rightkey;
-int aimkey;
-int psychicaimkey;
-int psychickey;
+struct Key keymap;
bool blood;
diff --git a/src/Person.cpp b/src/Person.cpp
index 3789865..d096b1d 100644
--- a/src/Person.cpp
+++ b/src/Person.cpp
@@ -23,6 +23,7 @@
#include "Person.h"
#include "Support.h"
+#include "config.h"
#include "misc.h"
extern float multiplier;
@@ -38,13 +39,7 @@ extern Sprites sprites;
extern float soundscalefactor;
extern int slomo;
-extern int forwardskey;
-extern int backwardskey;
-extern int leftkey;
-extern int rightkey;
-extern int aimkey;
-extern int psychicaimkey;
-extern int psychickey;
+extern struct Key keymap;
extern Model skeletonmodels[10];
extern Costume costume[10];
@@ -629,136 +624,100 @@ void Person::DoAnimationslite(int who)
currentframe = targetframe;
}
-void Person::DoStuff(int who){
- int moveanim;
+void Person::control()
+{
+ if (visions)
+ speed = 40;
+ else
+ speed = (targetanimation == joganim) ? 2.2 : 2.5;
- if((targetanimation!=idleanim&&targetanimation!=crouchanim)||visions==1)playerlowrotation=playerrotation;
- if(targetanimation!=crouchanim&&currentanimation!=crouchanim){
- if(playerrotation>playerlowrotation+50){playerlowrotation=playerrotation-50; targetanimation=walkanim; targetframe=0; target=0;}
- if(playerrotation<playerlowrotation-50){playerlowrotation=playerrotation+50; targetanimation=walkanim; targetframe=0; target=0;}
+ if (keyPress(GLFW_KEY_LEFT_CONTROL)
+ && currentanimation == idleanim
+ && targetanimation == idleanim) {
+ targetanimation = crouchanim;
+ target = 0;
}
- if(targetanimation==crouchanim||currentanimation==crouchanim){
- if(playerrotation>playerlowrotation+70){playerrotation=playerlowrotation+70;}
- if(playerrotation<playerlowrotation-70){playerrotation=playerlowrotation-70;}
+ if (!keyPress(GLFW_KEY_LEFT_CONTROL)
+ && currentanimation == crouchanim
+ && targetanimation == crouchanim) {
+ targetanimation = idleanim;
+ target = 0;
}
- if(who==0)camera.rotation=180-playerrotation;
- if(who!=0&&visions==0){
- if(targetanimation!=walkanim&&targetanimation!=zombiewalkanim)speed=1.0*speedmult;
- if(targetanimation==walkanim||targetanimation==zombiewalkanim)speed=1.8*speedmult;
- playerlowrotation=playerrotation;
+ auto forwards = keyPress(keymap.forwards) - keyPress(keymap.backwards);
+ auto right = keyPress(keymap.right) - keyPress(keymap.left);
+ if ((onground || visions) && currentanimation != crouchanim) {
+ playerlowrotation -= right * (forwards ? forwards * 45 : 90);
+ backwardsanim = forwards < 0;
+ auto moveanim = (keyPress(GLFW_KEY_LEFT_SHIFT) || visions)
+ ? joganim : walkanim; // Should jog be the default?
+ if (forwards || right) {
+ if (targetanimation != moveanim) {
+ targetanimation = moveanim;
+ targetframe = 0;
+ }
+ } else {
+ if (targetanimation == joganim
+ || (targetanimation == walkanim
+ && targetframe != 0 && targetframe != 2)) {
+ targetanimation = idleanim;
+ targetframe = 0;
+ target = 0;
+ }
+ }
}
- //Do controls
- if(who==0&&targetanimation!=diveanim&&targetanimation!=throwanim&&targetanimation!=thrownanim&&currentanimation!=diveanim&&currentanimation!=getupfrontanim){
- backwardsanim=0;
- if(visions==1)speed=40;
- if(visions==0&&targetanimation==joganim)speed=2.2;
- if(visions==0&&targetanimation!=joganim)speed=1.3;
- if(visions==0&&targetanimation==walkanim)speed=2.5;
+ // Air? What does this even do?
+ if (!onground && (forwards || right)) {
+ auto rotation = playerrotation
+ - right * (forwards ? forwards * 45 : 90);
+ facing = DoRotation({0, 0, 1}, 0, rotation, 0);
+ velocity += facing * multiplier * 4;
+ }
+}
- if (IsKeyDown(GLFW_KEY_LEFT_SHIFT) || visions == 1)
- moveanim = joganim;
- else
- moveanim=walkanim;
- if (IsKeyDown(GLFW_KEY_LEFT_CONTROL)
- && currentanimation == idleanim
- && targetanimation == idleanim) {
- targetanimation = crouchanim;
+void Person::DoStuff(int who)
+{
+ if (targetanimation != idleanim && targetanimation != crouchanim || visions)
+ playerlowrotation = playerrotation;
+ if (targetanimation != crouchanim && currentanimation != crouchanim) {
+ if (playerrotation > playerlowrotation + 50) {
+ playerlowrotation = playerrotation - 50;
+ targetanimation = walkanim;
+ targetframe = 0;
target = 0;
- }
- if (!IsKeyDown(GLFW_KEY_LEFT_CONTROL)
- && currentanimation == crouchanim
- && targetanimation == crouchanim) {
- targetanimation = idleanim;
+ } else if (playerrotation < playerlowrotation - 50) {
+ playerlowrotation = playerrotation + 50;
+ targetanimation = walkanim;
+ targetframe = 0;
target = 0;
}
- if((onground||(who==0&&visions==1))&&currentanimation!=crouchanim){
- if (IsKeyDown(forwardskey) && !IsKeyDown(backwardskey)) {
- if(targetanimation!=moveanim)targetframe=0;
- targetanimation=moveanim;
- }
- if (IsKeyDown(rightkey) && !IsKeyDown(leftkey)) {
- if(targetanimation!=moveanim)targetframe=0;
- targetanimation=moveanim;
- playerlowrotation-=90;
- if (IsKeyDown(forwardskey))
- playerlowrotation += 45;
- if (IsKeyDown(backwardskey))
- playerlowrotation -= 235;
- }
- if (IsKeyDown(leftkey) && !IsKeyDown(rightkey)) {
- if(targetanimation!=moveanim)targetframe=0;
- targetanimation=moveanim;
- playerlowrotation+=90;
- if (IsKeyDown(forwardskey))
- playerlowrotation -= 45;
- if (IsKeyDown(backwardskey))
- playerlowrotation += 235;
- }
- if (IsKeyDown(backwardskey)) {
- if(targetanimation!=moveanim)targetframe=0;
- targetanimation=moveanim;
- backwardsanim=1;
- }
- }
- //air control
- if(!onground){
- float oldplayerrotation=playerrotation;
- if (IsKeyDown(forwardskey) && !IsKeyDown(backwardskey)) {
- facing=0;
- facing.z=1;
- facing=DoRotation(facing,0,playerrotation,0);
- velocity=velocity+facing*multiplier*4;
- }
- if (IsKeyDown(rightkey) && !IsKeyDown(leftkey)) {
- playerrotation-=90;
- if (IsKeyDown(forwardskey))
- playerrotation += 45;
- if (IsKeyDown(backwardskey))
- playerrotation -= 45;
- facing=0;
- facing.z=1;
- facing=DoRotation(facing,0,playerrotation,0);
- velocity=velocity+facing*multiplier*4;
- }
- if (IsKeyDown(leftkey) && !IsKeyDown(rightkey)) {
- playerrotation+=90;
- if (IsKeyDown(forwardskey))
- playerrotation -= 45;
- if (IsKeyDown(backwardskey))
- playerrotation += 45;
- facing=0;
- facing.z=1;
- facing=DoRotation(facing,0,playerrotation,0);
- velocity=velocity+facing*multiplier*4;
- }
- if (IsKeyDown(backwardskey) && !IsKeyDown(forwardskey)
- && !IsKeyDown(leftkey) && !IsKeyDown(rightkey)) {
- playerrotation+=180;
- facing=0;
- facing.z=1;
- facing=DoRotation(facing,0,playerrotation,0);
- velocity=velocity+facing*multiplier*4;
- }
- playerrotation=oldplayerrotation;
- }
- if (!IsKeyDown(forwardskey) && !IsKeyDown(leftkey)
- && !IsKeyDown(rightkey) && !IsKeyDown(backwardskey)
- && (targetanimation==joganim||targetanimation==walkanim)) {
- if(!(targetanimation==walkanim&&(targetframe==0||targetframe==2))){
- targetanimation=idleanim;
- targetframe=0;
- target=0;
- }
- }
+ } else if (playerrotation > playerlowrotation + 70) {
+ playerrotation = playerlowrotation + 70;
+ } else if (playerrotation < playerlowrotation - 70) {
+ playerrotation = playerlowrotation - 70;
}
- facing=0;
- facing.z=1;
+ if (who == 0)
+ camera.rotation = 180 - playerrotation;
+
+ if(who!=0&&visions==0){
+ if(targetanimation!=walkanim&&targetanimation!=zombiewalkanim)speed=1.0*speedmult;
+ if(targetanimation==walkanim||targetanimation==zombiewalkanim)speed=1.8*speedmult;
+ playerlowrotation=playerrotation;
+ }
- facing=DoRotation(facing,0,playerlowrotation,0);
- if(backwardsanim)facing*=-1;
+ // Do controls
+ if (who == 0 && targetanimation != diveanim
+ && targetanimation != throwanim
+ && targetanimation != thrownanim
+ && currentanimation != diveanim
+ && currentanimation != getupfrontanim)
+ control();
+
+ facing = DoRotation({0, 0, 1}, 0, playerlowrotation, 0);
+ if (backwardsanim)
+ facing *= -1;
if(onground){
velocity=0;
diff --git a/src/Person.h b/src/Person.h
index 840876d..516ecf9 100644
--- a/src/Person.h
+++ b/src/Person.h
@@ -126,6 +126,7 @@ class Person
void FindRotationGun(XYZ start, XYZ target);
int DrawSkeleton(int who);
+ void control();
void DoStuff(int who);
void DoAnimations(int who);
void DoAnimationslite(int who);
diff --git a/src/Skeleton.h b/src/Skeleton.h
index 7687610..c8c41df 100644
--- a/src/Skeleton.h
+++ b/src/Skeleton.h
@@ -82,7 +82,7 @@ class Muscle
class Animation
{
public:
- size_t numframes;
+ int numframes;
bool canbeoverridden;
bool ismodified[max_joints][max_frames];
XYZ position[max_joints][max_frames];
diff --git a/src/Support.cpp b/src/Support.cpp
index 850d0bb..2f24e94 100644
--- a/src/Support.cpp
+++ b/src/Support.cpp
@@ -1,17 +1,8 @@
-#include <stdbool.h>
#include <stdlib.h>
-#include <GLFW/glfw3.h>
-
#include "Support.h"
int Random()
{
return (rand() % 65535) - 32767;
}
-
-bool IsKeyDown(int key)
-{
- const auto window = glfwGetCurrentContext();
- return glfwGetKey(window, key) == GLFW_PRESS;
-}
diff --git a/src/Support.h b/src/Support.h
index 8fbe1d8..a5683ec 100644
--- a/src/Support.h
+++ b/src/Support.h
@@ -1,17 +1,4 @@
#ifndef SUPPORT_H
#define SUPPORT_H
-
-#include <stdio.h>
-#include <unistd.h>
-
-#define fsFromStart SEEK_SET
-
-typedef char* Str255;
-typedef int OSErr;
-
-#define SetFPos(fildes, whence, offset) lseek(fildes, offset, whence)
-#define FSClose(fildes) close(fildes)
-
int Random();
-bool IsKeyDown(int key);
#endif
diff --git a/src/config.h b/src/config.h
index f4ed20d..486a4f6 100644
--- a/src/config.h
+++ b/src/config.h
@@ -14,6 +14,11 @@ struct Level {
float difficulty;
};
+struct Key {
+ int forwards, backwards, left, right;
+ int aim, psychic_aim, psychic;
+};
+
struct Config {
int width;
int height;
@@ -24,10 +29,7 @@ struct Config {
bool music;
float mouse_sensitivity;
- struct {
- int forwards, backwards, left, right;
- int aim, psychic_aim, psychic;
- } key;
+ struct Key key;
struct {
struct Level *ptr;
diff --git a/src/config.zig b/src/config.zig
index 64785f8..eec36d7 100644
--- a/src/config.zig
+++ b/src/config.zig
@@ -18,13 +18,12 @@
const Dir = std.fs.Dir;
const File = std.fs.File;
-const Key = gfz.Key;
+const Key = @import("gfz").Key;
const allocator = std.heap.c_allocator;
const c = @import("main.zig").c;
const cwd = std.fs.cwd;
const data_dir = @import("build_options").data_dir;
const eql = std.mem.eql;
-const gfz = @import("gfz");
const ini = @import("ini");
const join = std.fs.path.join;
const maxInt = std.math.maxInt;
diff --git a/src/misc.h b/src/misc.h
index be5570a..7a377f6 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -55,6 +55,7 @@ struct Scores {
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
+ bool keyPress(int key);
struct AnimationData loadAnimation(const char*);
void loadJoints(struct JointData*);
struct ModelData loadModel(const char*);
diff --git a/src/misc.zig b/src/misc.zig
index e30c154..678538b 100644
--- a/src/misc.zig
+++ b/src/misc.zig
@@ -25,6 +25,8 @@ usingnamespace @cImport({
const Dir = std.fs.Dir;
const TokenIterator = std.mem.TokenIterator;
+const Key = gf.Key;
+const Window = gf.Window;
const al = @import("zeal");
const allocPrint = std.fmt.allocPrint;
const allocator = std.heap.c_allocator;
@@ -35,6 +37,7 @@ const data_dir = @import("build_options").data_dir ++ [_]u8{ sep };
const endsWith = std.mem.endsWith;
const eql = std.mem.eql;
const free = std.c.free;
+const gf = @import("gfz");
const ini = @import("ini");
const join = std.fs.path.joinZ;
const hash = std.crypto.hash.Blake3.hash;
@@ -47,6 +50,12 @@ const startsWith = std.mem.startsWith;
const std = @import("std");
const tokenize = std.mem.tokenize;
+/// Return whether the given keyboard key is pressed.
+export fn keyPress(key: c_int) bool {
+ const window = (Window.getCurrent() catch unreachable).?;
+ return (window.getKey(@intToEnum(Key, key)) catch unreachable) == .press;
+}
+
/// Read given file to heap, allocated by C allocator.
fn readFile(dir: Dir, comptime fmt: []const u8, args: anytype) ![]const u8 {
const filename = try allocPrint(allocator, fmt, args);