Skip to content

Commit be13b29

Browse files
committed
..
1 parent c772f6b commit be13b29

File tree

3 files changed

+62
-45
lines changed

3 files changed

+62
-45
lines changed

sunsnake_compiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function compile(script) {
2626
script = script.replaceAll('.index(', '.indexOf(')
2727
script = script.replaceAll(' self.', ' this.')
2828
script = script.replaceAll('(self.', '(this.')
29+
script = script.replaceAll('def __init__(self, ', 'constructor(')
2930
// script = script.replaceAll('(self.', '(this.')
3031

3132
var all_lines = script.split('\n')
@@ -108,7 +109,7 @@ function compile(script) {
108109
}
109110

110111
if (all_lines[i].startsWith('class ')) {
111-
class_name = all_lines[i].slice(6).split(' ')[0]
112+
class_name = all_lines[i].slice(6).split(' ')[0].split('(')[0].split(':')[0]
112113
print('found class:', class_name)
113114
_class_names.push(class_name)
114115
}

taptapir.js

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// aspect_ratio = 16/9
21
scale = 1
32
print = console.log
43

@@ -85,56 +84,66 @@ scene._children = []
8584
_game_window.appendChild(scene)
8685

8786
// print('browser aspect_ratio:', browser_aspect_ratio)
88-
format = null
8987
is_mobile = 'ontouchstart' in document.documentElement
9088
fullscreen = false
9189
camera = null
90+
ASPECT_RATIO = 9/16
9291

9392
function set_orientation(value) {
93+
if (value == 'vertical') {
94+
aspect_ratio = 9/16
95+
}
96+
if (value == 'horizontal') {
97+
aspect_ratio = 16/9
98+
}
99+
set_aspect_ratio(aspect_ratio)
100+
}
101+
102+
// set_orientation
103+
function set_aspect_ratio(aspect_ratio) {
104+
ASPECT_RATIO = aspect_ratio
105+
print('set aspect ratio to:', aspect_ratio)
94106
var width = window.innerWidth
95107
var height = window.innerHeight
96108
browser_aspect_ratio = width / height
97109
// print('width:', width, 'height:', height, 'browser_aspect_ratio:', browser_aspect_ratio)
98-
99-
format = value
100-
if (format == 'vertical') {
101-
aspect_ratio = 16/9
110+
if (aspect_ratio < 1) {
102111
// used for setting correct draggable limits
103112
asp_x = 1
104-
asp_y = 9/16
113+
asp_y = aspect_ratio
105114

106-
if (browser_aspect_ratio >= 9/16) { // if the screen is wider than the game, like a pc monitor.
107-
// print('vertical view desktop')
108-
_game_window.style.width = `${width*scale/browser_aspect_ratio/(16/9)}px`
115+
if (browser_aspect_ratio < aspect_ratio) { // if the screen is wider than the game, like a pc monitor.
116+
print('vertical view desktop')
117+
_game_window.style.width = `${width*scale/browser_aspect_ratio*aspect_ratio}px`
109118
_game_window.style.height = `${height*scale}px`
110119
}
111120
else { // if the screen is taller than the game, like a phone screen.
112-
// print('vertical view mobile')
113-
_game_window.style.height = `${width*scale*(16/9)}px`
114-
_game_window.style.width = `${width*scale}px`
115-
}
116-
if (camera) {camera.ui.scale = [1, 1/aspect_ratio]}
117-
top_left = [-.5, .5*aspect_ratio]
118-
top_right = [.5, .5*aspect_ratio]
119-
bottom_left = [-.5, -.5*aspect_ratio]
120-
bottom_right = [.5, -.5*aspect_ratio]
121-
top = [0, .5*aspect_ratio]
122-
bottom = [0, -.5*aspect_ratio]
121+
print('vertical view mobile', width, width/aspect_ratio, height)
122+
_game_window.style.height = `${height*scale}px`
123+
_game_window.style.width = `${height*scale*aspect_ratio}px`
124+
}
125+
if (camera) {camera.ui.scale = [1, 1*aspect_ratio]}
126+
top_left = [-.5, .5/aspect_ratio]
127+
top_right = [.5, .5/aspect_ratio]
128+
bottom_left = [-.5, -.5/aspect_ratio]
129+
bottom_right = [.5, -.5/aspect_ratio]
130+
top = [0, .5/aspect_ratio]
131+
bottom = [0, -.5/aspect_ratio]
123132
left = [-.5, 0]
124133
right = [.5, 0]
125134
}
126135
else {
127-
aspect_ratio = 16/9
128-
asp_x = 16/9
136+
// aspect_ratio = 16/9
137+
asp_x = aspect_ratio
129138
asp_y = 1
130139
scene.style.width = `${1/asp_x*100}%`
131140
scene.style.height = `${1/asp_y*100}%`
132-
if (browser_aspect_ratio > 16/9) { // if the screen is wider than 16/9, fit to height
141+
if (browser_aspect_ratio > aspect_ratio) { // if the screen is wider than 16/9, fit to height
133142
_game_window.style.height = `${height*scale}px`
134-
_game_window.style.width = `${width*scale/browser_aspect_ratio*16/9}px`
143+
_game_window.style.width = `${width*scale/browser_aspect_ratio*aspect_ratio}px`
135144
}
136145
else { // if the screen is taller than 16/9, fit to width
137-
_game_window.style.height = `${height*scale*browser_aspect_ratio/(16/9)}px`
146+
_game_window.style.height = `${height*scale*browser_aspect_ratio/aspect_ratio}px`
138147
_game_window.style.width = `${width*scale}px`
139148
}
140149
if (camera) {camera.ui.scale = [1/aspect_ratio, 1]}
@@ -253,7 +262,8 @@ function set_window_color(value) {_game_window.style.backgroundColor = value}
253262
function set_background_color(value) {document.body.style.backgroundColor = value}
254263
function set_scale(value) {
255264
scale = value
256-
set_orientation(format)
265+
// set_orientation(format)
266+
set_aspect_ratio(ASPECT_RATIO)
257267
}
258268

259269
function set_fullscreen(value) {
@@ -299,7 +309,7 @@ LAST_SCENE = null
299309
DEFAULT_FONT = null
300310
TEXT_SIZE_MULTIPLIER = 1
301311

302-
class Entity {
312+
class Entity {
303313
constructor(options=null) {
304314
if (!('type' in options)) {
305315
options['type'] = 'entity'
@@ -444,7 +454,7 @@ class Entity {
444454
else {
445455
this.el.style.visibility = 'hidden'
446456
}
447-
457+
448458
this._enabled = value
449459

450460
if (value && this.on_enable) {
@@ -666,7 +676,7 @@ class Entity {
666676
get text_size() {return this._text_size}
667677
set text_size(value) {
668678
this._text_size = value
669-
if (format == 'vertical') {
679+
if (aspect_ratio < 1) {
670680
this.model.style.fontSize = `${value * scale * 1 * TEXT_SIZE_MULTIPLIER}vh`
671681
}
672682
else {
@@ -776,7 +786,7 @@ class Entity {
776786
}
777787
else {
778788
// fallback to linear
779-
// entity[variable_name] = lerp(start_value, target_value, t)
789+
// entity[variable_name] = lerp(start_value, target_value, t)
780790
}
781791
},
782792
1000*i/60
@@ -824,6 +834,9 @@ class Entity {
824834
look_at(target_pos) {
825835
this.rotation = -(Math.atan2(target_pos[1] - this.y, target_pos[0] - this.x)) * (180/Math.PI)
826836
}
837+
look_in_direction(direction) {
838+
this.rotation = -(Math.atan2(direction[1], direction[0])) * (180/Math.PI)
839+
}
827840

828841
destroy_children() {
829842
for (let _entity of this.children) {
@@ -842,7 +855,7 @@ class Entity {
842855
async function check_image(url){
843856
const res = await fetch(url);
844857
const buff = await res.blob();
845-
858+
846859
return buff.type.startsWith('image/')
847860
}
848861

@@ -912,7 +925,7 @@ class Camera {
912925
get x() {return this._x}
913926
set x(value) {
914927
this._x = value
915-
if (format == 'vertical') {
928+
if (aspect_ratio < 1) {
916929
scene.style.left = `${50+(-value*100/this.fov)}%`
917930
}
918931
else {
@@ -922,7 +935,7 @@ class Camera {
922935
get y() {return this._y}
923936
set y(value) {
924937
this._y = value
925-
if (format == 'vertical') {
938+
if (aspect_ratio < 1) {
926939
scene.style.top = `${50+(value*100*asp_y/this.fov)}%`
927940
}
928941
else {
@@ -955,7 +968,7 @@ class Camera {
955968
this._fov = value
956969
scene.style.width = `${1/value*100/asp_x}%`
957970

958-
if (format == 'vertical') {
971+
if (aspect_ratio < 1) {
959972
scene.style.height = `${1/value*100/asp_x*asp_y}%`
960973
}
961974
else {
@@ -978,7 +991,7 @@ class Camera {
978991
after(duration, () => {
979992
this.xy = original_xy
980993
})
981-
994+
982995
}
983996
}
984997
}
@@ -1018,11 +1031,11 @@ function Scene(input) {
10181031
settings = {parent:scene, visible_self:false, on_enter:null, on_exit:null, enabled:false, texture:null}
10191032
_bg_color = input.color
10201033
input.color = null
1021-
1034+
10221035
for (const [key, value] of Object.entries(input)) {
10231036
settings[key] = value
10241037
}
1025-
1038+
10261039
_entity = new Entity(settings)
10271040

10281041
if (input.texture || _bg_color) {
@@ -1377,7 +1390,7 @@ function _onmousemove(pageX, pageY, clientX, clientY, pressure, target) {
13771390
}
13781391
document.addEventListener('mousemove', (event) => {
13791392
_onmousemove(event.pageX, event.pageY, event.clientX, event.clientY, event.pressure, event.target)
1380-
1393+
13811394
})
13821395
document.addEventListener('touchmove', (event) => {
13831396
event = event.touches[0]
@@ -1432,7 +1445,7 @@ function Video(options) {
14321445
_entity = new Entity(settings)
14331446
// let name = settings['name']
14341447

1435-
let video_entity = new Entity(settings)
1448+
let video_entity = new Entity(settings)
14361449
video_entity.video = document.createElement('video')
14371450
video_entity.video.src = settings['name']
14381451
video_entity.video.controls = false
@@ -1532,7 +1545,7 @@ function sample(population, k){
15321545

15331546
return result;
15341547
}
1535-
1548+
15361549
function grid_layout(l, options) {
15371550
let settings = {spacing:[1.1,1.1], offset:[0,0], max_x:16}
15381551
for (const [key, value] of Object.entries(options)) {
@@ -1642,7 +1655,7 @@ function _input(event) {
16421655
else if (event.type == "keydown") { // prevent key repeat
16431656
return
16441657
}
1645-
1658+
16461659
if (key == 'left mouse up') {
16471660
// drop draggables
16481661
for (var e of entities) {
@@ -1664,13 +1677,13 @@ function _input(event) {
16641677
input(key)
16651678
}
16661679

1667-
// swipe input
1680+
// swipe input
16681681
if (key == 'left mouse up') {
16691682
mouse.click_end_position = mouse.position
16701683
if (time - time_of_press < .15) {
16711684
diff_x = mouse.position[0] - mouse.swipe_start_position[0]
16721685
diff_y = mouse.position[1] - mouse.swipe_start_position[1]
1673-
1686+
16741687
if (diff_x < -.05 && abs(diff_y) < .15) {
16751688
_input('swipe left')
16761689
}
@@ -1714,3 +1727,6 @@ function _fullscreenchange() {
17141727
document.addEventListener('fullscreenchange', _fullscreenchange)
17151728

17161729
set_orientation('vertical')
1730+
// set_orientation('horizontal')
1731+
// set_aspect_ratio(.5)
1732+
set_scale(.95)

0 commit comments

Comments
 (0)