mirror of
https://github.com/sam-astro/Z-Sharp.git
synced 2026-02-04 07:02:11 +00:00
Optimize script
This commit is contained in:
parent
3d8fca05ba
commit
86af2af61a
@ -8,6 +8,7 @@ float g_jumpHeight = 20
|
||||
float g_currPlayerSpeed = 400
|
||||
|
||||
bool g_running = false
|
||||
bool g_colliding = false
|
||||
|
||||
float g_gravitySpeed = -86
|
||||
|
||||
@ -17,7 +18,7 @@ func Main()
|
||||
{
|
||||
//SplitThread(ThreadedFunction())
|
||||
|
||||
// Immediately creates the window, then Start(), then the game loop. The game loop calls Update() every frame
|
||||
// Immediately creates the window, then runs Start(), then the game loop. The game loop calls Update() every frame
|
||||
ZS.Graphics.Init("Platformer game", g_screenw, g_screenh, g_resolutionScale)
|
||||
}
|
||||
|
||||
@ -28,33 +29,20 @@ func ThreadedFunction()
|
||||
|
||||
func Start()
|
||||
{
|
||||
float centerX = g_screenw / 2
|
||||
float centerY = g_screenh / 2
|
||||
global Vec2 g_screencenter = NVec2(centerX, centerY)
|
||||
global Vec2 g_screencenter = NVec2(g_screenw / 2, g_screenh / 2)
|
||||
|
||||
Vec2 playerPos = g_screencenter
|
||||
Vec2 playerScale = NVec2(16, 16)
|
||||
global Sprite g_playerSprite = ZS.Graphics.Sprite("./mariostill.png", playerPos, playerScale, 0)
|
||||
global Sprite g_playerSprite = ZS.Graphics.Sprite("./mariostill.png", g_screencenter, NVec2(16, 16), 0)
|
||||
|
||||
Vec2 groundPos = NVec2(g_screencenter.x, 192)
|
||||
Vec2 groundScale = NVec2(256, 16)
|
||||
global Sprite g_groundSprite = ZS.Graphics.Sprite("./square.png", groundPos, groundScale, 0)
|
||||
global Sprite g_groundSprite = ZS.Graphics.Sprite("./square.png", NVec2(g_screencenter.x, 192), NVec2(256, 16), 0)
|
||||
|
||||
Vec2 instructionsPos = NVec2(centerOfScreen.x, centerOfScreen.y)
|
||||
global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", instructionsPos, 20, 0, 255, 255, 255)
|
||||
global Text g_instructionsText = ZS.Graphics.Text("Use Arrow Keys or WASD to Move and Spacebar to Jump", "./arial.ttf", NVec2(centerOfScreen.x, centerOfScreen.y), 11, 0, 255, 255, 255)
|
||||
|
||||
global Vec2 g_playerTargetPosition = playerPos
|
||||
|
||||
int i = 0
|
||||
while i < 10 {
|
||||
i += 1
|
||||
print "while iter : " + i
|
||||
}
|
||||
}
|
||||
|
||||
func Update(deltaTime) {
|
||||
float fps = 1 / deltaTime
|
||||
print "FPS: " + fps
|
||||
print "FPS: " + fps + "\r"
|
||||
//TestInclude()
|
||||
|
||||
//// Test automatic conversion from bool to int
|
||||
@ -64,41 +52,41 @@ func Update(deltaTime) {
|
||||
// Shift key lets you sprint
|
||||
g_running = GetKey("SHIFT_L")
|
||||
|
||||
if g_running == true{
|
||||
if g_running == true {
|
||||
g_currPlayerSpeed = g_playerRunSpeed
|
||||
}
|
||||
if g_running == false
|
||||
{
|
||||
if g_running == false {
|
||||
g_currPlayerSpeed = g_playerWalkSpeed
|
||||
}
|
||||
|
||||
// Move Left And Right
|
||||
if GetKey("A") == true
|
||||
{
|
||||
float newY = g_playerSprite.position.y
|
||||
|
||||
float newX = g_playerTargetPosition.x - g_currPlayerSpeed * deltaTime
|
||||
g_playerTargetPosition = NVec2(newX, newY)
|
||||
g_playerTargetPosition = NVec2(newX, g_playerSprite.position.y)
|
||||
}
|
||||
if GetKey("D") == true
|
||||
{
|
||||
float newY = g_playerSprite.position.y
|
||||
|
||||
{
|
||||
float newX = g_playerTargetPosition.x + g_currPlayerSpeed * deltaTime
|
||||
g_playerTargetPosition = NVec2(newX, newY)
|
||||
g_playerTargetPosition = NVec2(newX, g_playerSprite.position.y)
|
||||
}
|
||||
// Lerps from old position to destination smoothly
|
||||
float oldX = g_playerSprite.position.x
|
||||
float newX = g_playerTargetPosition.x
|
||||
float stopSpeed = deltaTime * 7
|
||||
float lerpedX = Lerp(oldX, newX, stopSpeed)
|
||||
g_playerSprite.position = NVec2(lerpedX, g_playerTargetPosition.y)
|
||||
|
||||
// Apply gravity
|
||||
g_colliding = Colliding(g_playerSprite, g_groundSprite)
|
||||
if g_colliding == false {
|
||||
g_playerTargetPosition.y -= deltaTime * g_gravitySpeed
|
||||
}
|
||||
|
||||
// Lerps from old position to destination smoothly
|
||||
float stopSpeed = deltaTime * 7
|
||||
float lerpedX = Lerp(g_playerSprite.position.x, g_playerTargetPosition.x, stopSpeed)
|
||||
g_playerSprite.position = NVec2(lerpedX, g_playerTargetPosition.y)
|
||||
|
||||
// Finally draws all of the sprites
|
||||
ZS.Graphics.Draw(g_playerSprite)
|
||||
ZS.Graphics.Draw(g_groundSprite)
|
||||
|
||||
// Draw the text
|
||||
ZS.Graphics.DrawText(g_instructionsText)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user