forked from fyhamoeba/IncageMapCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSCheck.m
More file actions
87 lines (76 loc) · 1.92 KB
/
Copy pathJSCheck.m
File metadata and controls
87 lines (76 loc) · 1.92 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function [JSMoved, JSCode, JSVoltage, bug, keyCode, tNow] = JSCheck
global JSup JSdown JSleft JSright up down left right; % trans_data;
global bug;
global upKey downKey leftKey rightKey;
persistent JS_last_direction JS_start_time JS_duration_threshold;
bug = 0;
% Initialize duration tracking variables
if isempty(JS_last_direction)
JS_last_direction = 0; % 0 means no direction
JS_start_time = GetSecs; % record start time
JS_duration_threshold = 0.15; % threshold in seconds, adjustable
end
JSVoltage = [0,0,0,0]; % left, down, right, up
% check keyboard state
[~, tNow, keyCode] = KbCheck(-1);
% Reset direction states
[left,down,right,up]=deal(0);
% Detect current key state
current_time = tNow;
current_direction = 0;
if keyCode(downKey)
current_direction = 1; % down
elseif keyCode(upKey)
current_direction = 2; % up
elseif keyCode(leftKey)
current_direction = 3; % left
elseif keyCode(rightKey)
current_direction = 4; % right
end
% Check direction and duration
if current_direction == 0
% No direction detected, reset timer
JS_last_direction = 0;
JS_start_time = current_time;
elseif current_direction == JS_last_direction
% Same direction, check duration
if (current_time - JS_start_time) >= JS_duration_threshold
% Duration reached threshold, set direction
switch current_direction
case 1 % down
down = 1;
case 2 % up
up = 1;
case 3 % left
left = 1;
case 4 % right
right = 1;
end
end
% Duration not reached threshold, do nothing
else
% New direction detected, reset timer
JS_last_direction = current_direction;
JS_start_time = current_time;
end
%fyh--------------------------------------
dirCode = sum([1,3,5,7] .* [up,right,left,down]);
JSMoved = dirCode > 0;
JSCode = zeros(1,4);
if up
JSCode(JSup) = 1;
end
if right
JSCode(JSright) = 1;
end
if left
JSCode(JSleft) = 1;
end
if down
JSCode(JSdown) = 1;
end
% Bug detection
if dirCode == 4 || dirCode == 6 || dirCode > 7
bug = 1;
end
end