basics of moving stars done

This commit is contained in:
Jonathan Hirz 2019-01-26 16:52:14 -08:00
parent 7600d281b8
commit df976707ca
4 changed files with 87 additions and 24 deletions

104
game.wren
View file

@ -6,6 +6,7 @@ import "luxe: math" for Math
import "luxe: draw" for Draw
import "luxe: io" for IO
import "random" for Random
// import "luxe: array" for Lists
import "outline/app" for App
@ -25,6 +26,8 @@ class game is Game {
// starfield
_star_scale = 2
_stars = []
_stars2 = []
// player ship
_ship_rotation = 0
@ -35,12 +38,16 @@ class game is Game {
_ship_velocity_y = 0
_ship_dampening = 0.85
Camera.ortho(app.camera, 0, 0, app.width / _camera_scale, app.height / _camera_scale, -1, 1)
Camera.ortho(app.camera, 0, 0, app.width / _camera_scale, app.height / _camera_scale, -5, 5)
draw_starfield()
create_ship()
create_startracker()
draw_starfield_2()
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))
// NOTES
// z-layers: -1 stars, 0 player ship
} //ready
tick(delta) {
@ -61,27 +68,6 @@ class game is Game {
// 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")
@ -92,6 +78,66 @@ class game is Game {
} //create_ship
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
// draw_starfield() {
// //todo: understand this starfield, expand it, make it move.
// //todo: for now, just generate a very large starfield. Do something at the "edge of space"]
// var rng = Random.new(25)
// var ctx = Draw.create(World.render_set(app.world))
// var color = [1,1,1,1]
// for(i in 0 ... 100000) {
// var x = rng.int(-app.width / _star_scale * 10, app.width / _star_scale * 10)
// var y = rng.int(-app.height / _star_scale * 10, app.height / _star_scale * 10)
// var z = -1
// 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
draw_starfield_2() {
//try to do this again, making sprites/entities for each star, and attach to player ship transform
//Transform.set_pos(x, y, -1)
var rng = Random.new()
var star_spread = 1000
for(i in 0 ... 1000) {
_star = Entity.create(app.world, "star")
Transform.create(_star)
Transform.set_pos(_star, rng.int(-star_spread, star_spread), rng.int(-star_spread, star_spread), -1)
var star_mat = Assets.material("material/star")
Sprite.create(_star, star_mat, 8, 8)
Transform.link(_star, _star_tracker)
_stars.add(_star)
}
for(i in 0 ... 1000) {
_star = Entity.create(app.world, "star")
Transform.create(_star)
Transform.set_pos(_star, rng.int(-star_spread, star_spread), rng.int(-star_spread, star_spread), -1)
var star_mat = Assets.material("material/star")
Sprite.create(_star, star_mat, 4, 4)
Transform.link(_star, _star_tracker)
_stars2.add(_star)
}
} //draw_starfield_2
//////////////
// UPDATERS //
//////////////
@ -117,7 +163,7 @@ class game is Game {
// move ship
Transform.translate(_ship, _ship_velocity_x, _ship_velocity_y)
System.print("[%(Transform.get_pos_x(_ship)), %(Transform.get_pos_y(_ship))]")
// System.print("[%(Transform.get_pos_x(_ship)), %(Transform.get_pos_y(_ship))]")
} //tick_ship
@ -137,6 +183,16 @@ class game is Game {
tick_starfield(delta) {
Transform.set_pos(_star_tracker, Transform.get_pos_x(_ship), Transform.get_pos_y(_ship), Transform.get_pos_z(_ship))
for(star in _stars) {
Transform.set_pos_x(star, Transform.get_pos_x(star) + -_ship_velocity_x)
Transform.set_pos_y(star, Transform.get_pos_y(star) + -_ship_velocity_y)
}
for(star in _stars2) {
Transform.set_pos_x(star, Transform.get_pos_x(star) + -_ship_velocity_x * 0.9)
Transform.set_pos_y(star, Transform.get_pos_y(star) + -_ship_velocity_y * 0.9)
}
} //tick_starfield
/////////////

3
image/star.image.lx Normal file
View file

@ -0,0 +1,3 @@
image = {
source = "image/star.png"
}

BIN
image/star.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 B

4
material/star.material Normal file
View file

@ -0,0 +1,4 @@
material = {
basis = "luxe: material_basis/sprite_pixelated"
samplers = { 0 = "image/star" }
}