However, there were some issues with walls, and there is one remaining bug that I cannot figure out on my own. I will do my best to describe it.
In order to prevent the player from passing through walls, the script sets the hero-x value appropriately behind the wall.
Code: Select all
script, can right, begin
variable(hx)
hx := hero-x / 200 + 1
if(
(read pass block(hx,(hero-y) / 200),and,west wall)
||
(read pass block(hx,(hero-y + 199) / 200),and,west wall)
)
then(
hero-x := hero-x -- (hero-x,mod,200) ###this part
if(hero-vx > 0) then(hero-vx := 0)
return(false)
)
else(return(true))
end
This only applies to east and west walls, and not to north and south. In other words, coming at a corner from the left or right does not exhibit the same behavior and the hero simply stops moving horizontally.
I'm sure the problem is simply something I am not adept enough to find out. This is why I am coming here for help.
Here is the entire script:
Code: Select all
global variable, begin
1,friction
2,hero-x
3,hero-y
4,hero-vx
5,hero-vy
6,hero-speed
7,hero-max-vx
8,hero-max-vy
9,want left
10,want right
11,want up
12, want down
13,hero-sprite
14,paused
17,playing
19,stepon-slice
20,stepon-slice2
21,enemy-slice
end
# ID of the timer to use
define constant(15, timer:three frame walking)
# Slice extradata slot to use for metadata
define constant(2, extradata:three frame walking)
plotscript, new game, begin
suspend player
variable(title)
title := load slice collection(1)
set hero direction(me,up)
wait for key(any key)
free slice(title)
run game
end
script, initialize, begin
suspend player
friction := 10
hero-x := hero pixel x(me) * 10
hero-y := hero pixel y(me) * 10
hero-vx := 0
hero-vy := 0
hero-speed := 10
hero-max-vx := 40
hero-max-vy := 40
hero-sprite := lookup slice(sl:walkabout sprite component, get hero slice(me))
toggle
end
script, run game, begin
initialize
playing := true
while(playing) do, begin
if(want left) then(hero-vx -= hero-speed, animate hero eachtick)
if(want right) then(hero-vx += hero-speed, animate hero eachtick)
if(want up) then(hero-vy -= hero-speed, animate hero eachtick)
if(want down) then(hero-vy += hero-speed, animate hero eachtick)
if(hero-vx >> hero-max-vx) then(hero-vx := hero-max-vx)
if(hero-vx <<hero>> hero-max-vy) then(hero-vy := hero-max-vy)
if(hero-vy << hero-max-vy * -1) then(hero-vy := hero-max-vy * -1)
hero-x += hero-vx
hero-y += hero-vy
if(hero-vx <<friction>> friction * -1 && want left == false && want right == false) then(
hero-vx := 0)
if (hero-vx >= friction && want right == false) then(
hero-vx -= friction)
if (hero-vx <= friction * -1 && want left == false) then(
hero-vx += friction)
if(hero-vy <<friction>> friction * -1 && want up == false && want down == false) then(
hero-vy := 0)
if (hero-vy >= friction && want down == false) then(
hero-vy -= friction)
if (hero-vy <= friction * -1 && want up == false) then(
hero-vy += friction)
if (hero-vx <0>= 0) then (can right)
if (hero-vy <0>= 0) then(can down)
put hero(me, hero-x/10, hero-y/10)
show value(hero-y)
want left := false
want right := false
want up := false
want down := false
wait(1)
end
end
# =============================================
plotscript, keypress, begin
if(not(paused)) then(
if(key is pressed(key:left)) then(
want left := true)
if(key is pressed(key:right)) then(
want right := true)
if(key is pressed(key:up)) then(
want up := true)
if(key is pressed(key:down)) then(
want down := true)
if(keyval(key:esc) > 1 || keyval(key:filtered alt) > 1) then(
if(menu is open(0)) then(close menu(0))
else(open menu(0))
)
if(keyval(key:space) > 1) then(
use faced NPC)
)
end
script, can left, begin
variable(hx,hy-down,hy-up)
hx := (hero-x -- 10) / 200
hy-down := (hero-y) / 200 + 1
hy-up := (hero-y) / 200
if(
(read pass block(hx,(hero-y) / 200),and,east wall)
||
(read pass block(hx,(hero-y + 199) / 200),and,east wall)
||
(hero-x == 0)
)
then(
variable(new x)
new x := 0
if(hero-x,mod,200 > 100) then(new x := 200)
if(hero-vx <100> 50)) then(new x := 200)
hero-x := hero-x -- (hero-x,mod,200) + new x
show value(1)
if(hero-vx <0> 0) then(hero-vx := 0)
return(false)
)
else(return(true))
end
script, can up, begin
variable(hy)
hy := (hero-y) / 200
if(
(read pass block((hero-x) / 200, hy),and,south wall)
||
(read pass block((hero-x + 199) / 200, hy),and,south wall)
||
(hero-y == 0)
)
then(
variable(new y)
new y := 0
if(hero-y,mod,200 > 100) then(new y := 200)
if(hero-vy <100> 50)) then(new y := 200)
hero-y := hero-y -- (hero-y,mod,200) + new y
if(hero-vy <0> 0) then(hero-vy := 0)
return(false)
)
else(return(true))
end