I decided a few days ago that It would be a lot easier to make a platformer system right now, and just add to it, rather than reinventing the wheel every time I need a nice movement system. So, I started, I learned about velocity and gravity, also I figured out how to add in a variable friction. I also had a nice plan done for having multiple platforms, to jump on. But, I hit a snag, I have it so that all of the platforms made automatically add their positions to an array of a record to check with when the sprites are figuring out gravity, and collision.
The problem is that if I put the check for ground to disable gravity in a for loop, it allows it through because it only checks one value at a time, I need it to meet all the values at once, for every step of movement. There is probably something obvious here. But I lack the experience to say.
P.S. I realize there are some unfinished systems, and that I do need to check the X values for that platform as well. I just want the Y working first before I move on.
I also wouldn't mind getting ideas for shortening the code. If you have any idea's not matter how fragmented, I'd like to hear them.
Thanks in advanced, any help I get that attributes to a working system I will divide all of my bits amongst all the helpers.
P.P.S. The Sprite can be an AI character of a human, the four players were just a test.
Turing: | var window : int :=
Window.Open ("nobuttonbar, graphics:500;500, offscreenonly")
var Key : array char of boolean
% These varibales are used to check all Platform X, Y values
% with the character X, Y values for gravity collision, and run
% collision.
var PlatformRecord : flexible array 1 .. 0 of
record
BottomX, TopX, BottomY, TopY : int
end record
var SpriteRecord : flexible array 1 .. 0 of
record
Height, Width, PositionX, PositionY : int
end record
const GRAVITY := 1
const FRICTION := 1
const AIRRESISTANCE := 3
% Sprite Template
class NewSprite
% I/O for class
import Draw, Key, PlatformRecord, GRAVITY, FRICTION, SpriteRecord
export Initialize, Control, Apply, OutputVisual
% Variable Declarations
var PositionX, PositionY, VelocityX, VelocityY,
Height, Width, Density, Mass, Color, Shape,
MovementType, RunSpeed, Acceleration, JumpForce : int := 0
% This should not be called in a loop.
procedure Initialize (PositionX_, PositionY_, Height_, Width_, Color_,
Density_, Shape_, MovementType_, RunSpeed_, JumpForce_ : int)
% Set Values For Class
new SpriteRecord, upper (SpriteRecord ) + 1
SpriteRecord (upper (SpriteRecord )).Height := Height_
SpriteRecord (upper (SpriteRecord )).Width := Width_
SpriteRecord (upper (SpriteRecord )).PositionX := PositionX_
SpriteRecord (upper (PlatformRecord )).PositionY := PositionY_
MovementType := MovementType_
Mass := Height_ * Width_
PositionX := PositionX_
PositionY := PositionY_
Density := Density_
Height := Height_
Width := Width_
Color := Color_
Shape := Shape_
RunSpeed := RunSpeed_
JumpForce := JumpForce_
end Initialize
% Based on MovementType entered, each type will be initiated.
procedure Control
/* Movement Types:
0 - Player Controlled (WASD)
1 - Player Controlled (TFGH)
2 - Player Controlled (IJKL)
3 - Player Controlled (Arrow Keys)
*/
case MovementType of
label 0 :
if Key ('d') then
for i : 1 .. RunSpeed
VelocityX + = 1
end for
elsif Key ('a') then
for i : 1 .. RunSpeed
VelocityX - = 1
end for
end if
if Key ('w') and
PlatformRecord (1).TopY = PositionY then
VelocityY + = JumpForce
end if
label 1 :
if Key ('h') then
for i : 1 .. RunSpeed
VelocityX + = 1
end for
elsif Key ('f') then
for i : 1 .. RunSpeed
VelocityX - = 1
end for
end if
if Key ('t') and
PlatformRecord (1).TopY = PositionY then
VelocityY + = JumpForce
end if
label 2 :
if Key ('l') then
for i : 1 .. RunSpeed
VelocityX + = 1
end for
elsif Key ('j') then
for i : 1 .. RunSpeed
VelocityX - = 1
end for
end if
if Key ('i') and
PlatformRecord (1).TopY = PositionY then
VelocityY + = JumpForce
end if
label 3 :
if Key (KEY_RIGHT_ARROW) then
for i : 1 .. RunSpeed
VelocityX + = 1
end for
elsif Key (KEY_LEFT_ARROW) then
for i : 1 .. RunSpeed
VelocityX - = 1
end for
end if
if Key (KEY_UP_ARROW) and
PlatformRecord (1).TopY = PositionY then
VelocityY + = JumpForce
end if
end case
end Control
% Apply Physics, and Collision to forces and objects.
procedure Apply
if PlatformRecord (1).TopY not= PositionY then
VelocityY - = GRAVITY
end if
for i : 1 .. -VelocityY
if PlatformRecord (1).TopY not= PositionY then
PositionY - = 1
if PlatformRecord (1).TopY = PositionY then
VelocityY := 0
end if
end if
end for
if VelocityY > 0 then
for i : 1 .. VelocityY
PositionY + = 1
end for
end if
if VelocityX > 0 then
VelocityX - = FRICTION
elsif VelocityX < 0 then
VelocityX + = FRICTION
end if
PositionX + = VelocityX
end Apply
% Draw Character
procedure OutputVisual
/* Shapes :
0 - Square
1 - Circle
2 - Star
3 - Maple Leaf
4 - Picture (R&D) */
case Shape of
label 0 :
Draw.FillBox (PositionX - round (Width / 2),
PositionY, PositionX + round (Width / 2),
PositionY + Height, Color )
label 1 :
Draw.FillOval (PositionX, PositionY + round (Height / 2),
round (Width / 2), round (Height / 2), Color )
label 2 :
Draw.FillStar (PositionX - round (Width / 2),
PositionY, PositionX + round (Width / 2),
PositionY + Height, Color )
label 3 :
Draw.FillMapleLeaf (PositionX - round (Width / 2),
PositionY, PositionX + round (Width / 2),
PositionY + Height, Color )
end case
end OutputVisual
end NewSprite
% Plateform Template
class NewPlatform
% I/O for class
import Draw, PlatformRecord
export Initialize, DrawPlatform
% Variable Declarations
var BottomX, BottomY, TopX, TopY, Color : int
% This should not be called in a loop.
procedure Initialize (BottomX_, BottomY_, TopX_, TopY_, Color_ : int)
new PlatformRecord, upper (PlatformRecord ) + 1
PlatformRecord (upper (PlatformRecord )).TopY := TopY_
PlatformRecord (upper (PlatformRecord )).TopX := TopX_
PlatformRecord (upper (PlatformRecord )).BottomX := BottomX_
PlatformRecord (upper (PlatformRecord )).BottomY := BottomY_
TopX := TopX_
TopY := TopY_
BottomX := BottomX_
BottomY := BottomY_
Color := Color_
end Initialize
% This should be called in a loop.
procedure DrawPlatform
Draw.Box (BottomX, BottomY, TopX, TopY, Color )
end DrawPlatform
end NewPlatform
var Ground : ^NewPlatform
new Ground
Ground -> Initialize (0, 0, maxx, 100, green)
% Create Sprites
var Player1, Player2, Player3, Player4 : ^NewSprite
new Player1
new Player2
new Player3
new Player4
Player1 -> Initialize (100, 500, 10, 10, red, 4, 2, 0, 4, 15)
Player2 -> Initialize (200, 500, 10, 10, blue, 4, 1, 1, 2, 30)
Player3 -> Initialize (300, 500, 2, 2, green, 4, 1, 2, 2, 20)
Player4 -> Initialize (400, 500, 30, 30, yellow, 4, 1, 3, 2, 15)
loop
Input.KeyDown (Key )
Player1 -> Control
Player1 -> Apply
Player1 -> OutputVisual
Player2 -> Control
Player2 -> Apply
Player2 -> OutputVisual
Player3 -> Control
Player3 -> Apply
Player3 -> OutputVisual
Player4 -> Control
Player4 -> Apply
Player4 -> OutputVisual
Ground -> DrawPlatform
View.Update
Draw.Cls
delay (30)
end loop
|
|