187 lines
4.7 KiB
Text
187 lines
4.7 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
|
|
import "luxe: math" for Math
|
|
import "luxe: draw" for Draw
|
|
import "luxe: io" for IO
|
|
import "random" for Random
|
|
|
|
import "outline/app" for App
|
|
|
|
class game is Game {
|
|
|
|
construct ready() {
|
|
|
|
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 = 1.5
|
|
|
|
// starfield
|
|
_star_scale = 2
|
|
|
|
// player ship
|
|
_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, app.width / _camera_scale, app.height / _camera_scale, -1, 1)
|
|
|
|
draw_starfield()
|
|
create_ship()
|
|
Transform.set_pos(app.camera, Transform.get_pos_x(_ship) - (app.width / 2 / _camera_scale), Transform.get_pos_y(_ship) - (app.height / 2 / _camera_scale))
|
|
|
|
} //ready
|
|
|
|
tick(delta) {
|
|
|
|
tick_ship(delta)
|
|
tick_camera(delta)
|
|
tick_starfield(delta)
|
|
|
|
if(Input.key_state_released(Key.escape)) {
|
|
IO.shutdown()
|
|
}
|
|
|
|
app.tick(delta)
|
|
|
|
} //tick
|
|
|
|
//////////////
|
|
// CREATORS //
|
|
//////////////
|
|
|
|
draw_starfield() {
|
|
|
|
//todo: understand this starfield, expand it, make it move.
|
|
var rng = Random.new(25)
|
|
var ctx = Draw.create(World.render_set(app.world))
|
|
var color = [1,1,1,1]
|
|
for(i in 0 ... 1000) {
|
|
var x = rng.int(-app.width / _star_scale, app.width / _star_scale)
|
|
var y = rng.int(-app.height / _star_scale, app.height / _star_scale)
|
|
var z = 0
|
|
var w = rng.int(1, 4)
|
|
// var h = rng.int(1, 3)
|
|
var h = w
|
|
var a = 0
|
|
color.a = 0.1 * w
|
|
Draw.quad(ctx, x, y, z, w, h, a, color)
|
|
}
|
|
Draw.commit(ctx)
|
|
|
|
} //draw_starfield
|
|
|
|
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
|
|
|
|
//////////////
|
|
// UPDATERS //
|
|
//////////////
|
|
|
|
tick_ship(delta) {
|
|
|
|
var input_vec = [get_axis("horizontal"), get_axis("vertical"), 0]
|
|
Math.normalize(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)
|
|
System.print("[%(Transform.get_pos_x(_ship)), %(Transform.get_pos_y(_ship))]")
|
|
|
|
} //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 - (app.width / 2 / _camera_scale), camerax, interpolation)
|
|
var camera_interp_y = lerp(shipy - (app.height / 2 / _camera_scale), cameray, interpolation)
|
|
Transform.set_pos(app.camera, camera_interp_x, camera_interp_y)
|
|
|
|
} //tick_camera
|
|
|
|
tick_starfield(delta) {
|
|
|
|
} //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
|