space/game.wren
Jonathan Hirz 2897245701 updating
2019-11-30 13:02:49 -08:00

249 lines
8.2 KiB
Text

import "luxe: game" for Game
import "luxe: assets" for Assets
import "luxe: input" for Input, Key
import "luxe: world" for World, Entity, Transform, Sprite, Values, Tags, Camera, Text
import "luxe: math" for Math
import "luxe: draw" for Draw, PathStyle
import "luxe: io" for IO
import "random" for Random
// import "luxe: array" for Lists
import "outline/app" for App
class game is Game {
construct ready() {
//todo: get animation going on ship/exhaust duh
//todo: ammo. initialize a bunch of entities (bullets) into a list, when fired, add one to another list. that second list gets run through every tick, and we update the position of each bullet. then recycle them after a while
System.print("ready!")
app = App.new()
app.color = [0,0,0,1]
System.print("render size: %(app.width) x %(app.height) @ %(app.scale)x")
// game vars
// camera
_camera_scale = 2
_cam_offset_x = (app.width / 2 / _camera_scale)
_cam_offset_y = (app.height / 2 / _camera_scale)
// starfield
_stars = []
// player ship
_input_vec = []
_ship_rotation = 0
_ship_speed = 40
_ship_acceleration_x = 0
_ship_acceleration_y = 0
_ship_velocity_x = 0
_ship_velocity_y = 0
_ship_dampening = 0.85
Camera.ortho(app.camera, 0, 0, _cam_offset_x * 2, _cam_offset_y * 2, -5, 5)
create_ship()
create_ui_text()
create_asteroid(5, 10)
create_startracker()
create_starfield()
Transform.set_pos(app.camera, Transform.get_pos_x(_ship) - _cam_offset_x, Transform.get_pos_y(_ship) - _cam_offset_y)
// NOTES
// z-layers: -1 stars, 0 player ship
// camera stuff: we're moving the ship in the world, based on input. then, moving the camera,
// with lerp, based on where the ship goes. then, moving star_tracker to the camera position,
// with offset. positions and offsets react to _camera_scale
} //ready
tick(delta) {
tick_ship(delta)
tick_camera(delta)
tick_starfield(delta)
something_else()
if(Input.key_state_released(Key.escape)) {
IO.shutdown()
}
app.tick(delta)
} //tick
//////////////
// CREATORS //
//////////////
something_else() {
}
create_ship() {
_ship = Entity.create(app.world, "ship")
Transform.create(_ship)
var ship_mat = Assets.material("material/ship")
Sprite.create(_ship, ship_mat, 16, 32)
Transform.set_pos(_ship, 0, 0)
} //create_ship
create_ui_text() {
_position_text = Entity.create(app.ui)
_mat_font = Assets.material("luxe: material/font")
Transform.create(_position_text)
Transform.set_pos(_position_text, app.width / 2 - 35, app.height - 50)
Text.create(_position_text, _mat_font, 32, "fonts/lato", [1,1,1,1])
} //create_ui_text
create_asteroid(x, y) {
_asteroid = Entity.create(app.world, "asteroid")
Transform.create(_asteroid)
var asteroid_mat = Assets.material("material/asteroid")
Sprite.create(_asteroid, asteroid_mat, 32, 32)
Transform.set_pos(_asteroid, x, y)
} //create_asteroid
create_startracker() {
_star_tracker = Entity.create(app.world, "star_tracker")
Transform.create(_star_tracker)
Transform.set_pos(_star_tracker, Transform.get_pos_x(_ship), Transform.get_pos_y(_ship), Transform.get_pos_z(_ship))
} //create_startracker
create_starfield() {
// sometimes you come back to some code and don't remember exactly how it all works
// that's ok, just trust that past you figured this out, and it works how you want it to
// hopefully you can figure it out later if needed
//
// this makes the starfield background, with randomness applied to the position, size, and set_alpha
// of each star. stars are parented to _star_tracker, which follows the player around
// put a value in the new() call to make the same starfield each time
var rng = Random.new()
var number_of_stars = 500
for(i in 0...number_of_stars) {
var randomness = rng.float(0.5)
var randomness_x = rng.float()
var randomness_y = rng.float()
_star = Entity.create(app.world, "star")
Transform.create(_star)
Transform.set_pos(_star, (app.width * randomness_x) - (app.width / 2), (app.height * randomness_y) - (app.height / 2), -1)
var star_mat = Assets.material("material/star")
Sprite.create(_star, star_mat, 8 * randomness, 8 * randomness)
Sprite.set_alpha(_star, randomness)
// Sprite.set_color(_star, 1, 1, 0, 1)
Transform.link(_star, _star_tracker)
_stars.add(_star)
}
// debug red star
// Sprite.set_color(_stars[1], 1, 0, 0, 1)
} //create_starfield
//////////////
// UPDATERS //
//////////////
tick_ship(delta) {
var input_vec = [get_axis("horizontal"), get_axis("vertical")]
Math.normalize2D(input_vec)
// 2d movement physics
_ship_acceleration_x = input_vec.x * _ship_speed
_ship_acceleration_y = input_vec.y * _ship_speed
_ship_velocity_x = _ship_velocity_x + (_ship_acceleration_x * delta)
_ship_velocity_y = _ship_velocity_y + (_ship_acceleration_y * delta)
_ship_velocity_x = _ship_velocity_x * _ship_dampening
_ship_velocity_y = _ship_velocity_y * _ship_dampening
// rotate ship in direction of movement, if input is detected (solves snap at vel=0)
if(input_vec.x != 0 || input_vec.y != 0) {
_ship_rotation = Math.atan2(_ship_velocity_y, _ship_velocity_x) + Math.radians(90)
Transform.set_euler_world(_ship, 0, 0, _ship_rotation)
}
// move ship
Transform.translate(_ship, _ship_velocity_x, _ship_velocity_y)
// update position text
var ship_pos_x_int = Math.floor_around_zero(Transform.get_pos_x(_ship))
var ship_pos_y_int = Math.floor_around_zero(Transform.get_pos_y(_ship))
Text.set_text(_position_text, "[%(ship_pos_x_int), %(ship_pos_y_int)]")
} //tick_ship
tick_camera(delta) {
// move the camera, with some lerp delay, along with the ship
var shipx = Transform.get_pos_x(_ship)
var shipy = Transform.get_pos_y(_ship)
var camerax = Transform.get_pos_x(app.camera)
var cameray = Transform.get_pos_y(app.camera)
var interpolation = 0.8
var camera_interp_x = lerp(shipx - _cam_offset_x, camerax, interpolation)
var camera_interp_y = lerp(shipy - _cam_offset_y, cameray, interpolation)
Transform.set_pos(app.camera, camera_interp_x, camera_interp_y)
} //tick_camera
tick_starfield(delta) {
Transform.set_pos(_star_tracker, Transform.get_pos_x(app.camera) + _cam_offset_x, Transform.get_pos_y(app.camera) + _cam_offset_y, -1)
// loop through stars[]
for(star in _stars) {
Transform.set_pos_x(star, Transform.get_pos_x(star) + -_ship_velocity_x * (Sprite.get_alpha(star) * 0.5))
Transform.set_pos_y(star, Transform.get_pos_y(star) + -_ship_velocity_y * (Sprite.get_alpha(star) * 0.5))
// x pos reset
if(Transform.get_pos_x(star) > _cam_offset_x * _camera_scale) {
Transform.set_pos_x(star, -_cam_offset_x * _camera_scale)
}
if(Transform.get_pos_x(star) < -_cam_offset_x * _camera_scale) {
Transform.set_pos_x(star, _cam_offset_x * _camera_scale)
}
// y pos reset
if(Transform.get_pos_y(star) > _cam_offset_y * _camera_scale) {
Transform.set_pos_y(star, -_cam_offset_y * _camera_scale)
}
if(Transform.get_pos_y(star) < -_cam_offset_y * _camera_scale) {
Transform.set_pos_y(star, _cam_offset_y * _camera_scale)
}
}
} //tick_starfield
/////////////
// HELPERS //
/////////////
get_axis(axis) {
if(axis == "horizontal") {
var xaxis = 0
if(Input.key_state_down(Key.key_a) || Input.key_state_down(Key.left)) {
xaxis = xaxis - 1
}
if(Input.key_state_down(Key.key_d) || Input.key_state_down(Key.right)) {
xaxis = xaxis + 1
}
return xaxis
}
if(axis == "vertical") {
var yaxis = 0
if(Input.key_state_down(Key.key_w) || Input.key_state_down(Key.up)) {
yaxis = yaxis - 1
}
if(Input.key_state_down(Key.key_s) || Input.key_state_down(Key.down)) {
yaxis = yaxis + 1
}
return yaxis
}
} //get_axis
lerp(a, b, t) {
return a + t * (b - a)
} //lerp
destroy() {
System.print("unready!")
app.destroy()
} //destroy
app { _app }
app=(v) { _app=v }
} //Game