Forum

> > CS2D > Scripts > Keep Player Moving
Forums overviewCS2D overview Scripts overviewLog in to reply

English Keep Player Moving

5 replies
To the start Previous 1 Next To the start

old Keep Player Moving

AbAeterno
User Off Offline

Quote
Hello everyone!
How can you keep the player moving in a direction?
I mean, when the player tries to move to the left(Example), the player automatically would move constantly to the left until colliding with a wall.
Also, the player while "Moving Constantly", should not be able to move into any other direction.
If you didn't understand I may explain it better.

Thanks in advance!
PS:Not sure if this is possible.

old Re: Keep Player Moving

Apache uwu
User Off Offline

Quote
Oh so like skating on ice?

I didn't have time to make it more efficient, however I'm sure there are no errors.

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
addhook("move","_move")
addhook("spawn","_spawn")
addhook("always","_always")

previous={}
direction={}
speed=5

function _spawn(id)
	previous[id]={player(id,"x"),player(id,"y")}
	direction[id]=nil
end

function _move(id,x,y)
	if x~=previous[id][1] or y~=previous[id][2] then
		if x>previous[id][1] then
			direction[id]=1
		elseif x<previous[id][1] then
			direction[id]=0
		elseif y>previous[id][2] then
			direction[id]=3
		elseif y<previous[id][2] then
			direction[id]=2
		end
		parse("speedmod "..id.." -50")
	end
	previous[id]={x,y}
end

function _always()
	for _,id in ipairs(player(0,"tableliving")) do
		if direction[id]~=nil then
			local x,y=0,0
			if direction[id]==0 then
				x=-speed
			elseif direction[id]==1 then
				x=speed
			elseif direction[id]==2 then
				y=-speed
			elseif direction[id]==3 then
				y=speed
			end
			parse("setpos "..id.." "..player(id,"x")+x.." "..player(id,"y")+y)
			if x==speed then
				x=16
				y=0
			elseif x==-speed then
				x=-16
				y=0
			end
			if y==speed then
				y=16
				x=0
			elseif y==-speed then
				y=-16
				x=0
			end
			if tile(math.floor((player(id,"x")+x)/32),math.floor((player(id,"y")+y)/32),"wall") then
				parse("setpos "..id.." "..(32*player(id,"tilex")+16).." "..(32*player(id,"tiley")+16))
				parse("speedmod "..id.." 0")
				direction[id]=nil
				previous[id]={player(id,"x"),player(id,"y")}
			end
		end
	end
end

old Re: Keep Player Moving

AbAeterno
User Off Offline

Quote
Thank you very much, really! It works. It also bugs when moving to the map limit, but that's not a problem, I don't actually expect the players to move to the map limit.
Thanks!

old Re: Keep Player Moving

EngiN33R
Moderator Off Offline

Quote
Instead of user Apache uwu's method, I would do it this way, to allow for some intricate manoeuvres:

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
function angledir(rot,x,y,factor)
	if rot<-90 then rot=rot+360 end
	local angle=math.rad(math.abs(rot+90))-math.pi
	local cx=x+(math.cos(angle)*factor)
	local cy=y+(math.sin(angle)*factor)
	return cx,cy
end

speed = 5

addhook("always","skate")
function skate()
	for _,i in pairs(player(0,"tableliving")) do
		local dx,dy = angledir(player(i,"rot"),player(i,"x"),player(i,"y"),speed)
		local tdx,tdy = math.floor(dx/32),math.floor(dy/32)
		if (tile(tdx,tdy,"walkable")) then
			parse("setpos "..i.." "..dx.." "..dy)
		end
	end
end

addhook("spawn","beginskating")
function beginskating(id)
	parse("speedmod "..id.." -50")
end

It's also shorter, but either works.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview