Code archives/Miscellaneous/Follow player's position with delay
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This could be used as the basis for a trailing camera, to allow a missile to follow a player, etc. (To use in 3D, just add a z field and update it appropriately.) You'd want to modify it to be useful, as a camera would end up inside the player when they're travelling slowly, and so on. Also, to use for multiple entities, you'd probably want to add an identifying field to the Type for each entity to be trailed... (I'm trying to simulate delays in receiving position updates over the internet and just came up with this along the way.) | |||||
; Use cursors... 'enemy' will trail by 2 seconds... Type Snapshot Field x# Field y# Field ticks End Type ; The key variables -- play with these! trail = 1 ; Updates applied every 'trail' milliseconds... latency = 2000 ; Trailing entity delayed by this much (in milliseconds)... Graphics 1024, 768, 0, 2 SetBuffer BackBuffer () ticks = MilliSecs () x# = 0 y# = 0 xs# = 0 ys# = 0 move# = 0.025 Repeat ; Move player... If KeyDown (203) Then xs = xs - move If KeyDown (205) Then xs = xs + move If KeyDown (200) Then ys = ys - move If KeyDown (208) Then ys = ys + move x = x + xs y = y + ys ; Record current time... ms = MilliSecs () If ms - trail => ticks ; Create a snapshot in time with player's current position... ticks = ms snap.Snapshot = New Snapshot snap\x = x snap\y = y snap\ticks = ticks EndIf Cls ; Draw player's position... Color 127, 64, 64 Rect x - 4, y - 4, 8, 8 ; Draw player's position Color 127, 127, 255 For snap.Snapshot = Each Snapshot If ms - latency => snap\ticks trailx# = snap\x traily# = snap\y Delete snap EndIf Next Rect trailx - 2, traily - 2, 4, 4 Flip Until KeyHit (1) End |
Comments
None.
Code Archives Forum