; ;--------------------------------------------------------------------------;
; ; Step 8 ;
; ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
;
;,,,,
; Time to put the player in the center of the screen.
; E.g. what you see in all those 2D games.
;
; First, we have to know what relates to what.
; If the the player is in the center of the screen,
; then there's no need to control the viewpoint,
; as the viewpoint is always the player on screen.
; We could add a controllable viewpoint but this would
; have to be relative to the player screen position.
; Let's start with just centering the view on the player.
;
; +---------------+
; | | - Screen
; | (Player) |
; | \ |
; | X |
; | |
; | |
; | |
; +---------------+
;
; When the player is in the center of the screen,
; the player screen position is constant.
;
; Player_ScreenPos = Screen_Size / 2
;
; Add a background.
;
; +---------------+
; | | - Screen
; | (Player) |
; | \ |
; | X - - - |-------+
; | | | | - Background
; | | |
; | | | |
; +---------------+ |
; | |
; | |
; | |
; +---------------+
;
; If the player moves left, the background moves in the reversed
; direction.
; Yes, it's that reversed thingy again :)
;
; How does the player world position relate to the view position?
;
; (View_Pos)
; \
; -···O---------------+
; ^ | | - Screen
; ry | | (Player) |
; v | \ |
; -···|·······X - - - |-------+
; | | | | - Background
; | . | |
; | | | |
; +---------------+ |
; . | |
; . | |
; . | |
; . +---------------+
; . .
; |<----->|
; rx
;
; Let's assume that the background always stays at world position (0,0).
; And the player also starts at world position (0,0).
; At this point, world coordinate (0,0) is center of screen.
; When we move the player off the map for about a quarter..
;
; (View_Pos)
; \
; -···O---------------+
; ^ | | - Screen
; ry | | (Player) |
; v | \ |
; -···|·······X |
; | . |
; | . +- -|-----------+
; | . | | | - Background
; +---------------+ |
; . . | |
; . . | |
; |<----->| | |
; rx | |
; | |
; +---------------+
;
; The view position moves with the player world position.
; You can conclude that, in english, the view position is the player
; world position minus half the screen.
;
; View_Pos = Player_WorldPos - Screen_Size / 2
;
; Only thing is, we're not using a view point, as it's always
; directly relative to the player world position.
; So we can forget about that for now :)
;----
;
;,,,,
; Okay so where is the background on screen?
; Well, the background position on screen is also relative
; to the player world position.
;
; For a second, let's put the player in upperleft corner of screen
; instead of center of screen.
;
; (View_Pos)
; \
; X---------------+
; /| | - Screen
; (Player)| |
; | |
; | + - - - |-------+
; | | | | - Background
; | | |
; | | | |
; +---------------+ |
; | |
; | |
; | |
; +---------------+
;
; So now rx and ry in the above pictures are no longer relevant.
; They are always 0. Meaning?
; The view position equals the player world position.
;
; Also, in the above picture the background seems to begin in
; the middle of the screen.
; So because the background position on screen
; is relative to the player world position,
; the player *world* position has to be equal to..
; half the screen :)
;
; (Player_WorldPos)/(View_Pos)
; \
; -···X---------------+
; ^ | | - Screen
; ry | |(Back_WorldPos)|
; v | / |
; -···|·······O - - - |-------+
; | | | | - Background
; | . | |
; | | | |
; +---------------+ |
; . | |
; . | |
; . | |
; . +---------------+
; . .
; |<----->|
; rx
;
; Suppose the view position is still equal to the player position.
; In the above display you see rx and ry as the distance between
; player world position and background world position.
;
; So the background screen position is the background world position
; minus the player world position.
;
; Back_ScreenPos = Back_WorldPos - Player_WorldPos
;
; Since the background world position is also 0, you could also write:
;
; Back_ScreenPos = -Player_WorldPos
;
; But let's keep it in for clarity.
;
; Now if we move the player back to the center of the screen..
;
; +-------------+
; | | - Screen
; | (Player) |
; | \ |
; | X······|················-
; | . | ^
; | . | | ry
; | . | v
; +-------------+-------------+··-
; . | |
; . | | - Background
; . | |
; . | |
; . | |
; . | |
; . | |
; . +-------------+
; . .
; |<---->|
; rx
;
; The relativity (rx,ry) is still the same.
; The only thing that is changed is that half a screen is
; added to all screen positions.
;
; So let's remember our current background equation.
;
; Back_ScreenPos = Back_WorldPos - Player_WorldPos
;
; Now add half the screen.
;
; Back_ScreenPos = Back_WorldPos - Player_WorldPos + Screen_Size / 2
;
; And that's it! :)
;----
Back_SizeX = GraphicsWidth ()
Back_SizeY = GraphicsHeight ()
Player_SizeX = 20
Player_SizeY = 20
Back_WorldPosX = 0
Back_WorldPosY = 0
Player_WorldPosX = 0
Player_WorldPosY = 0
; This variable/constant is the offset on screen
; where the player should be.
Player_ScreenPosX = GraphicsWidth () / 2
Player_ScreenPosY = GraphicsHeight () / 2
SetBuffer BackBuffer ()
Repeat
; Arrow keys control player world position.
If KeyDown ( 200 ) Then Player_WorldPosY = Player_WorldPosY - 1
If KeyDown ( 208 ) Then Player_WorldPosY = Player_WorldPosY + 1
If KeyDown ( 203 ) Then Player_WorldPosX = Player_WorldPosX - 1
If KeyDown ( 205 ) Then Player_WorldPosX = Player_WorldPosX + 1
; View position relative to player world position.
; Originally we had:
;
; View_Pos = Player_WorldPos - ( Screen_Size / 2 )
; `-----------------'
; |
; +--------------------+
; |
; .--------------.
; I've defined Player_ScreenPos variables for this purpose.
View_PosX = Player_WorldPosX - Player_ScreenPosX
View_PosY = Player_WorldPosY - Player_ScreenPosY
; Although the above is totally unused :)
; Okay, the background.
; Filled in Player_ScreenPos in the same way.
; Now don't go filling in stuff with other stuff that incidently
; leads to the same results but has nothing to do with it :P
Back_ScreenPosX = Back_WorldPosX - Player_WorldPosX + Player_ScreenPosX
Back_ScreenPosY = Back_WorldPosY - Player_WorldPosY + Player_ScreenPosY
Rect Back_ScreenPosX , Back_ScreenPosY , Back_SizeX , Back_SizeY , False
; Player screen position is constant,
; so doesn't need to be calculated.
Rect Player_ScreenPosX,Player_ScreenPosY,Player_SizeX,Player_SizeY,False
Flip
Cls
Until KeyHit ( 1 )
End