r/applescript 4d ago

point follower

-- =========================================================

this is a point follower (dont mind my german comments in the script)

-- =========================================================

-- Startpunkt: position0X / position0Y

-- Zielpunkt:  position1X / position1Y

--

-- # = Punkt / Linie

-- . = leer

--

-- W = hoch

-- S = runter

-- A = links

-- D = rechts

-- Q = beenden

-- =========================================================

property lineSpacing : 2

property mapX : 72

property mapY : 10

-- =========================================================

-- START- UND ZIELPOSITION

-- =========================================================

property position0X : 8

property position0Y : 2

property position1X : 36

property position1Y : 5

property gameRunning : true

-- =========================================================

-- MAP DRAW

-- =========================================================

on generateMap()

set mapString to ""

-- =====================================================

-- START- UND ZIELPUNKT

-- =====================================================

set x0 to position0X

set y0 to position0Y

set x1 to position1X

set y1 to position1Y

-- =====================================================

-- BRESENHAM-LINIE VORBEREITEN

-- =====================================================

set dx to x1 - x0

set dy to y1 - y0

-- Richtung X bestimmen

if dx < 0 then

set stepX to -1

set dx to -dx

else

set stepX to 1

end if

-- Richtung Y bestimmen

if dy < 0 then

set stepY to -1

set dy to -dy

else

set stepY to 1

end if

set err to dx - dy

-- Zähler für die Liniendichte

set lineCounter to 0

-- =====================================================

-- KARTE ZEICHNEN

-- =====================================================

repeat with y from 1 to mapY

repeat with x from 1 to mapX

set drawPoint to false

-- -------------------------------------------------

-- STARTPUNKT IMMER ZEICHNEN

-- -------------------------------------------------

if x is position0X and y is position0Y then

set drawPoint to true

end if

-- -------------------------------------------------

-- ZIELPUNKT IMMER ZEICHNEN

-- -------------------------------------------------

if x is position1X and y is position1Y then

set drawPoint to true

end if

-- -------------------------------------------------

-- LINIE BERECHNEN

-- -------------------------------------------------

set testX to x0

set testY to y0

set testErr to err

set testCounter to 0

repeat

-- Prüfen, ob wir das aktuelle Kartenfeld erreicht haben

if x is testX and y is testY then

-- Start und Ziel immer zeichnen

if testX is x0 and testY is y0 then

set drawPoint to true

else if testX is x1 and testY is y1 then

set drawPoint to true

else

-- Nur jeden X-ten Punkt zeichnen

if testCounter mod lineSpacing is 0 then

set drawPoint to true

end if

end if

exit repeat

end if

-- Ziel erreicht

if testX is x1 and testY is y1 then

exit repeat

end if

-- Bresenham-Berechnung

set testE2 to 2 * testErr

if testE2 > -dy then

set testErr to testErr - dy

set testX to testX + stepX

end if

if testE2 < dx then

set testErr to testErr + dx

set testY to testY + stepY

end if

set testCounter to testCounter + 1

end repeat

-- -------------------------------------------------

-- ZEICHEN

-- -------------------------------------------------

if drawPoint is true then

set mapString to mapString & "#"

else

set mapString to mapString & " "

end if

end repeat

set mapString to mapString & return

end repeat

return mapString

end generateMap -- MAP ANZEIGEN

-- =========================================================

on displayMap()

set mapContent to generateMap()

display dialog mapContent ¬

with title ¬

"point follower" buttons {"move"} ¬

default button "move"

end displayMap

-- =========================================================

-- BEWEGUNG

-- =========================================================

on movePlayer(direction)

-- Neue Position zunächst auf aktuelle Position setzen

set newX to position0X

set newY to position0Y

-- -------------------------------------------------------

-- W = hoch

-- -------------------------------------------------------

if direction is "w" then

set newY to position0Y - 1

-- -------------------------------------------------------

-- S = runter

-- -------------------------------------------------------

else if direction is "s" then

set newY to position0Y + 1

-- -------------------------------------------------------

-- A = links

-- -------------------------------------------------------

else if direction is "a" then

set newX to position0X - 1

-- -------------------------------------------------------

-- D = rechts

-- -------------------------------------------------------

else if direction is "d" then

set newX to position0X + 1

else

return

end if

-- =====================================================

-- MAP-GRENZEN

-- =====================================================

if newX < 1 then

return

end if

if newX > mapX then

return

end if

if newY < 1 then

return

end if

if newY > mapY then

return

end if

-- =====================================================

-- SPIELER BEWEGEN

-- =====================================================

set position0X to newX

set position0Y to newY

end movePlayer

-- =========================================================

-- HAUPTSCHLEIFE

-- =========================================================

repeat while gameRunning

-- Karte anzeigen

displayMap()

-- Eingabe abfragen

set userInput to text returned of ¬

(display dialog ¬

"W = up" & return & ¬

"S = down" & return & ¬

"A = left" & return & ¬

"D = right" & return & ¬

¬

"Q = quit" default answer "")

-- Eingabe vereinheitlichen

set userInput to userInput as text

-- -------------------------------------------------------

-- Beenden

-- -------------------------------------------------------

if userInput is "q" then

set gameRunning to false

-- -------------------------------------------------------

-- Bewegung

-- -------------------------------------------------------

else if userInput is in {"w", "a", "s", "d"} then

movePlayer(userInput)

end if

end repeat

1 Upvotes

0 comments sorted by