Skip to content

Commit

Permalink
make example cuter
Browse files Browse the repository at this point in the history
  • Loading branch information
Acepie committed Mar 21, 2024
1 parent 58d233d commit 1b844de
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
8 changes: 1 addition & 7 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ name = "p5js_gleam"
version = "1.0.0"
target = "javascript"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
description = "A simple game library providing p5.js bindings for Gleam to make basic games and animations"
description = "A simple game library providing p5.js bindings for Gleam in a functional style to make basic games and animations. Heavily inspired by the Racket library 2htdp/universe"
licences = ["MIT"]
repository = { type = "github", user = "Acepie", repo = "p5js_gleam" }
links = [{ title = "P5.js", href = "https://p5js.org/" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = "~> 0.34 or ~> 1.0"
Expand Down
55 changes: 44 additions & 11 deletions src/p5js_gleam.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ import gleam/float
import gleam/io
import gleam/list

type Vector {
Vector(x: Float, y: Float)
}

fn add_vectors(a: Vector, b: Vector) -> Vector {
Vector(a.x +. b.x, a.y +. b.y)
}

type Ball {
Ball(x: Float, y: Float, dx: Float, dy: Float)
Ball(position: Vector, velocity: Vector, color: String)
}

type WorldState {
WorldState(balls: List(Ball))
}

const colors = [
"#ffaff3", "#fefefc", "#a6f0fc", "#fffbe8", "#584355", "#292d3e", "#2f2f2f",
]

const ball_size = 40.0

const ball_speed = 5.0
Expand All @@ -28,29 +40,40 @@ fn setup(p: P5) -> WorldState {
fn draw(p: P5, state: WorldState) -> Nil {
p5.background(p, 220)
{
use Ball(x, y, _, _) <- list.map(state.balls)
p5.ellipse(p, x, y, ball_size *. 2.0, ball_size *. 2.0)
use Ball(Vector(x, y), _, color) <- list.map(state.balls)
p
|> p5.fill(color)
|> p5.ellipse(x, y, ball_size *. 2.0, ball_size *. 2.0)
}
Nil
}

fn advance_ball(b: Ball) -> Ball {
Ball(b.x +. b.dx, b.y +. b.dy, b.dx, b.dy)
Ball(add_vectors(b.position, b.velocity), b.velocity, b.color)
}

fn flip_ball_x(b: Ball) -> Ball {
Ball(b.x -. b.dx *. 2.0, b.y +. b.dy, float.negate(b.dx), b.dy)
let Ball(position, velocity, color) = b
let new_velocity = Vector(float.negate(velocity.x), velocity.y)
let new_position =
Vector(position.x -. velocity.x *. 2.0, position.y +. velocity.y)
Ball(new_position, new_velocity, color)
}

fn flip_ball_y(b: Ball) -> Ball {
Ball(b.x +. b.dx, b.y -. b.dy *. 2.0, b.dx, float.negate(b.dy))
let Ball(position, velocity, color) = b
let new_velocity = Vector(velocity.x, float.negate(velocity.y))
let new_position =
Vector(position.x +. velocity.x, position.y -. velocity.y *. 2.0)
Ball(new_position, new_velocity, color)
}

fn tick(state: WorldState) -> WorldState {
io.debug(state)
let balls = {
use ball <- list.map(state.balls)
case ball.x, ball.y {
let Ball(Vector(x, y), _, _) = ball
case x, y {
x, _ if x <. 0.0 || x >. screen_width -> flip_ball_x(ball)
_, y if y <. 0.0 || y >. screen_height -> flip_ball_y(ball)
_, _ -> advance_ball(ball)
Expand All @@ -70,11 +93,21 @@ fn on_mouse(
y_position: Float,
state: WorldState,
) -> WorldState {
io.debug(x_position)
io.debug(y_position)
state
let assert [color, ..] =
colors
|> list.shuffle()

let x_speed = case float.random() {
x if x <. 0.5 -> ball_speed
_ -> float.negate(ball_speed)
}
let y_speed = case float.random() {
x if x <. 0.5 -> ball_speed
_ -> float.negate(ball_speed)
}

WorldState([
Ball(x_position, y_position, ball_speed, ball_speed),
Ball(Vector(x_position, y_position), Vector(x_speed, y_speed), color),
..state.balls
])
}
Expand Down

0 comments on commit 1b844de

Please sign in to comment.