aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/macroknight/game.hy45
-rw-r--r--src/macroknight/utils.hy14
2 files changed, 36 insertions, 23 deletions
diff --git a/src/macroknight/game.hy b/src/macroknight/game.hy
index c6c329e..5de4c2b 100644
--- a/src/macroknight/game.hy
+++ b/src/macroknight/game.hy
@@ -21,11 +21,10 @@
pytmx.util_pygame [load_pygame]
entities [Player LevelTile Goal Enemy PlayerKilled Physics]
tiles [TileSet draw-tile]
- utils [neg]
+ utils [neg add-tuples null-tuple normalise-tuple]
text [render-text]
systems [apply-gravity apply-collisions run-enemies GoalHit]
- math [floor]
- pprint [pprint])
+ math [floor])
(pygame.init)
@@ -118,7 +117,7 @@
(setv body.position #((* TILE_SIZE (get player-pos 0))
(* TILE_SIZE (get player-pos 1))))
(setv body.elasticity 0.2)
- (setv body.friction 0.5)
+ (setv body.friction 0.1)
(Physics body (pymunk.Poly.create_box body #(TILE_SIZE TILE_SIZE))))
TILE_SIZE
#* player-pos))
@@ -139,7 +138,7 @@
(let [body (pymunk.Body :body_type pymunk.Body.STATIC)]
(setv body.position #((* x TILE_SIZE) (* y TILE_SIZE)))
(setv body.elasticity 0.0)
- (setv body.friction 0.6)
+ (setv body.friction 0.1)
(Physics body (pymunk.Poly.create_box body #(TILE_SIZE TILE_SIZE))))
TILE_SIZE
x
@@ -148,7 +147,7 @@
(.add space tile-ent._physics.body tile-ent._physics.shape)
(.append entities tile-ent))))
- (setv ongoing_inputs [])
+ (setv ongoing-inputs [])
(while running
(for [event (pygame.event.get)]
@@ -165,9 +164,9 @@
(setv (get macro-commands (.index macro-commands None)) event.key))
(if (and (= event.key pygame.K_RETURN) (= macro-wait-time 0))
(setv macro-input-mode True)
- (.append ongoing_inputs event.key))))
- pygame.KEYUP (when (in event.key ongoing_inputs)
- (.remove ongoing_inputs event.key))))
+ (.append ongoing-inputs event.key))))
+ pygame.KEYUP (when (in event.key ongoing-inputs)
+ (.remove ongoing-inputs event.key))))
(.fill screen "#000000")
@@ -218,20 +217,22 @@
(let [progress (round (* 3 (/ (- macro-wait-time (pygame.time.get_ticks)) MACRO_COOLDOWN)))]
(for [indicator (range progress)]
(draw-tile screen tileset 725 (+ 4 indicator) 5))))
- (for [inp ongoing_inputs]
- (case inp
- ;; pygame.K_a (.move player #((neg player.SPEED) 0))
- ;; pygame.K_s (.move player #(0 1))
- ;; pygame.K_w (.jump player)
- ;; pygame.K_d (.move player #(player.SPEED 0))
- pygame.K_a (setv player._physics.body.velocity #((neg player.SPEED) 0))
- pygame.K_s (setv player._physics.body.velocity #(0 1))
- pygame.K_w (setv player._physics.body.velocity #(0 -100)) ;; (.jump player)
- pygame.K_d (setv player._physics.body.velocity #(player.SPEED 0))
- pygame.K_SPACE (.attack player)))
+ (let [player-velocity #(0 0)]
+ (for [inp ongoing-inputs]
+ (case inp
+ pygame.K_a (setv player-velocity (add-tuples player-velocity #((neg player.SPEED) 0)))
+ pygame.K_s (setv player-velocity (add-tuples player-velocity #(0 1)))
+ pygame.K_w (setv player-velocity (add-tuples player-velocity #(0 -100))) ;; (.jump player)
+ pygame.K_d (setv player-velocity (add-tuples player-velocity #(player.SPEED 0)))
+ pygame.K_SPACE (.attack player)))
+ (when (not (null-tuple player-velocity))
+ (setv player._physics.body.velocity (normalise-tuple
+ (add-tuples player._physics.body.velocity player-velocity)
+ #(-100 -200)
+ #(100 100)))))
(try
- (when (any ongoing_inputs)
+ (when (any ongoing-inputs)
(for [entity entities]
(apply-collisions entity entities)))
@@ -263,8 +264,6 @@
(.flush player)))
- (pprint player._physics.body.position)
-
(for [entity entities]
(.blit screen entity.surf entity.rect))
diff --git a/src/macroknight/utils.hy b/src/macroknight/utils.hy
index 05412e8..464bae5 100644
--- a/src/macroknight/utils.hy
+++ b/src/macroknight/utils.hy
@@ -61,6 +61,20 @@
#((- (get point2 0) (get point1 0))
(- (get point2 1) (get point2 1))))
+(defn add-tuples [tup1 tup2]
+ #((+ (get tup1 0) (get tup2 0))
+ (+ (get tup1 1) (get tup2 1))))
+
+(defn normalise-tuple [tup min-tup max-tup]
+ #((min (get max-tup 0)
+ (max (get min-tup 0) (get tup 0)))
+ (min (get max-tup 1)
+ (max (get min-tup 1) (get tup 1)))))
+
+(defn null-tuple [tup]
+ (and (= (get tup 0) 0)
+ (= (get tup 1) 0)))
+
(defn distance [point1 point2]
(sqrt (+ (** (- (get point1 0) (get point2 0)) 2)
(** (- (get point1 1) (get point2 1)) 2))))