Skip to content

Commit

Permalink
Merge pull request #1 from ShaneMcKeon/master
Browse files Browse the repository at this point in the history
merge my work
  • Loading branch information
ShaneMcKeon authored Sep 18, 2024
2 parents b7bb52c + 5a618e2 commit b64b38f
Show file tree
Hide file tree
Showing 21 changed files with 980 additions and 155 deletions.
21 changes: 15 additions & 6 deletions habit.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,31 @@
system.w = setup_screen(varargin{:});
system.pos = setup_pos(system.w, varargin{:});
system.tex = load_textures(system.w, varargin{:});

%% instructions
correctTrials = 0;

[onset, output] = instructions(system, 1);
%% instructions
if 0
[onset, output] = instructions(system, 1);
end

%% start timing and data collection
record(length(timing)) = struct();
system.starttime = GetSecs();



%% run through events
for i=1:length(timing)
t = timing(i);
[onset, output] = t.func(system, t, record);
t = timing(i); % undoing the fixed onsets
if i == 1 || strcmp(t.event_name, 'isi') % want to move to isi immediatly after they click a choice.
t.onset = 0;
else
t.onset= timing(i-1).dur;
end
[onset, output] = t.func(system, t, record, correctTrials);
record(i).event_name = t.event_name;
record(i).output = output;
record(i).onset = onset;
fprintf('%s %f\n', t.event_name, onset)
end

info.record = record;
Expand Down
1 change: 1 addition & 0 deletions private/background.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ function background(system)
Screen('DrawTexture', system.w, system.tex.ocean_bottom, ...
[], [padding, 0, 558+padding 299] );


end
87 changes: 71 additions & 16 deletions private/choice.m
Original file line number Diff line number Diff line change
@@ -1,57 +1,112 @@
function [onset, output] = choice(system, t, varargin)

allkeys = [system.keys.left system.keys.up system.keys.right];
keys = [];

ideal = GetSecs()+t.onset;
background(system);
Screen('DrawTexture', system.w, system.tex.astronaut{1,1});
Screen('DrawTexture', system.w, system.tex.ocean_bottom); % Show the background again
Screen('DrawTexture', system.w, system.tex.astronaut{1,1},...
[], [system.pos.character.x system.pos.character.y system.pos.character.x+60 system.pos.character.y+80] );

progressBar(system, t);


if t.i < 3
correctTrials = 0;

else

correctTrials = varargin{1}(t.i-2).output.correctTrials;

end

totalCount(system, correctTrials);
coinPile(system, correctTrials)



%% positon choice options
chest_w = 27;chest_h = 27; %TODO: use sprite
chest_w = 60; chest_h = 60; %TODO: use sprite
% TODO: use DrawTextures (many at once)
keys = [];

[screenWidth, screenHeight] = Screen('WindowSize', system.w);

% Define the size of the white box (e.g., 100x100 pixels)
boxWidth = 200;
boxHeight = 200;

% Calculate the position of the box in the lower right corner
% The coordinates are in the form [left, top, right, bottom]
boxRect = [screenWidth - boxWidth, screenHeight - boxHeight, screenWidth, screenHeight];

% Define the color white (white = [255 255 255])
black = [0 0 0];

% Draw the white box
Screen('FillRect', system.w, black, boxRect);

% chest graphics
Screen('DrawTexture', system.w, system.tex.chest,...
Screen('DrawTexture', system.w, system.tex.chest_sprites{1,1},...
[], [ system.pos.left.x system.pos.left.y system.pos.left.x+chest_w system.pos.left.y+chest_h] );
Screen('DrawTexture', system.w, system.tex.chest,...
Screen('DrawTexture', system.w, system.tex.chest_sprites{1,1},...
[], [ system.pos.up.x system.pos.up.y system.pos.up.x+chest_w system.pos.up.y+chest_h] );
Screen('DrawTexture', system.w, system.tex.chest,...
Screen('DrawTexture', system.w, system.tex.chest_sprites{1,1},...
[], [ system.pos.right.x system.pos.right.y system.pos.right.x+chest_w system.pos.right.y+chest_h] );

% add keys to chests
if ismember('right', t.choices)
Screen('DrawTexture', system.w, system.tex.key,...
[], [ system.pos.right.x system.pos.right.y system.pos.right.x+chest_w system.pos.right.y+chest_h] );
keys = [keys, system.keys.right];
[], [ system.pos.right.x+20 system.pos.right.y+20 system.pos.right.x+chest_w system.pos.right.y+chest_h] );
keys = [keys system.keys.right];

end

if ismember('left', t.choices)
Screen('DrawTexture', system.w, system.tex.key,...
[], [ system.pos.left.x system.pos.left.y system.pos.left.x+chest_w system.pos.left.y+chest_h] );
keys = [keys, system.keys.left];
[], [ system.pos.left.x+20 system.pos.left.y+20 system.pos.left.x+chest_w system.pos.left.y+chest_h] );
keys = [keys system.keys.left];


end

if ismember('up', t.choices)
Screen('DrawTexture', system.w, system.tex.key,...
[], [ system.pos.up.x system.pos.up.y system.pos.up.x+chest_w system.pos.up.y+chest_h] );
keys = [keys, system.keys.up];
[], [ system.pos.up.x+20 system.pos.up.y+20 system.pos.up.x+chest_w system.pos.up.y+chest_h] );
keys = [keys system.keys.up];


end


onset = Screen('Flip', system.w, ideal);
sendDaqs(system);

[k rt] = waitForKeys(keys, onset + t.max_rt);

if rt > 0
idx = find(keys == k,1);
idx = find(allkeys == k,1);
fprintf('choice %d, key %d',idx, k)
well_prob = t.chance(idx);
output.score = (rand(1) <= well_prob);
if idx == 1
output.pick = 'left';

elseif idx == 2
output.pick = 'up';

elseif idx == 3
output.pick = 'right';

end
else
output.score = 0
output.score = 0;
output.pick = 'none';
end
oupput.onset_ideal = ideal;
output.onset_ideal = ideal;
output.key = k;
output.rt = rt;



% TODO: animate avatar walk in while loop?
end
28 changes: 28 additions & 0 deletions private/coinPile.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function coinPile(system, correctTrials)

[screenWidth, screenHeight] = Screen('WindowSize', system.w);


coinSize = 20; % 20x20 pixels

% Loop through and draw 'c' number of coins based on correct trials

% Parameters
coins_per_row = 20; % Number of coins per row
row_spacing = coinSize + 20; % Vertical spacing between rows
yPos_start = screenHeight / 3; % Initial vertical position
xPos_start = screenWidth / 3; % Initial horizontal position

% Redraw the static coins
for c = 1:(correctTrials)
% Calculate row and column positions
row = floor((c - 1) / coins_per_row); % Determine the current row
col = mod((c - 1), coins_per_row); % Determine the column within the row

% Update horizontal and vertical positions
xPos = xPos_start + col * (coinSize + 10); % Horizontal position
yPos = yPos_start - row * row_spacing; % Vertical position based on row number

% Draw the coin
Screen('DrawTexture', system.w, system.tex.starcoin, [], [xPos, yPos, xPos + coinSize, yPos + coinSize]);
end
56 changes: 56 additions & 0 deletions private/drawScreen.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

function drawScreen(system, t, color, varargin)

%redraw the scene
ideal = GetSecs()+t.onset;

%% positon choice options
chest_w = 60; chest_h = 60; %TODO: use sprite
% TODO: use DrawTextures (many at once)

[screenWidth, screenHeight] = Screen('WindowSize', system.w);

% Define the size of the white box (e.g., 100x100 pixels)
boxWidth = 200;
boxHeight = 200;

% Calculate the position of the box in the lower right corner
% The coordinates are in the form [left, top, right, bottom]
boxRect = [screenWidth - boxWidth, screenHeight - boxHeight, screenWidth, screenHeight];

Screen('DrawTexture', system.w, system.tex.ocean_bottom); % Show the background again

% Draw the box
Screen('FillRect', system.w, color, boxRect);

% chest graphics
Screen('DrawTexture', system.w, system.tex.chest_sprites{1,1},...
[], [ system.pos.left.x system.pos.left.y system.pos.left.x+chest_w system.pos.left.y+chest_h] );
Screen('DrawTexture', system.w, system.tex.chest_sprites{1,1},...
[], [ system.pos.up.x system.pos.up.y system.pos.up.x+chest_w system.pos.up.y+chest_h] );
Screen('DrawTexture', system.w, system.tex.chest_sprites{1,1},...
[], [ system.pos.right.x system.pos.right.y system.pos.right.x+chest_w system.pos.right.y+chest_h] );

% add keys to chests
if ismember('right', t.choices)
Screen('DrawTexture', system.w, system.tex.key,...
[], [ system.pos.right.x+20 system.pos.right.y+20 system.pos.right.x+chest_w system.pos.right.y+chest_h] );
end

if ismember('left', t.choices)
Screen('DrawTexture', system.w, system.tex.key,...
[], [ system.pos.left.x+20 system.pos.left.y+20 system.pos.left.x+chest_w system.pos.left.y+chest_h] );

end

if ismember('up', t.choices)
Screen('DrawTexture', system.w, system.tex.key,...
[], [ system.pos.up.x+20 system.pos.up.y+20 system.pos.up.x+chest_w system.pos.up.y+chest_h] );


end

progressBar(system, t)


end
75 changes: 75 additions & 0 deletions private/dropCoin.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
function dropCoin(system, t, correctTrials)

[screenWidth, screenHeight] = Screen('WindowSize', system.w);
ideal = GetSecs() + t.onset;

color = [0 0 0];

drawScreen(system, t, color)
totalCount(system, correctTrials);

yStep = 10; % Step size for animating the coin downward
coinSize = 20; % 20x20 pixels

% Parameters
coins_per_row = 20; % Number of coins per row
row_spacing = coinSize + 20; % Vertical spacing between rows
yPos_start = screenHeight / 3; % Initial vertical position
xPos_start = screenWidth / 3; % Initial horizontal position

% Loop through and draw 'c' number of coins based on correct trials
for c = 1:(correctTrials - 1)
% Calculate row and column positions for each static coin
row = floor((c - 1) / coins_per_row); % Determine the current row
col = mod((c - 1), coins_per_row); % Determine the column within the row

% Update horizontal and vertical positions
xPos = xPos_start + col * (coinSize + 10); % Horizontal position
yPos = yPos_start - row * row_spacing; % Vertical position (rows stack upward)

% Draw the static coin texture at the calculated position
Screen('DrawTexture', system.w, system.tex.starcoin, [], [xPos, yPos, xPos + coinSize, yPos + coinSize]);
end

if correctTrials >= 1
% Floating coin's initial Y-position (above the topmost row)
yStart = 0;

% Floating coin's final Y-position (end point is where the static coins are)
new_coin_row = floor((correctTrials - 1) / coins_per_row); % Determine the current row for the new coin
yEnd = yPos_start - new_coin_row * row_spacing; % Final vertical position for the new coin
xPosNewCoin = xPos_start + mod((correctTrials - 1), coins_per_row) * (coinSize + 10); % Horizontal position

% Calculate the distance and the number of steps needed for animation
distance = yEnd - yStart;
steps = round(distance / yStep);

% Animate the new coin floating down in steps
for x = 1:steps
drawScreen(system, t, color);
totalCount(system, correctTrials);

% Redraw the static coins during each frame of the animation
for c = 1:(correctTrials - 1)
row = floor((c - 1) / coins_per_row);
col = mod((c - 1), coins_per_row);
xPos = xPos_start + col * (coinSize + 10); % Horizontal position
yPos = yPos_start - row * row_spacing; % Vertical position

% Draw the static coins
Screen('DrawTexture', system.w, system.tex.starcoin, [], [xPos, yPos, xPos + coinSize, yPos + coinSize]);
end

% Animate the floating coin down smoothly
yPositionFloating = yStart + (yEnd - yStart) * (x / steps); % Linear interpolation

% Draw the floating coin at its current position
Screen('DrawTexture', system.w, system.tex.starcoin, [], [xPosNewCoin, yPositionFloating, xPosNewCoin + coinSize, yPositionFloating + coinSize]);

% Flip the screen to show the current frame
onset = Screen('Flip', system.w);

% Optional: Add a small delay to control the speed of movement
WaitSecs(0.02);
end
end
Loading

0 comments on commit b64b38f

Please sign in to comment.